# codevs1299： 切水果

10 3

3 5

2 8

1 5

7

3

2

30%的数据满足N，M<=5,000

60%的数据满足N，M<=100,000

100% 的数据满足1<=L<=R<=N<=500,000，1<=M<=500,000

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #define maxn 500010
5 using namespace std;
6 int n,m,fa[maxn],ans,l,r;
7 int find(int x)
8 {
9     return x==fa[x]?x:fa[x]=find(fa[x]);
10 }
11 int main()
12 {
13     scanf("%d%d",&n,&m);
14     for(int i=1 ; i<=n+5 ; ++i)fa[i]=i;
15     for(int i=1 ; i<=m ; ++i )
16     {
17         scanf("%d%d",&l,&r);
18         if(n-ans<0)
19         {
20             printf("0\n");
21             continue;
22         }
23         for(int j=l ; j<=r; )
24         {
25             int fx=find(j);
26             if(fx==j)
27             {
28                 fa[j]=j+1;
29                 ++ans;
30                 j++;
31             }
32             else
33             {
34                 j=fx;
35             }
36         }
37         if(n-ans<0)printf("0\n");
38         else printf("%d\n",n-ans);
39     }
40     return 0;
41 }

posted @ 2017-09-22 23:35 傅judge 阅读(...) 评论(...) 编辑 收藏