codevs 1191 数轴染色

题目描述 Description

在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。

输入描述 Input Description

输入一行为N和M。下面M行每行两个数Li、Ri

输出描述 Output Description

输出M行,为每次操作后剩余黑色点的个数。

样例输入 Sample Input

10 3
3 3
5 7
2 8

样例输出 Sample Output

9
6
3

数据范围及提示 Data Size & Hint

数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000

神奇并查集:

#include<cstdio>

int f[200001];

int findfa(int x)
{
    if(f[x])
    {
        f[x]=findfa(f[x]);
        return f[x];
    }
    else
        return x;
}
int n,m,len;
int l,r;

int main()
{
    scanf("%d%d",&n,&m);
    len=n;
    while(m--)
    {
        scanf("%d%d",&l,&r);
        l=findfa(l);
        while(l<=r)
        {
            f[l]=l+1;
            len--;
            l=findfa(l);
        }
        printf("%d\n",len);
    }
}
/*
10 3
3 3
5 7
2 8
*/

 

posted @ 2017-06-21 21:01  ioioioioioio  阅读(124)  评论(0编辑  收藏  举报