Chain of title:
http://www.lydsy.com/JudgeOnline/problem.php?id=2209
The
following:splayIt's a good question, but I'm sick of the retarded. first it's easy to see that there's no matching parentheses at the end))))
. ((((...that is, the left side is the right parenthesis (set the number of NR), the right is to do parentheses (set the number of NL)the answer is
⌈nl÷2⌉+⌈nr÷2⌉ (⌈⌉: Rounding up) If you consider ' (' as 1, ') ' as-1,so in this sequence that contains only 1 and-1,
the opposite number of the minimum prefix is equal to NR,
the suffix maximum is equal to NL
so for the
inquiry operation , record in splay:Key[x] (node x is 1 or-1)sum[x] (sum of x subtree corresponding to the interval)Pmn[x] (the minimum prefix value of the x subtree corresponding to the interval),Smx[x] (the maximum number of suffixes of the x subtree corresponding to the interval). for
A second reversal operation ,can be seen, just the corresponding interval of 1→-1,-1→1,so two more things to maintain.Pmx[x] (the maximum value of the prefix of the x subtree corresponding to the interval),smn[x] (the suffix of the x subtree corresponds to the minimum value). then take the key[x],sum[x],pmn[x],pmx[x],smn[x],smx[x of the record] all back (multiply by 1),and Swap (pmn[x],pmx[x]), swap (smn[x],smx[x]) (because it's reversed)just a lazy sign, please. for
Third rollover Operation,Just swap the left and right subtrees,and Swap (pmx[x],smx[x]), swap (pmn[x],smn[x]) (just reverse the sequence, so swap the prefix information)then make a lazy mark. because the lazy tag does not have a succession of influence, so lazy decentralized when you can put whichever first. Code:
#include <cstdio> #include <cstring> #include <iostream> #define MAXN 100500using namespace Std;int n,m ; struct Spt{int pmx[maxn],pmn[maxn],smx[maxn],smn[maxn],sum[maxn],key[maxn];int CH[MAXN][2],SIZ[MAXN],FA[MAXN], Lazy[maxn],rt;void Reverse (int x) {sum[x]*=-1; key[x]*=-1;pmx[x]*=-1; pmn[x]*=-1; swap (pmx[x],pmn[x]); smx[x]*=-1; SMN [X]*=-1; Swap (smx[x],smn[x]);} void Flip (int x) {swap (pmx[x],smx[x]), swap (pmn[x],smn[x]), swap (ch[x][0],ch[x][1]);} void pushup (int x) {siz[x]=siz[ch[x][0]]+1+siz[ch[x][1]];sum[x]=sum[ch[x][0]]+key[x]+sum[ch[x][1]];p Mx[x]=max (pmx [Ch[x][0]],sum[ch[x][0]]+key[x]+pmx[ch[x][1]]);p mn[x]=min (pmn[ch[x][0]],sum[ch[x][0]]+key[x]+pmn[ch[x][1]); Smx[x]=max (Smx[ch[x][1]],sum[ch[x][1]]+key[x]+smx[ch[x][0]); Smn[x]=min (SMN[CH[X][1]],SUM[CH[X][1]]+KEY[X]+SMN [Ch[x][0]]);} void pushdown (int x) {if (lazy[x]&1) {Reverse (ch[x][0]); lazy[ch[x][0]]^=1; Reverse (ch[x][1]); Lazy[ch[x][1]]^=1;lazy[x]^=1;} if (lazy[x]&2) {Flip (ch[x][0]); lazy[ch[x][0]]^=2; Flip (ch[x][1]); Lazy[ch[x][1]]^=2;lazy[x]^=2;}} void Rotate (int x,int &k) {static int y,z,l,r;y=fa[x]; z=fa[y];l=ch[y][0]!=x; r=l^1;if (!z) K=x;else ch[z][ch[z][0]!= Y]=x;fa[ch[x][r]]=y; Fa[y]=x; FA[X]=Z;CH[Y][L]=CH[X][R]; Ch[x][r]=y; Pushup (y);} void splay (int x,int &k) {static int y,z;while (X!=K) {y=fa[x]; Z=fa[y];if (y!=k) (ch[z][0]!=y) ^ (ch[y][0]!=x)? Rotate (x,k): Rotate (y,k); Rotate (x,k);} Pushup (x);} int find (int x,int num) {if (lazy[x]) pushdown (x), if (Num<=siz[ch[x][0]]) return find (ch[x][0],num); else if (num==siz[ CH[X][0]]+1) return X;else return find (ch[x][1],num-siz[ch[x][0]]-1);} int Split (int l,int r) {static int dl,dr;dl=find (RT,L); Dr=find (rt,r+2); Splay (DL,RT); Splay (dr,ch[dl][1]); return ch[dr][0];} void Modify (int l,int r,int type) {static int p;p=split (L,R), if (type==1) Reverse (p), Else Flip (p); lazy[p]^=type; Pushup (Fa[p]); Pushup (Fa[fa[p]);} void Build (int &x,int dad,int l,int r) {static char c;if (L>R) return;x= (l+r) >>1; Fa[x]=dad; Build (ch[x][0],x,l,x-1); scanf ("%c", &c); key[x]= (c== ' ('? 1:-1); Build (CH[X][1],X,X+1,R); Pushup (x);} void Borderbuild () {rt=n+1;key[n+1]=0; key[n+2]=0;ch[n+1][1]=n+2; fa[n+2]=n+1; Build (Ch[n+2][0],n+2,1,n); Pushup (n+2); Pushup (n+1);} int Query (int l,int r) {static int p,ans,nl,nr;p=split (L,R); nl=-pmn[p]; nr=smx[p]; Ans= (nl+1)/2+ (nr+1)/2;return ANS;}} Dt;int Main () {freopen ("/home/noilinux/documents/Module Learning/2209.in", "R", stdin), scanf ("%d%d", &n,&m);D T. Borderbuild (); for (int i=1,c,l,r;i<=m;i++) {scanf ("%d%d%d", &c,&l,&r), if (c==0) printf ("%d\n", DT. Query (l,r)); else DT. Modify (l,r,c);} return 0;}
Bzoj 2209 [Jsoi2011] bracket sequence