Count color
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:16872 |
|
Accepted:4839 |
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 2 P 1 2C 2 2 2 P 1 2
Sample output
21
Source
Poj monthly -- 2006.03.26, Dodo still uses the lazy idea here (see the previous article ).
Type JI = ^ REC; Rec = record color, L, R: longint; lson, rson: JI; end; var V: array [-1 .. 30] of Boolean; ans, I, J, K, M, N, T, S, sum: longint; A: JI; Ch: Char; Procedure build (VAR: JI; L, R: longint); var mid: longint; begin new (a); a ^. l: = L; a ^. r: = r; if r> L + 1 then begin mid: = (L + r)> 1; build (a ^. lson, L, mid); Build (a ^. rson, mid, R); End else begin a ^. lson: = nil; a ^. rson: = nil; end; procedure down (var a: JI); begin if a ^. color> 0 then begin if a ^. lson <> nil then a ^. lson ^. color: = a ^. color; if a ^. rson <> nil then a ^. rson ^. color: = a ^. color; a ^. color: =-1; end; procedure insert (var a: JI; L, R, S: longint); var mid: longint; begin if (L <= a ^. l) and (A ^. r <= r) then a ^. color: = s else begin down (a); Mid: = (a ^. L + A ^. r)> 1; if l <mid then insert (a ^. lson, L, R, S); If R> mid then insert (a ^. rson, L, R, S); end; Procedure count (var a: JI; L, R: longint); var mid: longint; begin V [A ^. color]: = true; if a ^. color <0 then begin mid: = (a ^. L + A ^. r)> 1; if l <mid then count (a ^. lson, L, R); If R> mid then count (a ^. rson, L, R); end; begin readln (n, sum, m); Build (A, 0, n); a ^. color: = 1; for I: = 1 to M do begin read (CH, S, T); If CH = 'C' then begin read (k); insert (, s-1, T, k); End else begin ans: = 0; fillchar (v, sizeof (V), false); count (A, S-1, T); For J: = 1 to sum do if V [J] Then Inc (ANS); writeln (ANS); end; readln; end.