Count Color
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
|
题意:
有一个长板子,多次操作,有两种操作,第一种是给从a到b那段染一种颜色c,另一种是询问a到b有多少种不同的颜色。
做了这题后,体会到了线段树的强大功能。线段树远比我想象的要强大。
思路:
由于颜色最多30种,所以用二进制表示。二进制第几位为1表示第几种颜色存在。通过线段树维护颜色信息。由于最后询问只关心颜色数目。所以通过按位与求和。然后统计其中一的个数就知道颜色的个数了。
详细见代码:
#include <iostream> #include<string.h> #include<stdio.h> using namespace std; const int maxn=100100; int L,T,O; struct node { int l,r; bool same;//记录区间颜色是否相同 int color; } smt[3*maxn]; void btree(int l,int r,int k) { int ls,rs,mid; smt[k].l=l; smt[k].r=r; smt[k].same=true; smt[k].color=1; if(smt[k].l==smt[k].r) return; ls=k<<1; rs=k<<1|1; mid=(smt[k].l+smt[k].r)>>1; btree(l,mid,ls); btree(mid+1,r,rs); } void update(int l,int r,int k,int c) { int ls,rs,mid; if(smt[k].l==l&&smt[k].r==r) { smt[k].same=true; smt[k].color=c; return; } ls=k<<1; rs=k<<1|1; mid=(smt[k].l+smt[k].r)>>1; if(smt[k].same) { smt[ls].same=smt[rs].same=true; smt[k].same=false; smt[ls].color=smt[rs].color=smt[k].color; } if(l>mid) update(l,r,rs,c); else if(r<=mid) update(l,r,ls,c); else { update(l,mid,ls,c); update(mid+1,r,rs,c); } smt[k].color=smt[ls].color|smt[rs].color;//二进制压缩颜色 } int qu(int l,int r,int k) { int ls,rs,mid; if((smt[k].l==l&&smt[k].r==r)||smt[k].same) return smt[k].color; ls=k<<1; rs=k<<1|1; mid=(smt[k].l+smt[k].r)>>1; if(l>mid) return qu(l,r,rs); else if(r<=mid) return qu(l,r,ls); else return qu(l,mid,ls)|qu(mid+1,r,rs); } int countc(int cor) { int cnt=0; while(cor) { if(cor&1) cnt++; cor>>=1; } return cnt; } int main() { char com[10]; int a,b,c; while(~scanf("%d%d%d",&L,&T,&O)) { gets(com); btree(1,L,1); while(O--) { scanf("%s",com); if(com[0]=='C') { scanf("%d%d%d",&a,&b,&c); update(a,b,1,1<<(c-1)); } else { scanf("%d%d",&a,&b); printf("%d\n",countc(qu(a,b,1))); } } } return 0; }