hdu 5023 线段树延迟更新+状态压缩
2014-09-22 15:27
141 查看
/* 线段树延迟更新+状态压缩 */ #include<stdio.h> #define N 1100000 struct node { int x,y,yanchi,sum; }a[N*4]; int lower[31]; void build(int t,int x,int y) { a[t].x=x; a[t].y=y; a[t].yanchi=0; if(x==y){ a[t].sum=lower[1]; return ; } int temp=t<<1; int mid=(x+y)/2; build(temp,x,mid); build(temp+1,mid+1,y); a[t].sum=a[temp].sum|a[temp+1].sum; } void inset(int t,int x,int y,int c) { //printf("%d %d %d\n",a[t].yanchi,a[t].x,a[t].y); if(a[t].x==x&&a[t].y==y) { a[t].yanchi=lower[c]; a[t].sum=lower[c];//sum和yanchi都需要改变 // printf("%d %d %d\n",x,y,lower[c]); return ; } int temp=t*2; int mid=(a[t].x+a[t].y)/2; if(a[t].yanchi) {//如果区间a[t].x--a[t].y被操作过就赋给子区间 a[temp].yanchi=a[t].yanchi; a[temp].sum=a[t].sum; a[temp+1].yanchi=a[t].yanchi; a[temp+1].sum=a[t].sum; a[t].yanchi=0; } if(x>mid)inset(temp+1,x,y,c); else if(y<=mid)inset(temp,x,y,c); else { inset(temp,x,mid,c); inset(temp+1,mid+1,y,c); } a[t].sum=a[temp].sum|a[temp+1].sum; return ; } int qury(int t,int x,int y) { // printf("%d %d %d\n",a[t].yanchi,a[t].x,a[t].y); if(a[t].x==x&&a[t].y==y) { // printf("%d\n",a[t].sum); return a[t].sum; } int temp=t*2; int mid=(a[t].x+a[t].y)/2; if(a[t].yanchi) {//如果区间a[t].x--a[t].y被操作过就赋给子区间 a[temp].yanchi=a[t].yanchi; a[temp].sum=a[t].sum; a[temp+1].yanchi=a[t].yanchi; a[temp+1].sum=a[t].sum; a[t].yanchi=0; } if(x>mid) return qury(temp+1,x,y); else if(y<=mid) return qury(temp,x,y); else return qury(temp,x,mid)|qury(temp+1,mid+1,y); a[t].sum=a[temp+1].sum|a[temp].sum;//更新 } int main() { int n,m,i,j,k,ans; char s[10]; lower[0]=1; for(i=1;i<=30;i++) lower[i]=lower[i-1]*2; while(scanf("%d%d",&n,&m),n||m) { build(1,1,n); while(m--) { scanf("%s",s); if(s[0]=='P') { scanf("%d%d%d",&i,&j,&k); inset(1,i,j,k-1); } if(s[0]=='Q') { scanf("%d%d",&i,&j); ans=qury(1,i,j); // printf("%d\n",ans); k=0; for(i=0;i<30;i++) if(ans&lower[i]) { if(k) printf(" "); printf("%d",i+1); k=1; } printf("\n"); } } } return 0; }
相关文章推荐
- hdu 5023 线段树延迟更新+状态压缩
- HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩
- poj2777 Count Color线段树延迟更新,二进制状态记录
- HDU 5023 - A Corrupt Mayor's Performance Art(线段树区间更新+状态压缩)
- HDU 5023(区间更新/状态压缩)
- hdoj 2777 Count Color 【线段树lazy区间染色 + 查询区间颜色数目 + 状态压缩】
- poj 2777 Count Color(线段树、状态压缩、位运算)
- 线段树成段更新之延迟更新
- POJ 2777 Count Color——区间更新的线段树+状态压缩
- hdu3954 线段树延迟更新
- 线段树区间更新区间求和(转延迟标记精讲)
- zoj 1610 Count the Colors(线段树延迟更新)
- poj3468 A Simple Problem with Integers 线段树延迟标记区间更新区间求和
- hdu1698-Just a Hook (线段树区间更新)(延迟标记)
- poj 3468 A Simple Problem with Integers(线段树、延迟更新)
- poj 2777 Count Color ——线段树(带状态压缩)
- HDU 1698 Just a Hook(线段树 + Lazy Tag(延迟更新))
- hdu1698 Just a Hook 线段树更新延迟标记
- POJ P2777 Count Color——线段树状态压缩
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)