Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16872 | Accepted: 4839 |
Description
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
Output
Sample Input
2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2
Sample Output
2 1
Source
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 a: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; 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; 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; 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; 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(a,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; end.