讨论 / wish进,此题为何我只有60分
ljm491 2008-11-09 05:25:00
点我顶贴 收藏 删除
program rq91;

const maxn=500000;

var

count:array[1..maxn*4] of longint;

lc,rc:array[1..maxn*4] of integer;

i,j,k,n,st,rotate,qn,c,color,pp:longint;

s,flag:string;

procedure ins(p,tl,tr,l,r:longint);

var m:longint;

begin

if (tl=l)and(tr=r) then

begin

count[p]:=1;

lc[p]:=color;

rc[p]:=color;

exit;

end;

if count[p]=1 then

begin

count[p*2]:=1;count[p*2+1]:=1;

lc[p*2]:=lc[p];rc[p*2]:=lc[p];

lc[p*2+1]:=lc[p];rc[p*2+1]:=lc[p];

end;

m:=(tl+tr) div 2;

if l<=m then

begin

if r<=m then ins(p*2,tl,m,l,r) else

begin

ins(p*2,tl,m,l,m);

ins(p*2+1,m+1,tr,m+1,r);

end;

end else ins(p*2+1,m+1,tr,l,r);

lc[p]:=lc[p*2];

rc[p]:=rc[p*2+1];

count[p]:=count[p*2]+count[p*2+1];

if (rc[p*2]=lc[p*2+1])and(count[p]>1) then dec(count[p]);

end;

function ask(p,tl,tr,l,r:longint):longint;

var m:longint;

begin

if ((tl=l)and(tr=r))or(count[p]=1) then exit(count[p]);

m:=(tl+tr) div 2;

if l<=m then

begin

if r<=m then ask:=ask(p*2,tl,m,l,r) else

begin

ask:=ask(p*2,tl,m,l,m)+ask(p*2+1,m+1,tr,m+1,r);

if rc[p*2]=lc[p*2+1] then dec(ask);

end;

end else ask:=ask(p*2+1,m+1,tr,l,r);

end;

procedure find_i_j(var i,j:longint);

var t:longint;

begin

if rotate=0 then

begin

i:=(n-st+i) mod n;

if i=0 then i:=n;

j:=(n-st+j) mod n;

if j=0 then j:=n;

end

else

begin

i:=n-i+2;

i:=(n-st+i) mod n;

if i=0 then i:=n;

j:=n-j+2;

j:=(n-st+j) mod n;

if j=0 then j:=n;

t:=i;i:=j;j:=t;

end;

end;

function find(p,tl,tr,d:longint):longint;

begin

if count[p]=1 then exit(lc[p]);

if d<=(tl+tr) div 2 then find:=find(p*2,tl,(tl+tr) div 2,d) else

find:=find(p*2+1,(tl+tr) div 2+1,tr,d);

end;

procedure Print(k:longint);

begin

writeln(k);

end;

procedure Rotate_k;

var kk:longint;

begin

val(copy(s,1,pos(’ ’,s)-1),kk);

if rotate=0 then st:=st+kk else st:=st-kk;

while st<0 do st:=st+n;

st:=st mod n;

end;

procedure Flip;

begin

rotate:=1-rotate;

end;

procedure Swap_i_j;

var i,j,ic,jc:longint;

begin

val(copy(s,1,pos(’ ’,s)-1),i);

delete(s,1,pos(’ ’,s));

val(copy(s,1,pos(’ ’,s)-1),j);

find_i_j(i,j);

ic:=find(1,1,n,i);

jc:=find(1,1,n,j);

color:=ic;

ins(1,1,n,j,j);

color:=jc;

ins(1,1,n,i,i);

end;

procedure Paint_i_j_x;

var i,j:longint;

begin

val(copy(s,1,pos(’ ’,s)-1),i);

delete(s,1,pos(’ ’,s));

val(copy(s,1,pos(’ ’,s)-1),j);

delete(s,1,pos(’ ’,s));

val(copy(s,1,pos(’ ’,s)-1),color);

find_i_j(i,j);

if i<=j then ins(1,1,n,i,j) else

begin

ins(1,1,n,i,n);

ins(1,1,n,1,j);

end;

end;

procedure Print_Count;

begin

if count[1]=1 then Print(1) else

if lc[1]=rc[1] then Print(count[1]-1) else Print(count[1]);

end;

procedure Cs_i_j;

var i,j,t:longint;

begin

val(copy(s,1,pos(’ ’,s)-1),i);

delete(s,1,pos(’ ’,s));

val(copy(s,1,pos(’ ’,s)-1),j);

find_i_j(i,j);

if (i=1)and(j=n) then Print_Count else

if i<=j then Print(ask(1,1,n,i,j)) else

begin

t:=ask(1,1,n,i,n)+ask(1,1,n,1,j);

if lc[1]=rc[1] then dec(t);

Print(t);

end;

end;

begin

fillchar(count,sizeof(count),0);

fillchar(lc,sizeof(lc),0);

fillchar(rc,sizeof(rc),0);

readln(n,c);

for i:=1 to n do

begin

read(color);

ins(1,1,n,i,i);

end;

st:=0;

rotate:=0;

readln(qn);

for qn:=qn downto 1 do

begin

readln(s);

s:=s+’ ’;

flag:=copy(s,1,pos(’ ’,s)-1);

delete(s,1,pos(’ ’,s));

if flag=’R’ then Rotate_k;

if flag=’F’ then Flip;

if flag=’S’ then Swap_i_j;

if flag=’P’ then Paint_i_j_x;

if flag=’C’ then Print_Count;

if flag=’CS’ then Cs_i_j;

end;

end.

#1 OI帝国@2008-11-09 05:25:00
回复 删除
你还不如用模拟(模拟也60)
查看更多回复
提交回复