标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39905 | Accepted: 12034 |
Description
Input
Output
Sample Input
2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2
Sample Output
2 1 写完之后就是wrong,看别人说要用位运算,第一次用位运算 ac代码:#include<stdio.h> struct s { int color; int left; int right; int lazy; }tree[4000000+1000]; void pushdown(int i) { if(tree[i].lazy) { tree[i*2].lazy=tree[i*2+1].lazy=1; tree[i*2].color=tree[i].color; tree[i*2+1].color=tree[i].color; tree[i].lazy=0; } } void build(int l,int r,int i) { int mid; tree[i].left=l; tree[i].right=r; tree[i].lazy=1; tree[i].color=1; if(l==r) return; mid=(l+r)/2; build(l,mid,i*2); build(mid+1,r,i*2+1); } void update(int l,int r,int c,int i) { int mid; if(tree[i].left>=l&&tree[i].right<=r) { tree[i].lazy=1; tree[i].color=(1<<(c-1)); return; } pushdown(i); mid=(tree[i].left+tree[i].right)/2; if(r<=mid) update(l,r,c,i*2); else if(l>mid) update(l,r,c,i*2+1); else { update(l,mid,c,i*2); update(mid+1,r,c,i*2+1); } tree[i].color=tree[i*2].color|tree[i*2+1].color; } int query(int l,int r,int i) { int mid; if(l<=tree[i].left&&r>=tree[i].right) return tree[i].color; pushdown(i); mid=(tree[i].left+tree[i].right)/2; if(r<=mid) return query(l,r,i*2); else if(l>mid) return query(l,r,i*2+1); else return query(l,mid,i*2)|query(mid+1,r,i*2+1); } int fun(int x) { int num=0; while(x) { if(x%2) num++; x/=2; } return num; } int main() { int i,n,l,t; int a,b,c,ans; char ch[3]; while(scanf("%d%d%d",&l,&t,&n)!=EOF) { build(1,l,1); for(i=0;i<n;i++) { scanf("%s",ch); if(ch[0]=='C') { scanf("%d%d%d",&a,&b,&c); if(b<a) update(b,a,c,1); else update(a,b,c,1); } else if(ch[0]=='P') { scanf("%d%d",&a,&b); if(b<a) ans=query(b,a,1); else ans=query(a,b,1); printf("%d\n",fun(ans)); } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
标签:
原文地址:http://blog.csdn.net/helloiamclh/article/details/48132311