Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26254 | Accepted: 7833 |
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
Source
有修改的线段树,颜色需要位运算。
代码很恶心,不看也罢。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef struct { int l,r,lazy,vis; }Tree; char str[5]; Tree tree[500000]; int num; int ans; void Build(int t,int l,int r) { int mid; tree[t].lazy=0; tree[t].l=l; tree[t].r=r; if (l==r) return; mid=(l+r)/2; Build(2*t+1,l,mid); Build(2*t+2,mid+1,r); } void Color(int t,int x,int y,int c) { int l,r,mid,i,j,k; l=tree[t].l; r=tree[t].r; mid=(l+r)/2; if (l==x && r==y) { tree[t].vis=c; tree[t].lazy=1; return; } if (tree[t].lazy==1) { tree[t].lazy=0; Color(2*t+1,l,mid,tree[t].vis); Color(2*t+2,mid+1,r,tree[t].vis); } if (x<=mid) { Color(2*t+1,x,min(mid,y),c); } if (y>=mid+1) { Color(2*t+2,max(x,mid+1),y,c); } i=2*t+1; j=2*t+2; tree[t].vis=(tree[i].vis | tree[j].vis); } void Find(int t,int x,int y) { int l,r,mid,i; l=tree[t].l; r=tree[t].r; if (l==x && r==y) { ans=(ans | tree[t].vis); return; } mid=(l+r)/2; if (tree[t].lazy==1) { tree[t].lazy=0; Color(2*t+1,l,mid,tree[t].vis); Color(2*t+2,mid+1,r,tree[t].vis); } if (x<=mid) { Find(2*t+1,x,min(y,mid)); } if (y>=mid+1) { Find(2*t+2,max(x,mid+1),y); } } int main() { int i,j,n,q,x,y,z,cnt; while(scanf("%d%d%d",&n,&num,&q)!=EOF) { Build(0,0,n-1); Color(0,0,n-1,1); while(q--) { scanf("%s",str); scanf("%d%d",&x,&y); if (x>y) { swap(x,y); } if (str[0]=='C') { scanf("%d",&z); Color(0,x-1,y-1,1<<(z-1)); } else { ans=0; Find(0,x-1,y-1); cnt=0; for (i=0;i<num;i++) { if ((ans & (1<<i))!=0) cnt++; } printf("%d\n",cnt); } } } return 0; }