Count Color
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 38921 |
|
Accepted: 11696 |
Description
Chosen Problem solving and program design as a optional course, you is required to solve all kinds of problems. Here, we get a new problem.
There is a very long board with length L centimeter, L was a positive integer, so we can evenly divide the board into L seg ments, and they is labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we has to color the Board-one segment with only one color. We can do following-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 had very few words to describe a color (red, green, blue, yellow ...), so if you could assume that the Tota L 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 is painted in color 1. Now the rest of problem are left to your.
Input
First line of input contains L (1 <= L <= 100000), T (1 <= t <=) 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 is integers, and A may is larger than B) as an Operat Ion defined previously.
Output
Ouput results of the output operation in order, each line contains a number.
Sample Input
2 2 4C 1 1 2P 1 2C 2 2 2P 1 2
Sample Output
21st
Source
POJ Monthly--2006.03.26,dodo
The main idea: to dye an interval and then output the number of colors for an interval.
The title of the segment tree. Because there are up to 30 colors, you can use an integer variable to store the color, each one representing a color. Color represents the number of colors, and add is used to defer updates. If add is 0, the update is not required. For the combination of two intervals, judging the number of colors, as long as the use or operation can be, so it is very convenient. Note that a may be less than B.
#include <stdio.h> #include <string.h> #define M 100005struct tree{int L,r,color,add;} Tree[m<<2];void pushup (int root) {if (TREE[ROOT].L==TREE[ROOT].R) return;tree[root].color=tree[root<<1] .color|tree[root<<1|1].color; The color type of the child node is updated to the parent node. return;} void pushdown (int root) {if (TREE[ROOT].L==TREE[ROOT].R) return; if (tree[root].add==0) Return;tree[root<<1].add =TREE[ROOT<<1|1].ADD=TREE[ROOT].ADD;TREE[ROOT<<1].COLOR=TREE[ROOT].COLOR;TREE[ROOT<<1|1]. Color=tree[root].color;tree[root].add=0;return;} void build (int l,int r,int root) {tree[root].l=l;tree[root].r=r;tree[root].color=1;tree[root].add=0;if (l==r) return; int Mid=l+r>>1;build (l,mid,root<<1); build (mid+1,r,root<<1|1);} void update (int l,int r,int z,int root) {if (tree[root].l==l&&tree[root].r==r) {tree[root].color=1<< (z-1 ); Tree[root].add=z;return;} Pushdown (root), int mid=tree[root].l+tree[root].r>>1;if (r<=mid) Update (l,r,z,root<<1), else if (l> MID) Update (l,r, z,root<<1|1), else {update (l,mid,z,root<<1); update (mid+1,r,z,root<<1|1);} Pushup (root);} int query (int l,int r,int root) {if (tree[root].l==l&&tree[root].r==r) {return tree[root].color;} Pushdown (root), int mid=tree[root].l+tree[root].r>>1;if (R<=MID) return query (l,r,root<<1), else if (l >MID) return query (l,r,root<<1|1), else {return query (l,mid,root<<1) |query (mid+1,r,root<<1|1); Color merging cleverly uses bit operations}}int cal (int x) {int ans=0;while (x) {ANS+=X%2;X=X/2;} return ans;} int main () {int L,t,o,i,j,k,a,b,c;char s[20];while (scanf ("%d%d%d", &l,&t,&o)!=eof) {build (1,l,1); while ( o--) {scanf ("%s%d%d", S,&a,&b), if (a>b) {int t=a;a=b;b=t;} if (s[0]== ' C ') {scanf ("%d", &c); update (a,b,c,1);} if (s[0]== ' P ') {int ans=query (a,b,1); ans=cal (ans);p rintf ("%d\n", ans);}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2777 Count Color (segment tree, State compression, bitwise operation)