POJ 2777(涂点问线)

内容目录

线段树……

Program p2777;
const
   maxl=100000;
   maxt=30;
   maxo=100000;
var
   l,t,o,i,j,k:longint;
   col:array[1..maxl*10] of longint;
   c:char;
   x,y,color:longint;
   visit:array[1..maxt] of boolean;
procedure build(l,r,root:longint);
var
   mid:longint;
begin
   if l=r then
   begin
      col[root]:=1;
      exit;
   end;
   mid:=(l+r) shr 1;
   build(l,mid,root shl  1);
   build(mid+1,r,root shl 1+1);
   col[root]:=1;
end;
procedure swap(var a,b:longint);
var
   p:longint;
begin
   p:=a;a:=b;b:=p;
end;
procedure detate(l,r,sonl,sonr,root,color:longint);
var
   i,j,mid:longint;
begin
   mid:=(l+r) shr 1;
   if l=r then
   begin
      col[root]:=color;
      exit;
   end;

   if (sonl<=l) and (r<=sonr) then
   begin
      col[root]:=color;
      exit;
   end;

   if col[root]>0 then
   begin
      col[root*2]:=col[root];
      col[root*2+1]:=col[root];
      col[root]:=-1;


   end;

   if not((mid<sonl) or (sonr<l)) then detate(l,mid,sonl,sonr,root shl 1,color);
   if not((r<sonl) or (sonr<mid+1)) then detate(mid+1,r,sonl,sonr,root shl 1+1,color);



end;
function quere(l,r,sonl,sonr,root:longint):longint;
var
   mid,i,j:longint;
begin
   quere:=0;
   mid:=(l+r) shr 1;

   if col[root]>0 then
   begin
      if not(visit[col[root]]) then
      begin
         visit[col[root]]:=true;
         exit(1);
      end;
      exit(0);
   end;


   if not((mid<sonl) or (sonr<l)) then inc(quere,quere(l,mid,sonl,sonr,root shl 1));
   if not((r<sonl) or (sonr<mid+1)) then inc(quere,quere(mid+1,r,sonl,sonr,root shl 1+1));


end;

begin
   readln(l,t,o);
   build(1,l,1);
   for i:=1 to o do
   begin
      read(c);
      if c='C' then
      begin
         readln(x,y,color);
         if x>y then swap(x,y);
         detate(1,l,x,y,1,color);
      end
      else
      begin
         readln(x,y);
         if x>y then swap(x,y);
         fillchar(visit,sizeof(visit),false);
         writeln(quere(1,l,x,y,1));
      end;
   end;
end.