C ++ - POJ2777-Count Color [segment tree] [tag the lazy] [section Modification]

 

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int MAXN=1e5+10;
 5 struct node{int l,r,lazy,color;}t[MAXN*4];
 6 int L,R,C,n,m,q;
 7 #define ls t[x].l
 8 #define rs t[x].r
 9 int count(int x){int ans=0;for(;x;x>>=1)if(x&1)ans++;return ans;}
10 void pushdown(int x){if(t[x].lazy)t[ls].lazy=t[rs].lazy=t[ls].color=t[rs].color=t[x].lazy,t[x].lazy=0;}
11 void update(int x){t[x].color=t[ls].color|t[rs].color;}
12 void build(int x,int l,int r){
13     if(l==r){t[x].color=1;return;}
14     int mid=(l+r)>>1;t[x].l=(x<<1),t[x].r=(x<<1|1);
15     build(ls,l,mid),build(rs,mid+1,r),update(x);
16 }
17 void update(int x,int l,int r){
18     if(L<=l&&r<=R){t[x].color=t[x].lazy=1<<(C-1);return;}
19     pushdown(x);int mid=(l+r)>>1;
20     if(L<=mid)update(ls,l,mid);
21     if(R>mid)update(rs,mid+1,r);
22     update(x);
23 }
24 int query(int x,int l,int r){
25     if(L<=l&&r<=R)return t[x].color;
26     pushdown(x);int mid=(l+r)>>1,ans=0;
27     if(L<=mid)ans|=query(ls,l,mid);
28     if(R>mid)ans|=query(rs,mid+1,r);
29     return ans;
30 }
31 int main(){
32     scanf("%d%d%d",&n,&m,&q),build(1,1,n);
33     for(int a,b;q--;){
34         char s[2];scanf("%s",s);
35         if(s[0]=='C')scanf("%d%d%d",&a,&b,&C),L=min(a,b),R=max(a,b),update(1,1,n);
36         else scanf("%d%d",&a,&b),L=min(a,b),R=max(a,b),printf("%d\n",count(query(1,1,n)));
37     }
38     return 0;
39 }

 

Guess you like

Origin www.cnblogs.com/JasonCow/p/12347045.html