[BZOJ2209] [JSOI2011] Bracket sequence (splay)

Source: Internet
Author: User

One conclusion: For a sequence of parentheses, Jiancheng)) (the number of (after this form, set) is X, (the number is Y, then the answer is $\lceil \frac{x}{2} \rceil + \lceil \frac{y}{2} \rceil$.

Consider the x and y how to ask, think (as 1,) as-1, required is the minimum prefix and with the maximum suffix and.

Record LMN,LMX,RMN,RMX, reverse the exchange of each other, flip the mark, a variety of operations splay maintenance can be.

1#include <cstdio>2#include <algorithm>3 #defineRep (i,l,r) for (int i=l; i<=r; i++)4 using namespacestd;5 6 Const intn=100010;7 intn,m,op,rt,l,r,f[n],sz[n],v[n],sm[n],ch[n][2],lmn[n],rmn[n],lmx[n],rmx[n],rev[n],ops[n];8 CharC;9 Ten voidRev (intx) {rev[x]^=1; Swap (ch[x][0],ch[x][1]); Swap (lmn[x],rmn[x]); Swap (lmx[x],rmx[x]); } One voidOps (intx) { Aops[x]^=1; SM[X]=-SM[X]; v[x]=-V[x]; - swap (lmn[x],lmx[x]); swap (rmn[x],rmx[x]); -LMN[X]=-LMN[X]; RMN[X]=-RMN[X]; LMX[X]=-LMX[X]; rmx[x]=-Rmx[x]; the } -  - voidPushintx) { -     intl=ch[x][0],r=ch[x][1]; +     if(Rev[x]) Rev (L), Rev (R), rev[x]=0; -     if(Ops[x]) OPS (L), OPS (R), ops[x]=0; + } A  at voidUpdintx) { -     intl=ch[x][0],r=ch[x][1]; -sz[x]=sz[l]+sz[r]+1; sm[x]=sm[l]+sm[r]+V[x]; -Lmn[x]=min (lmn[l],sm[l]+v[x]+lmn[r]); -Lmx[x]=max (lmx[l],sm[l]+v[x]+lmx[r]); -Rmn[x]=min (rmn[r],sm[r]+v[x]+rmn[l]); inRmx[x]=max (rmx[r],sm[r]+v[x]+rmx[l]); - } to  + voidRotint&rt,intx) { -     inty=f[x],z=f[y],w=ch[y][1]==x; the     if(Y==RT) rt=x;Elsech[z][ch[z][1]==y]=x; *F[x]=z; F[y]=x; f[ch[x][w^1]]=y; $ch[y][w]=ch[x][w^1]; ch[x][w^1]=y; upd (y);Panax Notoginseng } -  the voidSplay (int&rt,intx) { +      while(x!=RT) { A         inty=f[x],z=F[y]; the         if(Y!=RT) Rot (RT, (ch[z][1]==y) ^ (ch[y][1]==x))?x:y); + rot (rt,x); -     } $ upd (x); $ } -  - intFindintXintk) { thepush (x);intl=ch[x][0],r=ch[x][1]; -     if(k==sz[l]+1)returnx;Wuyi     if(K<=sz[l])returnFind (L,K);Else returnFind (r,k-sz[l]-1); the } -  Wu intBuildintLintR) { -     if(L>r)return 0; About     if(l==R) { $SM[L]=V[L]; sz[l]=1; -         if(v[l]==-1) lmn[l]=rmn[l]=-1; -         if(v[l]==1) lmx[l]=rmx[l]=1; -         returnL; A     } +     intMid= (l+r) >>1; thef[ch[mid][0]=build (l,mid-1)]=mid; -f[ch[mid][1]=build (mid+1, R)]=mid; $UPD (mid);returnmid; the } the  the intSplitintLintR) { the     intX=find (rt,l), Y=find (rt,r+2); -Splay (RT,X); Splay (ch[x][1],y);returnch[y][0]; in } the  the intMain () { AboutFreopen ("bzoj2209.in","R", stdin); theFreopen ("Bzoj2209.out","W", stdout); thescanf"%d%d",&n,&m); theRep (I,2, n+1) scanf ("%c", &c), v[i]= (c=='(') ?1: -1; +Rt=build (1, n+2); -Rep (I,1, M) { thescanf"%d%d%d", &op,&l,&r);intx=split (l,r);Bayi         if(op==0) printf ("%d\n", (rmx[x]+1)/2-(lmn[x]-1)/2); the             Else if(op==1) Ops (x);ElseRev (x); the     } -     return 0; -}

[BZOJ2209] [JSOI2011] Bracket sequence (splay)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.