"Portal: BZOJ2209" Brief test Instructions:
Gives a sequence of parentheses of length n, with Q operations, 3 operations:
0 L R to find the minimum number of steps, each step can be changed by one parenthesis, the L-R bracket sequence into one by one pairs (that is, the left parenthesis on the right there is always a closing parenthesis corresponding to it)
1 L R turns the opening parenthesis in L to r into a closing parenthesis, and the closing parenthesis into an opening parenthesis
2 L R flips the sequence of L to R
The following:
Splay
For a sequence of parentheses, the opening parenthesis is treated as 1 and the closing parenthesis as 1
The minimum number of steps is in a sequence: (Minimum prefix and absolute value + 1)/2+ (maximum suffix and +1)/2
The minimum and maximum suffixes are maintained and the minimum and maximum prefixes are maintained, and (for ease of modification)
Then because the two change operation does not affect each other, so the order of the flag is arbitrarily
Because of the small details, adjusted the afternoon and the night, the heart tired
Reference Code:
#include <cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;structnode{intson[2],f,c,s,d; intLn,rn,lm,rm; BOOLFz,zf;} tr[110000];intLen,root;voidFanzhuan (intx) {TR[X].FZ=false; Swap (tr[x].son[0],tr[x].son[1]); intlc=tr[x].son[0],rc=tr[x].son[1]; if(lc!=0) {swap (TR[LC].LN,TR[LC].RN); Swap (TR[LC].LM,TR[LC].RM); TR[LC].FZ^=1; } if(rc!=0) {swap (TR[RC].LN,TR[RC].RN); Swap (TR[RC].LM,TR[RC].RM); TR[RC].FZ^=1; }}voidZhuanfan (intx) {Tr[x].zf=false; intlc=tr[x].son[0],rc=tr[x].son[1]; if(lc!=0) {Tr[lc].ln=-tr[lc].ln;tr[lc].lm=-TR[LC].LM; Swap (TR[LC].LN,TR[LC].LM); Tr[lc].rn=-tr[lc].rn;tr[lc].rm=-tr[lc].rm; Swap (TR[LC].RN,TR[LC].RM); Tr[lc].s=-tr[lc].s;tr[lc].d=-TR[LC].D; Tr[lc].zf^=1; } if(rc!=0) {Tr[rc].ln=-tr[rc].ln;tr[rc].lm=-TR[RC].LM; Swap (TR[RC].LN,TR[RC].LM); Tr[rc].rn=-tr[rc].rn;tr[rc].rm=-tr[rc].rm; Swap (TR[RC].RN,TR[RC].RM); Tr[rc].s=-tr[rc].s;tr[rc].d=-TR[RC].D; Tr[rc].zf^=1; }}voidUpdateintx) { intlc=tr[x].son[0],rc=tr[x].son[1]; TR[X].C=tr[lc].c+tr[rc].c+1; Tr[x].s=tr[lc].s+tr[rc].s+TR[X].D; Tr[x].ln=min (tr[lc].s+tr[rc].ln+tr[x].d,tr[lc].ln); TR[X].LM=max (tr[lc].s+tr[rc].lm+tr[x].d,tr[lc].lm); Tr[x].rn=min (tr[lc].rn+tr[rc].s+tr[x].d,tr[rc].rn); Tr[x].rm=max (tr[lc].rm+tr[rc].s+tr[x].d,tr[rc].rm);}inta[110000];intBtintLintR) { if(L>r)return 0; intNow=++len;intMid= (L+R)/2; TR[NOW].D=A[mid]; TR[NOW].FZ=tr[now].zf=false; tr[now].son[0]=BT (l,mid-1); tr[now].son[1]=BT (mid+1, R); intlc=tr[now].son[0],rc=tr[now].son[1]; if(lc!=0) tr[lc].f=Now ; if(rc!=0) tr[rc].f=Now ; Update (now); returnNow ;}voidRotateintXintW) { intf=tr[x].f,ff=tr[f].f; intR,r; R=TR[X].SON[W]; R=F; tr[r].son[1-w]=R; if(r!=0) tr[r].f=R; R=x; R=FF; if(tr[r].son[0]==F) tr[r].son[0]=R; Elsetr[r].son[1]=R; TR[R].F=R; R=f; R=x; TR[R].SON[W]=R; TR[R].F=s; Update (f); Update (x);}inttmp[110000],s;voidSplay (intXintRT) { inti=x;s=0; while(tr[i].f!=RT) {tmp[++s]=i; I=tr[i].f; } tmp[++s]=i; while(s!=0) {i=tmp[s--]; if(tr[i].fz==true) Fanzhuan (i); if(tr[i].zf==true) Zhuanfan (i); } while(tr[x].f!=RT) { intf=tr[x].f,ff=tr[f].f; if(ff==RT) { if(tr[f].son[0]==x) rotate (x,1); ElseRotate (x,0); } Else { if(tr[f].son[0]==x&&tr[ff].son[0]==f) {rotate (f),1); Rotate (x,1);Continue;} if(tr[f].son[1]==x&&tr[ff].son[0]==f) {Rotate (x,0); Rotate (x,1);Continue;} if(tr[f].son[0]==x&&tr[ff].son[1]==f) {Rotate (x,1); Rotate (x,0);Continue;} if(tr[f].son[1]==x&&tr[ff].son[1]==f) {rotate (f),0); Rotate (x,0);Continue;} } } if(rt==0) root=x;}intFinddizhi (intk) { intx=Root; while(1) { if(tr[x].fz==true) Fanzhuan (x); if(tr[x].zf==true) Zhuanfan (x); intlc=tr[x].son[0],rc=tr[x].son[1]; if(K<=TR[LC].C) x=LC; Else if(k>tr[lc].c+1) {k-=tr[lc].c+1; x=RC;} Else Break; } returnx;}voidFindmin (intLintR) { intLc=finddizhi (l1), Rc=finddizhi (r+1); Splay (LC,0); splay (RC,LC); intx=tr[rc].son[0]; printf ("%d\n", (ABS (TR[X].LN) +1)/2+ (tr[x].rm+1)/2);}voidZhuanfan_ (intLintR) { intLc=finddizhi (l1); intRc=finddizhi (r+1); Splay (LC,0); splay (RC,LC); intx=tr[rc].son[0]; Tr[x].ln=-tr[x].ln;tr[x].lm=-TR[X].LM; Swap (TR[X].LN,TR[X].LM); Tr[x].rn=-tr[x].rn;tr[x].rm=-tr[x].rm; Swap (TR[X].RN,TR[X].RM); Tr[x].s=-tr[x].s;tr[x].d=-TR[X].D; Tr[x].zf^=1; Splay (x,0);}voidFanzhuan_ (intLintR) { intLc=finddizhi (l1), Rc=finddizhi (r+1); Splay (LC,0); splay (RC,LC); intx=tr[rc].son[0]; Swap (TR[X].LN,TR[X].RN); Swap (TR[X].LM,TR[X].RM); TR[X].FZ^=1; Splay (x,0);}Charst[110000];intMain () {intn,q; scanf ("%d%d",&n,&q); scanf ("%s", st+1); for(intI=1; i<=n;i++) { if(st[i]=='(') a[i+1]=1; Elsea[i+1]=-1; } Len=0; ROOT=BT (1, n+2); for(intI=1; i<=q;i++) { intT,l,r; scanf ("%d%d%d", &t,&l,&r); l++;r++; if(t==0) findmin (l,r); if(t==1) Zhuanfan_ (l,r); if(t==2) Fanzhuan_ (l,r); } return 0;}
BZOJ2209: [Jsoi2011] Bracket sequence