POJ 2777 Count Color

发布时间:2017-1-22 18:09:13 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"POJ 2777 Count Color",主要涉及到POJ 2777 Count Color方面的内容,对于POJ 2777 Count Color感兴趣的同学可以参考一下。

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31512   Accepted: 9447 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board: 1. "C A B C" Color the board from segment A to segment B with color C. 2. "P A B" Output the number of different colors painted between segment A and segment B (including). In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your. Input First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously. Output Ouput results of the output operation in order, each line contains a number. Sample Input 2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2 Sample Output 2 1 Source POJ Monthly--2006.03.26,dodo   线段树 #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> #define N 100010 #define M 30 using namespace std; struct num { int l,r,sum; bool val[31]; }a[4*N]; char s1[5]; int n,m,t,res; bool status[31]; int main() { //freopen("data.in","r",stdin); void init(int k,int l,int r); void update(int k,int l,int r,int c); void find(int k,int l,int r); while(scanf("%d %d %d",&n,&m,&t)!=EOF) { init(1,1,n); a[1].sum=1; a[1].val[1]=true; while(t--) { scanf("%s",s1); int l,r,c; scanf("%d %d",&l,&r); if(l>r) { swap(l,r); } if(s1[0]=='C') { scanf("%d",&c); update(1,l,r,c); }else if(s1[0]=='P') { memset(status,false,sizeof(status)); res=0; find(1,l,r); printf("%d\n",res); } } } return 0; } void init(int k,int l,int r) { a[k].sum=0; for(int i=1;i<=m;i++) { a[k].val[i]=false; } a[k].l = l; a[k].r = r; if(l==r) { return ; } int mid=(l+r)/2; init(k*2,l,mid); init(k*2+1,mid+1,r); } void pushup(int k) { a[k].sum=0; for(int i=1;i<=m;i++) { a[k].val[i]=false; if(a[k*2].val[i]||a[k*2+1].val[i]) { a[k].val[i]=true; a[k].sum+=1; } } } void update(int k,int l,int r,int c) { if(a[k].l==l&&a[k].r==r) { for(int i=1;i<=m;i++) { a[k].val[i]=false; } a[k].val[c]=true; a[k].sum=1; return ; } if(a[k].sum==1) { for(int i=1;i<=m;i++) { a[k*2].val[i]=a[k].val[i]; a[k*2+1].val[i]=a[k].val[i]; a[k*2].sum=1; a[k*2+1].sum=1; } } int mid =(a[k].l+a[k].r)/2; if(mid>=r) { update(k*2,l,r,c); }else if(mid<l) { update(k*2+1,l,r,c); }else { update(k*2,l,mid,c); update(k*2+1,mid+1,r,c); } pushup(k); } void find(int k,int l,int r) { if(a[k].l<=l&&a[k].r>=r&&a[k].sum==1) { for(int i=1;i<=m;i++) { if(a[k].val[i]&&!status[i]) { res++; status[i]=true; } } return ; } if(a[k].l==l&&a[k].r==r) { for(int i=1;i<=m;i++) { if(a[k].val[i]&&!status[i]) { res++; status[i]=true; } } return ; } int mid = (a[k].r+a[k].l)/2; if(mid>=r) { find(k*2,l,r); }else if(mid<l) { find(k*2+1,l,r); }else { find(k*2,l,mid); find(k*2+1,mid+1,r); } }

上一篇:hdu 4274 Spy's Work
下一篇:java 从属性文件加载数据的方法工具类 实例 可直接使用