Gym - 101350F Monkeying Around (差分)

版权声明:本文为蒟蒻原创文章,转载请注明出处哦~ https://blog.csdn.net/a54665sdgf/article/details/82748052

题目链接

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

1
10 7
3 11 0
3 11 2
5 12 1
8 13 2
7 11 2
10 12 1
9 12 0

Output

3

利用差分的思想,把每个笑话的左端点和右端点打上标记,然后从左到右扫一遍,用优先队列维护每只猴子最后听到的笑话,用数组维护每个笑话听到的次数。

#define FRER() freopen("i.txt","r",stdin)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int n,m;
int type[N],cnt[N],inq[N];
int head[N],nxt[N<<1],val[N<<1],nnode;

void add(int u,int x)
{
    nxt[nnode]=head[u],val[nnode]=x,head[u]=nnode++;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(head,-1,sizeof head);
        nnode=0;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; ++i)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            int l=max(a-c,1),r=min(a+c,n);
            type[i]=b;
            add(l,i),add(r+1,-i);
        }
        priority_queue<int> q;
        memset(cnt,0,sizeof cnt);
        memset(inq,0,sizeof inq);
        int ans=0;
        for(int i=1; i<=n; ++i)
        {
            for(int e=head[i];~e; e=nxt[e])
            {
                int x=val[e];
                if(x>0)
                {
                    cnt[type[x]]++;
                    inq[x]=1;
                    q.push(x);
                }
                else
                {
                    cnt[type[-x]]--;
                    inq[-x]=0;
                }
            }
            while(!q.empty()&&!inq[q.top()])q.pop();
            if(q.empty()||cnt[type[q.top()]]>1)++ans;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a54665sdgf/article/details/82748052