poj 2777 Count Color

题目大意

  给一个固定长度为L的画板

  有两个操作:

    C A B C:区间AB内涂上颜色C。

    P A B:查询区间AB内颜色种类数。


分析

  和zoj1610差不多,不解释。

  A B可以A>B

 

代码

  

type
  pnode=^tnode;
  tnode=record
    lc,rc:pnode;
    c:longint;
end;

var
  t:pnode;
  i,j,k:longint;
  x,y,x1,y1:longint;
  n,m,nm:longint;
  ans:longint;
  f:array[-2..10000] of int64;
  s:char;

procedure neww(var t:pnode);
begin
  if t=nil then
    begin
      new(t);
      t^.c:=1;
      t^.lc:=nil;
      t^.rc:=nil;
    end;
end;

procedure insert(var t:pnode; l,r,x,y,ce:longint);
var
  i,j,k:longint;
  mid:longint;

begin
  with t^ do
    begin
      if c<>ce then
        begin
          mid:=(l+r) div 2;
          if (l=x) and (r=y)
            then
              begin
                c:=ce;
                exit;
              end;
          if c<>-1
            then
              begin
                neww(lc);
                neww(rc);
                lc^.c:=t^.c;
                rc^.c:=t^.c;
                t^.c:=-1;
              end;
          if (l<=x) and (mid>=y)
            then
              begin
                neww(lc);
                insert(lc,l,mid,x,y,ce);
                exit;
              end;
          if (mid<x) and (r>=y)
            then
              begin
                neww(rc);
                insert(rc,mid+1,r,x,y,ce);
                exit;
              end;
          neww(lc);
          neww(rc);
          insert(lc,l,mid,x,mid,ce);
          insert(rc,mid+1,r,mid+1,y,ce);
        end;
    end;
end;

procedure find(t:pnode;l,r,x,y:longint);
var
  mid:longint;
begin
  neww(t);
  with t^ do
    begin
      mid:=(l+r) div 2;
      if c<>-1
        then
          begin
            f[c]:=1;
            exit;
          end;
      if (l<=x) and (mid>=y) and (l<>r)
        then
          begin
            find(lc,l,mid,x,y);
            exit;
          end;
      if (mid<x) and (r>=y) and (l<>r)
        then
          begin
            find(rc,mid+1,r,x,y);
            exit;
          end;
      if l<>r then begin
      find(lc,l,mid,x,mid);
      find(rc,mid+1,r,mid+1,y);
      end;
    end;
end;

begin
  while not eof do begin
  readln(m,nm,n);
  fillchar(t,sizeof(t),0);
  fillchar(f,sizeof(f),0);
  neww(t);
  for i:=1 to n do
    begin
      read(s,x,y);
      if s='P'
        then
          begin
            fillchar(f,sizeof(f),0);
            if x>y then
              begin
                x1:=x;
                x:=y;
                y:=x1;
              end;
            x1:=0; y1:=0;
            ans:=0;
            find(t,1,m,x,y);
            for x1:=1 to 30 do
              if f[x1]<>0 then inc(ans);
            writeln(ans);
            readln;
          end
        else
          begin
            readln(j);
            if x>y then
              begin
                x1:=x;
                x:=y;
                y:=x1;
              end;
            insert(t,1,m,x,y,j);
          end;
    end;
  end;
end.


posted @ 2016-05-25 17:22  一个响亮的蒟蒻  阅读(81)  评论(0编辑  收藏  举报