POJ 2777-Shaped pressure line segment tree

Source: Internet
Author: User

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 36586 Accepted: 11041

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


Topic Meaning:
There are n pieces of wood, each piece of wood initialization color 1,m operation: C L R Val is the section L to the R section of the board dyed full color val;p l r output the number of different colors in paragraph l to section R.

Ideas:
A color of up to 30, the pressure color is OK, this paragraph has the color, then this bit is 1, otherwise 0. Line tree Each node has a flag flag, true words can be updated downward, false words cannot be updated down------interval update old routines.


Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <vector>6#include <queue>7 using namespacestd;8 9 #defineN 100005Ten #definell Root<<1 One #defineRR root<<1|1 A #defineMid (A[ROOT].L+A[ROOT].R)/2 - intMaxintXintY) {returnX>y?x:y;} - intMinintXintY) {returnX<y?x:y;} the  - structnode{ -     intL, R, Val;//Val is the color state -     BOOLFlag; +}a[n*4]; -  + intn, color, m; A  at voidBuildintLintRintroot) { -A[root].l=l; -A[root].r=R; -A[root].val=2;//here is the wrong one, the initialization color is 1, then the state is 1<<1 2 -a[root].flag=false; -     if(L==R)return; in build (L,MID,LL); -Build (mid+1, R,RR); to } +  - voidChangeintLintRintValintroot) { the     if(a[root].l==l&&a[root].r==R) { *A[root].val=1<<Val; $a[root].flag=true;Panax Notoginseng         return; -     } the     if(A[root].flag) {//if True, and the segment of the query is in this paragraph, then the update is down and flag becomes false +a[root].flag=false; A         if(a[root].l!=A[ROOT].R) { theA[ll].val=a[rr].val=A[root].val; +a[ll].flag=a[rr].flag=true; -         } $     } $     if(l>mid) Change (L,R,VAL,RR); -     Else if(r<=mid) Change (L,R,VAL,LL); -     Else{ the Change (L,MID,VAL,LL); -Change (mid+1, R,VAL,RR);Wuyi     } theA[root].val=a[ll].val|a[rr].val;// up - } Wu  - intFindintLintRintroot) { About     if(A[ROOT].L==L&AMP;&AMP;A[ROOT].R==R)returnA[root].val; $     if(a[root].flag) { -a[root].flag=false; -         if(a[root].l!=A[ROOT].R) { -A[ll].val=a[rr].val=A[root].val; Aa[ll].flag=a[rr].flag=true; +         } the     } -     if(L>mid)returnfind (L,R,RR); $     Else if(R<=mid)returnfind (L,R,LL); the     Else{ the         returnFind (L,MID,LL) |find (mid+1, R,RR); the     } thea[root].val=a[ll].val|A[rr].val; - } in  the Main () the { About     intI, J, K; the     Chars[Ten]; the     intx, y, Z; the      while(SCANF (" %d%d%d", &n,&color,&m) = =3){ +Build1N1); -          while(m--){ thescanf"%s", s);Bayi             if(strcmp (s),"C")==0){ thescanf" %d%d%d",&x,&y,&z); theChange (min (x, y), Max (x, y), Z,1); -             } -             Else if(strcmp (s),"P")==0){ thescanf"%d%d",&x,&y); the                 intNum=find (min (x, y), Max (x, y),1), ans=0; the                  the                  for(i=0; i<=color;i++) { -                     if((1<<i) &num) ans++;  the                 } theprintf"%d\n", ans); the             }94         } the          the     } the}

POJ 2777-Shaped pressure line segment tree

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.