Count Color
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 37647 |
|
Accepted: 11315 |
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 |
Test instructions
There is a board, the initial color 1, there are two operations, c to the interval [l,r] are painted color val,p ask interval [l,r] Color of the number of species.
Ideas:
Segment tree interval update, maintain an interval of the number of colors, because there are only 30 colors, you can consider the bit operation, with an int to represent.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MAXN 100005#define Lson (rt<<1) #define Rson (rt<<1|1) #define INF 0x3f3f3f3ftypedef Long Long ll;using namespace St D;int N,m,tot;char s[10];int vis[maxn<<2],sum[maxn<<2];void pushdown (int le,int ri,int RT) {if (vis[rt]!=-1 ) {VIS[LSON]=VIS[RSON]=VIS[RT]; Sum[lson]=sum[rson]= (1<<vis[rt]); Vis[rt]=-1; }}void pushup (int le,int ri,int RT) {Sum[rt]=sum[lson]|sum[rson];} void update (int le,int ri,int rt,int u,int v,int val) {if (le==u&&ri==v) {vis[rt]=val; Sum[rt]= (1<<val); return; } pushdown (LE,RI,RT); int mid= (LE+RI) >>1; if (v<=mid) {update (le,mid,lson,u,v,val); } else if (u>=mid+1) {update (mid+1,ri,rson,u,v,val); } else {update (le,mid,lson,u,mid,val); Update (MID+1,RI,RSON,MID+1,V,VAL); } pushup (LE,RI,RT);}int query (int le,int ri,int rt,int U,int v) {if (vis[rt]!=-1) return (1<<vis[rt]); else if (le==u&&ri==v) {return SUM[RT]; } pushdown (LE,RI,RT); int res=0,mid= (LE+RI) >>1; if (v<=mid) {res=query (le,mid,lson,u,v); } else if (u>=mid+1) {res=query (mid+1,ri,rson,u,v); } else {res=query (le,mid,lson,u,mid); Res|=query (MID+1,RI,RSON,MID+1,V); } return res; int main () {while (~scanf ("%d%d%d", &n,&tot,&m)) {memset (vis,-1,sizeof (VIS)); memset (sum,0,sizeof (sum)); Update (1,n,1,1,n,0); while (m--) {scanf ("%s", s); int u,v,val; if (s[0]== ' C ') {scanf ("%d%d%d", &u,&v,&val); if (u>v) swap (U,V); Update (1,N,1,U,V,VAL-1); } else {if (u>v) swap (U,V); scanf ("%d%d", &u,&v); int X=query (1,N,1,U,V), ans=0; while (x) {if (x&1) ans++; x>>=1; } printf ("%d\n", ans); }}} return 0;} /*2 2 4C 1 1 2P 1 2C 2 2 2P 1 2*/
POJ 2777 Count Color (segment tree interval update)