2329: [HNOI2011]括号修复 - BZOJ
恶心的splay,打标记的时候还有冲突,要特别小心
上次写完了,查了半天没查出错来,于是放弃
今天对着标程打代码,终于抄完了,我已经不想再写了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 const 2 maxn=100002; 3 type 4 node=record 5 data,sum,lc,rc,re,size,lmin,lmax,rmin,rmax:longint; 6 sw,inv:boolean; 7 end; 8 9 var 10 f:array[0..maxn]of node; 11 ans,root,n,m,i,x,y:longint; 12 ch:char; 13 14 procedure swap(var x,y:longint); 15 var 16 t:longint; 17 begin 18 t:=x;x:=y;y:=t; 19 end; 20 21 function min(x,y:longint):longint; 22 begin 23 if x<y then exit(x); 24 exit(y); 25 end; 26 27 function max(x,y:longint):longint; 28 begin 29 if x>y then exit(x); 30 exit(y); 31 end; 32 33 procedure updata(x:longint); 34 begin 35 with f[x] do 36 begin 37 size:=f[lc].size+f[rc].size+1; 38 sum:=f[lc].sum+f[rc].sum+data; 39 lmax:=max(f[lc].lmax,f[lc].sum+data+f[rc].lmax); 40 lmin:=min(f[lc].lmin,f[lc].sum+data+f[rc].lmin); 41 rmax:=max(f[rc].rmax,f[rc].sum+data+f[lc].rmax); 42 rmin:=min(f[rc].rmin,f[rc].sum+data+f[lc].rmin); 43 end; 44 end; 45 46 procedure release(x:longint); 47 begin 48 if x=0 then exit; 49 with f[x] do 50 begin 51 if re<>0 then 52 begin 53 data:=re; 54 sum:=size*re; 55 lmin:=min(0,sum); 56 rmin:=lmin; 57 lmax:=max(0,sum); 58 rmax:=lmax; 59 f[lc].re:=re; 60 f[rc].re:=re; 61 f[lc].inv:=false; 62 f[rc].inv:=false; 63 re:=0; 64 end; 65 if sw then 66 begin 67 swap(lc,rc); 68 swap(lmax,rmax); 69 swap(lmin,rmin); 70 f[lc].sw:=not f[lc].sw; 71 f[rc].sw:=not f[rc].sw; 72 sw:=false; 73 end; 74 if inv then 75 begin 76 data:=-data; 77 sum:=-sum; 78 swap(lmin,lmax); 79 swap(rmin,rmax); 80 lmin:=-lmin; 81 lmax:=-lmax; 82 rmin:=-rmin; 83 rmax:=-rmax; 84 f[lc].inv:=not f[lc].inv; 85 f[rc].inv:=not f[rc].inv; 86 inv:=false; 87 end; 88 end; 89 end; 90 91 procedure splay(x:longint;var root:longint); 92 var 93 tmp:longint; 94 begin 95 release(root); 96 release(f[root].lc); 97 release(f[root].rc); 98 if x=f[f[root].lc].size+1 then exit; 99 if x<=f[f[root].lc].size then 100 begin 101 splay(x,f[root].lc); 102 tmp:=f[root].lc; 103 f[root].lc:=f[tmp].rc; 104 f[tmp].rc:=root; 105 updata(root); 106 root:=tmp; 107 end 108 else 109 begin 110 splay(x-f[f[root].lc].size-1,f[root].rc); 111 tmp:=f[root].rc; 112 f[root].rc:=f[tmp].lc; 113 f[tmp].lc:=root; 114 updata(root); 115 root:=tmp; 116 end; 117 end; 118 119 procedure splay(x:longint); 120 begin 121 splay(x,root); 122 updata(root); 123 end; 124 125 procedure build(l,r:longint;var now:longint); 126 var 127 mid:longint; 128 begin 129 if l>r then exit; 130 mid:=(l+r)>>1; 131 now:=mid; 132 build(l,mid-1,f[mid].lc); 133 build(mid+1,r,f[mid].rc); 134 updata(mid); 135 end; 136 137 begin 138 readln(n,m); 139 for i:=2 to n+1 do 140 begin 141 read(ch); 142 if ch='(' then f[i].data:=1 143 else f[i].data:=-1; 144 end; 145 build(1,n+2,root); 146 readln; 147 while m>0 do 148 begin 149 dec(m); 150 read(ch); 151 case ch of 152 'R':begin 153 readln(ch,ch,ch,ch,ch,ch,x,y,ch,ch); 154 splay(y+2); 155 splay(x); 156 if ch='(' then f[f[f[root].rc].lc].re:=1 157 else f[f[f[root].rc].lc].re:=-1; 158 release(f[f[root].rc].lc); 159 updata(f[root].rc); 160 updata(root); 161 end; 162 'S':begin 163 readln(ch,ch,ch,x,y); 164 splay(y+2); 165 splay(x); 166 f[f[f[root].rc].lc].sw:=true; 167 release(f[f[root].rc].lc); 168 updata(f[root].rc); 169 updata(root); 170 end; 171 'I':begin 172 readln(ch,ch,ch,ch,ch,x,y); 173 splay(y+2); 174 splay(x); 175 f[f[f[root].rc].lc].inv:=true; 176 release(f[f[root].rc].lc); 177 updata(f[root].rc); 178 updata(root); 179 end; 180 'Q':begin 181 readln(ch,ch,ch,ch,x,y); 182 splay(y+2); 183 splay(x); 184 writeln((1-f[f[f[root].rc].lc].lmin)>>1+(1+f[f[f[root].rc].lc].rmax)>>1); 185 end; 186 end; 187 end; 188 end.