POJ2777 (line tree coloring problem)

Source: Internet
Author: User

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 42828 Accepted: 12973

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

Idea: Because there are only 30 colors, so you can use 2 binary color.
Note: l may be greater than r
#include <cstdio>#include<algorithm>using namespacestd;Const intmaxn=100005; typedefLong Longll;structnode{ll Color,lazy; intL,r;} A[MAXN*3];intn,m;ll Res;voidPushup (intRT) {A[rt].color=a[rt<<1].color | a[(rt<<1)|1].color;}voidBuildintRtintLintR) {A[RT].L=l; A[RT].R=R; A[rt].lazy=0; if(l==r) {A[rt].lazy=1; A[rt].color=1; return ; }    intMid= (l+r) >>1; Build (Rt<<1, L,mid); Build (Rt<<1)|1, mid+1, R); Pushup (RT);}voidPushdown (intRT) {    if(a[rt].lazy!=0) {A[rt<<1].lazy=a[rt<<1].color=A[rt].color; a[(Rt<<1)|1].lazy=a[(rt<<1)|1].color=A[rt].color; A[rt].lazy=0; }}voidUpdateintRtintLintRintval) {    if(a[rt].l==l&&a[rt].r==R) {ll e=1; E<<= (val-1); A[rt].color=e; A[rt].lazy=e; return ;    } pushdown (RT); intMid= (A[RT].L+A[RT].R) >>1; if(r<=mid) {update (RT<<1, L,r,val); }    Else if(mid<l) {update (RT<<1)|1, L,r,val); }    Else{update (RT<<1, L,mid,val); Update (RT<<1)|1, mid+1, R,val); } pushup (RT);}voidQueryintRtintLintR) {    if(a[rt].l==l&&a[rt].r==r) {res=res|A[rt].color; return ;    } pushdown (RT); intMid= (A[RT].L+A[RT].R) >>1; if(r<=mid) {query (RT<<1, L,r); }    Else if(mid<l) {query (RT<<1)|1, L,r); }    Else{query (RT<<1, L,mid); Query (RT<<1)|1, mid+1, R); }}intMain () { while(SCANF ("%d%*d%d", &n,&m)! =EOF) {Build (1,1, N);  for(intI=0; i<m;i++) {scanf ("%*c"); Charop; intL,r; scanf ("%c%d%d",&op,&l,&R); if(l>r) Swap (L,R); if(op=='C')            {                intVal; scanf ("%d",&val); Update (1, L,r,val); }                Else{res=0; Query (1, L,r); intCnt=0;  while(res>0)                {                    if(res&1) cnt++; Res>>=1; } printf ("%d\n", CNT); }        }    }    return 0;}

POJ2777 (line tree coloring problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.