Bzoj 2209: [Jsoi2011] bracket sequence [splay brackets]

Source: Internet
Author: User

2209: [Jsoi2011] Bracket sequence time limit:20 Sec Memory limit:259 MB
submit:1111 solved:541
[Submit] [Status] [Discuss] The first line of the Descriptioninput input data contains two integers n and q, each representing the length of the bracket sequence and the number of operations. The second line contains a sequence of parentheses with a length of N. Next Q line, each line of three integers t, x and Y, respectively, indicating the type of operation, the starting position of the operation and the end position of the operation, the input data guaranteed x is not less than Y. Where t=0 indicates an inquiry operation, T=1 represents a reversal operation, and t=2 represents a rollover operation. Output for each query operation, a line is shown that indicates the minimum number of changes required to modify the sequence of parentheses to match the subsequence. Sample Input6 3
)(())(
0 1 6
0 1 4
0 3 4Sample Output2
2
0
HINT

100% of data meet n,q not more than 10^5

Source

First round

Background: The key lock room in the computer can only come to the electronic reading room, in the cold humid air unexpectedly 1 A is too moved ( although not entirely self-thought out )

Parentheses question a common technique is (-1) +1

The last to be modified)) (the number of ((this way)) is the minimum prefix and ((

So the number of changes is (lmn+1)/2+ (rmx+1)/2

Information merging and dynamic maximal sub-sequences the problem is very similar.

How is information maintained after flipping and reversing?

Flip: Just swap L. and R.. Just fine.

Invert: all. mx. MN must be negative, then swap (LMX,LMN) swap (RMX,RMN)

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;typedefLong Longll;#defineLC T[x].ch[0]#defineRC T[x].ch[1]#definePA T[X].FAConst intn=1e5+5, inf=1e9;inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intN,q,l,r,a[n];Chars[n],op[ -];structnode{intch[2],FA,SIZE,V,SUM,LMX,LMN,RMX,RMN,REV,FLP; Node (): FA (0) {ch[0]=ch[1]=0;}} T[n];intRoot;inlineintWhintx) {returnt[pa].ch[1]==x;} InlinevoidUpdateintx) {    if(!x)return; T[x].size=t[lc].size+t[rc].size+1; T[x].sum=t[lc].sum+t[rc].sum+t[x].v; T[X].LMX=max (T[lc].lmx,max (t[lc].sum+t[x].v,t[lc].sum+t[x].v+t[rc].lmx)); T[X].LMN=min (T[lc].lmn,min (t[lc].sum+t[x].v,t[lc].sum+t[x].v+t[rc].lmn)); T[x].rmx=max (T[rc].rmx,max (t[rc].sum+t[x].v,t[rc].sum+t[x].v+t[lc].rmx)); T[X].RMN=min (T[rc].rmn,min (t[rc].sum+t[x].v,t[rc].sum+t[x].v+t[lc].rmn)); }inlinevoidRever (intx) {T[x].rev^=1;    Swap (LC,RC);    Swap (T[X].LMX,T[X].RMX); Swap (T[X].LMN,T[X].RMN);} InlinevoidFlipintx) {T[X].FLP^=1; T[x].sum=-t[x].sum;t[x].v=-t[x].v; T[X].LMX=-t[x].lmx;t[x].lmn=-T[X].LMN;    Swap (T[X].LMX,T[X].LMN); T[x].rmx=-t[x].rmx;t[x].rmn=-T[X].RMN; Swap (T[X].RMX,T[X].RMN);} InlinevoidPushdown (intx) {    if(T[x].rev) {if(LC) Rever (LC); if(RC) rever (RC); T[x].rev=0; }    if(T[X].FLP) {if(LC) Flip (LC); if(RC) flip (RC); T[X].FLP=0; }}inlinevoidRotateintx) {    intF=t[x].fa,g=t[f].fa,c=WH (x); if(g) T[g].ch[wh (f)]=x;t[x].fa=G; T[F].CH[C]=t[x].ch[c^1];t[t[f].ch[c]].fa=F; T[x].ch[c^1]=f;t[f].fa=x; Update (f); update (x);} InlinevoidSplay (intXinttar) {     for(;p a!=tar;rotate (x))if(T[pa].fa!=tar) Rotate (WH (x) ==WH (PA)?pa:x); if(tar==0) root=x;}intBuildintLintRintf) {//printf ("Build%d%d%d\n", l,r,f);    if(L>r)return 0; intx= (l+r) >>1; LC=build (l,x-1, x); Rc=build (x+1, r,x); T[X].FA=F; T[x].rev=t[x].flp=0;//printf ("Al%d\n", A[l]);T[X].V=A[X];//Not needUpdate (x);//printf ("Get%d%d%d%d%d%d%d%d %d\n", X,L,R,T[X].V,T[X].SUM,T[X].LMX,T[X].LMN,T[X].RMX,T[X].RMN);    returnx;} InlineintKthintK) {//printf ("kth%d\n", k);    intx=root,ls=0;  while(x) {pushdown (x); int_=ls+t[lc].size;//printf ("Size%d%d\n", x,_);        if(_<k&&k<=_+1)returnx; Else if(k<=_) x=LC; Elsels=_+1, x=RC; }    return 0;}voidQuery (intLintR) {//printf ("Query%d%d\n", l,r);    intF=kth (l); Splay (F,0); intX=kth (r+2); splay (x,f); intA=T[LC].LMX,B=T[LC].RMN;//printf ("Hi%d%d%d%d%d\n", f,x,lc,a,b);printf"%d\n", (A +1)/2+ (-b+1)/2);}voidFlip (intLintR) {    intF=kth (l); Splay (F,0); intX=kth (r+2); splay (x,f); Flip (LC); update (x); update (f);}voidRever (intLintR) {    intF=kth (l); Splay (F,0); intX=kth (r+2); splay (x,f); Rever (LC); update (x); update (f);}intMain () {//freopen ("In.txt", "R", stdin);N=read (); q=read (); scanf ("%s", s+1);  for(intI=1; i<=n;i++) a[i+1]=s[i]=='('?-1:1; //for (int i=1;i<=n+2;i++) printf ("%d", A[i]);p UTS ("");t[0].lmn=t[0].rmn=INF; t[0].lmx=t[0].rmx=-INF; Root=build (1, n+2, Root);  while(q--) {scanf ("%s", op); L=read (); r=read (); if(op==0) Query (l,r); if(op==1) Flip (l,r); if(op==2) Rever (l,r); }}

Bzoj 2209: [Jsoi2011] bracket sequence [splay brackets]

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.