Codeforces gym 101350F 想法

来源:互联网 发布:excel找重复数据 编辑:程序博客网 时间:2024/05/09 08:17

Monkeying Around
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

When the monkey professor leaves his class for a short time, all the monkeys go bananas. N monkeys are lined up sitting side by side on their chairs. They each have the same joke book. Before the professor returns, M jokes were heard.

Each of the M jokes are said in the order given and have the following properties:

xi - position of the monkey who said it.

li – index of the joke in the book.

ki – volume the monkey says that joke.

When the monkey at position xi says the joke li, all monkeys at a distance less than or equal to ki from that monkey (including the monkey who said the joke) will fall off their chairs in laughter if they have never heard the joke li before.

If the joke li has been heard anytime during the past before, and the monkey hears it again, then he will sit back up in his chair.

A monkey can fall off his chair more than once (every time he hears a new joke), and if he is already on the ground and hears a new joke, he will stay on the ground.

Can you figure out how many monkeys will be in their seats by the time the professor comes back?

Input

The first line of input is T – the number of test cases.

The first line of each test case is NM (1 ≤ N ≤ 105) (1 ≤ M ≤ 105) – the number of monkeys in the class, and the number of jokes said before the professor returns.

The next M lines contain the description of each joke: xi, li, ki (1 ≤ xi ≤ N) (1 ≤ li ≤ 105) (0 ≤ ki ≤ N).

Output

For each test case, output on a line a single integer - the number of monkeys in their seats after all jokes have been said.

Example
input
110 73 11 03 11 25 12 18 13 27 11 210 12 19 12 0
output
3

题意:有n只猴子  m个笑话

每个笑话的讲的猴子的编号  笑话的编号  传播半径

如果笑话传播到当前猴子  如果他听过  就会笑到地上  否则他会上树

问最后有几只猴子在树上


题解:先处理出所有线段  然后对于当前的猴子  找出影响他的笑话最后一个是哪个  并找出他听过这个笑话的次数


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<set>using namespace std;int num[100005],cnt;struct node{int x,y,nu,lab;}e[100005];vector<int>sp[100005];struct nodes{int num,lab;bool operator <(const nodes& a)const{if(lab==a.lab)return num<a.num;return lab<a.lab;}};set<nodes>sd;set<nodes>::iterator it;int main(){int t;scanf("%d",&t);while(t--){int n,m,i,j,x,y,z;scanf("%d%d",&n,&m);memset(num,0,sizeof(num));sd.clear();for(i=1;i<=n;i++)sp[i].clear();for(i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&z);e[i].lab=i;e[i].nu=y;e[i].x=max(1,x-z);e[i].y=min(n,x+z);sp[e[i].x].push_back(i);sp[e[i].y+1].push_back(-i);}int ans=0;for(i=1;i<=n;i++){for(j=0;j<sp[i].size();j++){if(sp[i][j]>0){num[e[sp[i][j]].nu]++;sd.insert((nodes){e[sp[i][j]].nu,e[sp[i][j]].lab});}else{num[e[-sp[i][j]].nu]--;sd.erase((nodes){e[-sp[i][j]].nu,e[-sp[i][j]].lab});}}if(sd.empty())ans++;else{it=sd.end();it--;if(num[(*it).num]!=1)ans++;}}printf("%d\n",ans);}return 0;}


1 0
原创粉丝点击