切水果 [Codevs1299]

来源:互联网 发布:软件开发创业项目 编辑:程序博客网 时间:2024/04/30 10:25

AC通道 :http://codevs.cn/problem/1299/

本题分析请看我另一篇博客“疯狂的馒头”。

#include <iostream>#include <cstdio>using namespace std;int fa[500010];int n,m;int find(int x){int tmp=x,pre;while(tmp!=fa[tmp])tmp=fa[tmp];while(x!=tmp){pre=fa[x];fa[x]=tmp;x=pre;}return tmp;}int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)fa[i]=i;for(int i=1;i<=m;i++){int l,r,tot=0;scanf("%d%d",&l,&r);for(int i=find(l);i<=r;i=find(i)){if(i==0)break;tot++;fa[i]=i+1;}n-=tot;printf("%d\n",n);}return 0;}


1 0
原创粉丝点击