您的位置:首页 > 其它

2329: [HNOI2011]括号修复 - BZOJ

2014-04-21 15:50 405 查看
const
maxn=100002;
type
node=record
data,sum,lc,rc,re,size,lmin,lmax,rmin,rmax:longint;
sw,inv:boolean;
end;

var
f:array[0..maxn]of node;
ans,root,n,m,i,x,y:longint;
ch:char;

procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end;

function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end;

function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end;

procedure updata(x:longint);
begin
with f[x] do
begin
size:=f[lc].size+f[rc].size+1;
sum:=f[lc].sum+f[rc].sum+data;
lmax:=max(f[lc].lmax,f[lc].sum+data+f[rc].lmax);
lmin:=min(f[lc].lmin,f[lc].sum+data+f[rc].lmin);
rmax:=max(f[rc].rmax,f[rc].sum+data+f[lc].rmax);
rmin:=min(f[rc].rmin,f[rc].sum+data+f[lc].rmin);
end;
end;

procedure release(x:longint);
begin
if x=0 then exit;
with f[x] do
begin
if re<>0 then
begin
data:=re;
sum:=size*re;
lmin:=min(0,sum);
rmin:=lmin;
lmax:=max(0,sum);
rmax:=lmax;
f[lc].re:=re;
f[rc].re:=re;
f[lc].inv:=false;
f[rc].inv:=false;
re:=0;
end;
if sw then
begin
swap(lc,rc);
swap(lmax,rmax);
swap(lmin,rmin);
f[lc].sw:=not f[lc].sw;
f[rc].sw:=not f[rc].sw;
sw:=false;
end;
if inv then
begin
data:=-data;
sum:=-sum;
swap(lmin,lmax);
swap(rmin,rmax);
lmin:=-lmin;
lmax:=-lmax;
rmin:=-rmin;
rmax:=-rmax;
f[lc].inv:=not f[lc].inv;
f[rc].inv:=not f[rc].inv;
inv:=false;
end;
end;
end;

procedure splay(x:longint;var root:longint);
var
tmp:longint;
begin
release(root);
release(f[root].lc);
release(f[root].rc);
if x=f[f[root].lc].size+1 then exit;
if x<=f[f[root].lc].size then
begin
splay(x,f[root].lc);
tmp:=f[root].lc;
f[root].lc:=f[tmp].rc;
f[tmp].rc:=root;
updata(root);
root:=tmp;
end
else
begin
splay(x-f[f[root].lc].size-1,f[root].rc);
tmp:=f[root].rc;
f[root].rc:=f[tmp].lc;
f[tmp].lc:=root;
updata(root);
root:=tmp;
end;
end;

procedure splay(x:longint);
begin
splay(x,root);
updata(root);
end;

procedure build(l,r:longint;var now:longint);
var
mid:longint;
begin
if l>r then exit;
mid:=(l+r)>>1;
now:=mid;
build(l,mid-1,f[mid].lc);
build(mid+1,r,f[mid].rc);
updata(mid);
end;

begin
readln(n,m);
for i:=2 to n+1 do
begin
read(ch);
if ch='(' then f[i].data:=1
else f[i].data:=-1;
end;
build(1,n+2,root);
readln;
while m>0 do
begin
dec(m);
read(ch);
case ch of
'R':begin
readln(ch,ch,ch,ch,ch,ch,x,y,ch,ch);
splay(y+2);
splay(x);
if ch='(' then f[f[f[root].rc].lc].re:=1
else f[f[f[root].rc].lc].re:=-1;
release(f[f[root].rc].lc);
updata(f[root].rc);
updata(root);
end;
'S':begin
readln(ch,ch,ch,x,y);
splay(y+2);
splay(x);
f[f[f[root].rc].lc].sw:=true;
release(f[f[root].rc].lc);
updata(f[root].rc);
updata(root);
end;
'I':begin
readln(ch,ch,ch,ch,ch,x,y);
splay(y+2);
splay(x);
f[f[f[root].rc].lc].inv:=true;
release(f[f[root].rc].lc);
updata(f[root].rc);
updata(root);
end;
'Q':begin
readln(ch,ch,ch,ch,x,y);
splay(y+2);
splay(x);
writeln((1-f[f[f[root].rc].lc].lmin)>>1+(1+f[f[f[root].rc].lc].rmax)>>1);
end;
end;
end;
end.


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: