现在的位置: 首页 > 综合 > 正文

poj 2777 ——线段树基础

2012年10月22日 ⁄ 综合 ⁄ 共 2389字 ⁄ 字号 评论关闭
Count Color
Time Limit: 1000MS Memory Limit: 65536K
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 previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

Source

这里依然采用lazy思想(参看上一篇)。
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.

抱歉!评论已关闭.