Poj2777Count Color (line segment tree)

Source: Internet
Author: User

Poj2777Count Color (line segment tree)
Count Color

Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:40404   Accepted:12188

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2 ,... L from left to right, each is 1 centimeter long. now we have to color the board-one segment with only one color. we can do following two 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 have very few words to describe a color (red, green, blue, yellow ...), So you may assume that the total 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 was painted in color 1. now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) 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 are integers, and A may be larger than B) as an operation defined previusly.

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

21

Source

POJ Monthly -- 2006.03.26, dodo
A long (L) canvas with T color and O operations. Each operation changes a color range or queries the number of colors contained in a range. Analysis: This question is very similar to hud1698, so the query is different. For details, refer to the code and HDU1698.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include using namespace std; const double eps = 1e-6; const double pi = acos (-1.0); const int INF = 0x3f3f3f; const int MOD = 1000000007; # define ll long # define CL (a, B) memset (a, B, sizeof (a) # define MAXN 100010 struct node {int l, r, s ;} t [MAXN <2]; int L, O, T; int sum [33]; // you can use an array because there are not many colors; 1 indicates that the range has this color, 0 and vice versa void build (int l, int r, int I) {t [I]. l = l; t [I]. r = r; t [I]. s = 1; if (l = r) return; int mid = (L + r)> 1; build (l, mid, I <1); build (mid + 1, r, I <1 | 1 );} void update (int l, int r, int m, int I) {if (t [I]. s = m) return; if (t [I]. l = l & t [I]. r = r) {t [I]. s = m; return;} if (t [I]. s! =-1) {t [I <1]. s = t [I <1 | 1]. s = t [I]. s; t [I]. s =-1;} int mid = (t [I]. l + t [I]. r)> 1; if (l> mid) update (l, r, m, I <1 | 1); else if (r <= mid) update (l, r, m, I <1); else {update (l, mid, m, I <1); update (mid + 1, r, m, I <1 | 1) ;}} void query (int l, int r, int I) {if (t [I]. s! =-1) // If the solid color marks this color {sum [t [I]. s] = 1; return;} else // otherwise, query the subnode {int mid = (t [I]. l + t [I]. r)> 1; if (l> mid) query (l, r, I <1 | 1); else if (r <= mid) query (l, r, I <1); else {query (l, mid, I <1); query (mid + 1, r, I <1 | 1 );}}} int main () {char ch; int a, B, c; while (scanf ("% d", & L, & T, & O) = 3) {build (1, L, 1); while (O --) {getchar (); scanf ("% c", & ch ); if (ch = 'C') {scanf ("% d", & a, & B, & C); update (a, B, c, 1);} int ans = 0; if (ch = 'P') {CL (sum, 0 ); // scanf ("% d", & a, & B); query (a, B, 1); for (int I = 1; I <= T; I ++) // counts the number of colors that have occurred. if (sum [I]) ans ++; printf ("% d \ n ", ans) ;}}return 0 ;}
         
        
       
     
    
   
  
 

Related Article

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.