hdu 1556 Color the ball

来源:互联网 发布:怪物猎人x数据库 编辑:程序博客网 时间:2024/06/08 06:15

简单线段树,类似延迟更新的思想,最终统计的时候再更新到底

#include<iostream>#define maxn 100010using namespace std;int n;struct stu{int l,r,s;};stu mapp[maxn*4];void build(int l,int r,int count){mapp[count].l=l;mapp[count].r=r;mapp[count].s=0;if(l==r) return;int mid=(l+r)/2;build(l,mid,count*2);build(mid+1,r,count*2+1);}void updata(int l,int r,int count){if(mapp[count].l==l&&mapp[count].r==r){mapp[count].s++;return;}int mid=(mapp[count].l+mapp[count].r)/2;if(r<=mid) updata(l,r,count*2);else if(l>mid) updata(l,r,count*2+1);else{updata(l,mid,count*2);updata(mid+1,r,count*2+1);}}void output(int l,int r,int count){if(l==r){if(l==1) cout<<mapp[count].s;else cout<<" "<<mapp[count].s;return ;}int mid=(l+r)/2;mapp[count*2].s+=mapp[count].s;mapp[count*2+1].s+=mapp[count].s;output(l,mid,count*2);output(mid+1,r,count*2+1);}int main(){cin.sync_with_stdio(false);while(cin>>n&&n){build(1,n,1);for(int i=0;i<n;i++){int a,b;cin>>a>>b;updata(a,b,1);}output(1,n,1);cout<<endl;}return 0;} 


0 0
原创粉丝点击