Count Color
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 36646 |
|
Accepted: 11053 |
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
Test instructions is to find a wall color type, because the color type is up to 30, you can use binary to represent the total number of colors.
#include <cstdio> #include <cmath> #include <queue> #include <iostream> #include <algorithm >using namespace std; #define LL __int64#define N 100005struct node{int l,r; int s,v,f; Color type two-level representation, interval color, whether it is necessary to update}f[n*3];void creat (int t,int l,int r) {f[t].l=l; F[t].r=r; F[t].v=1; f[t].f=0; F[t].s=1; The starting color is 1, which can be used in binary notation if (l==r) {return; } int tmp=t<<1,mid= (L+R) >>1; Creat (Tmp,l,mid); Creat (tmp|1,mid+1,r);} void update (int t,int l,int R,int v) {int tmp=t<<1,mid= (F[T].L+F[T].R) >>1; if (f[t].l==l&&f[t].r==r) {f[t].v=v; F[t].s= (1<<V); f[t].f=1; return; } if (F[T].F)//down update {f[tmp].f=f[tmp|1].f=1; F[TMP].V=F[TMP|1].V=F[T].V; F[tmp].s=f[tmp|1].s= (1<<F[T].V); f[t].f=0; } if (r<=mid) update (TMP,L,R,V); else if (l>mid) update (TMP|1,L,R,V); else {update (TMP,L,MID,V);Update (TMP|1,MID+1,R,V); } f[t].f=0; Sum f[t].s=f[tmp].s|f[tmp|1].s upward; }int query (int t,int l,int R) {if (f[t].l==l&&f[t].r==r) {return f[t].s; } int tmp=t<<1,mid= (F[T].L+F[T].R) >>1; if (F[T].F) {f[tmp].f=f[tmp|1].f=1; F[TMP].V=F[TMP|1].V=F[T].V; F[tmp].s=f[tmp|1].s= (1<<F[T].V); f[t].f=0; } if (R<=mid) return query (TMP,L,R); else if (l>mid) return query (TMP|1,L,R); else {return query (Tmp,l,mid) |query (tmp|1,mid+1,r); }}int Main () {int n,t,q,l,r,c; Char ch; while (~SCANF ("%d%d%d", &n,&t,&q)) {creat (1,1,n); while (q--) {GetChar (); scanf ("%c", &ch); if (ch== ' C ') {scanf ("%d%d%d", &l,&r,&c); if (l>r) swap (L,R); Update (1,L,R,C-1); } else { scanf ("%d%d", &l,&r); if (l>r) swap (L,R); int Tmp=query (1,L,R), ans=0; while (TMP) {if (tmp&1) ans++; tmp>>=1; } printf ("%d\n", ans); }}} return 0;}
POJ 2777 Count Color (segment update + interval summation)