Portal
Splay practice.
Consider converting a sequence of parentheses into something similar to the interval maximum/minimum value.
It is clear that we can see that the $a$ of the bracket sequence is certainly the case, and $b$ (that is, the cost of legalizing all these brackets is obviously $\frac{a+1}{2}+\frac{b+1}{2}$.
Then we can turn ' (' into 1, turn ') ' to 1, and then each time we take the continuous minimum of the left interval, the continuous maximum of the right interval is the size of the $a$ and $b$.
Because there are interval flips, it is necessary to make the continuous maximum/small value of the left/right interval.
Splay can be.
Bzoj2209//by cydiater//2017.2.15#include <iostream> #include <iomanip> #include <cstring># Include <string> #include <queue> #include <map> #include <cmath> #include <ctime># Include <cstdlib> #include <cstdio> #include <algorithm> #include <bitset> #include <set> #include <vector> #include <complex>using namespace std; #define LL Long long#define up (i,j,n) for (int i=j;i <=n;i++) #define DOWN (i,j,n) for (int i=j;i>=n;i--) #define CMAX (A, B) A=max (A, b) #define Cmin (A, B) A=min (A, b) const int maxn=1e5+5;const int Oo=0x3f3f3f3f;inline int read () {char ch=getchar (); int x=0,f=1;while (ch> ' 9 ' | | ch< ' 0 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int Root,cnt=0,n,m,arr[maxn];char s[maxn];struct splaytree{int son[2],l0,l1,r0,r1,sum,fa,siz,tag0,tag1,val;} T[maxn];namespace solution{inline int get (int k) {return t[t[k].fa].son[1]==k;} inline void reload (int k) {inT S1=t[k].son[0],s2=t[k].son[1];t[k].l0=min (t[s1].l0,t[s1].sum+t[k].val+t[s2].l0); T[k].l1=max (T[s1].l1,t[s1]. SUM+T[K].VAL+T[S2].L1); T[k].r0=min (T[S2].R0,T[S2].SUM+T[K].VAL+T[S1].R0); T[k].r1=max (T[s2].r1,t[s2].sum+t[k]. VAL+T[S1].R1); t[k].siz=t[s1].siz+t[s2].siz+1;t[k].sum=t[s1].sum+t[s2].sum+t[k].val;} inline void push0 (int k) {if (!k) Return;swap (T[K].L0,T[K].L1); T[k].l0*=-1;t[k].l1*=-1;swap (T[K].R0,T[K].R1); t[k].r0 *=-1;t[k].r1*=-1;t[k].tag0^=1;t[k].val*=-1;t[k].sum*=-1;} inline void push1 (int k) {if (!k) Return;swap (T[K].L0,T[K].R0); swap (T[K].L1,T[K].R1); t[k].tag1^=1;} inline void pushdown (int k) {int s1=t[k].son[0],s2=t[k].son[1];if (T[K].TAG1) {push1 (S1);p ush1 (S2); Swap (t[k].son[0],t [k].son[1]); t[k].tag1=0;} if (t[k].tag0) {push0 (S1);p ush0 (S2); t[k].tag0=0;}} inline void rotate (int k) {int old=t[k].fa,oldf=t[old].fa,which=get (k); T[old].son[which]=t[k].son[which^1];t[t[old] . Son[which]].fa=old;t[k].son[which^1]=old;t[old].fa=k;t[k].fa=oldf;if (Oldf) t[oldf].son[t[oldf].son[1]==old]=k; Reload (old); reload (k);} inline void splay (int k,int aim) {for (int fa, (FA=T[K].FA); rotate (k)) {if (K==aim) Break;else if (fa==aim) {rotate (k); break ;} else if (t[fa].fa==aim) {rotate (get (FA) ==get (k)? fa:k); rotate (k); break;} else rotate (get (FA) ==get (k)? fa:k);} if (aim==root) root=k;} int Node (int rnk) {int Now=root;while (true) {pushdown (now); int Lsiz=t[now].son[0]?t[t[now].son[0]].siz:0;if (rnk<= Lsiz) now=t[now].son[0];else{if (rnk==lsiz+1) return now;rnk-=lsiz+1;now=t[now].son[1];}}} int Match (int l,int R) {int Kl=node (L), Kr=node (r+2); splay (kl,root); splay (kr,t[root].son[1]); return KR;} void Build (int l,int r,int &k,int fa) {if (!k) K=++cnt;int mid= (l+r) >>1;t[k].fa=fa;t[k].siz=1;t[k].tag0=t[k]. Tag1=0;t[k].l0=t[k].l1=t[k].r0=t[k].r1=0;t[k].val=arr[mid];if (l==r) {t[k].son[0]=t[k].son[1]=0;t[k].sum=t[k]. Val;t[k].l0=t[k].l1=t[k].r0=t[k].r1=t[k].val;cmin (t[k].l0,0); Cmin (t[k].r0,0); Cmax (t[k].l1,0); Cmax (t[k].r1,0); return;} if (l<=mid-1) build (l,mid-1,t[k].son[0],k); if (mid+1<=r) build (mid+1,r,t[k].son[1],k); reload (k);} int Col (intL,int R) {int k=match (L,R); return ((t[t[k].son[0]].r1+1)/2)-((t[t[k].son[0]].l0-1)/2);} void Inv (int l,int R) {int K=match (l,r);p ush0 (t[k].son[0]); reload (k); reload (T[K].FA);} void Rev (int l,int R) {int K=match (l,r);p ush1 (t[k].son[0]); reload (k); reload (T[K].FA);} void Prepare () {n=read (); M=read (); scanf ("%s", s+1), Up (I,1,n) arr[i]= (s[i]== ' ('? 1:-1); arr[0]=0;arr[n+1]=0; Build (0,n+1,root,0);} void Solve () {//debug (root), while (m--) {int op=read (), L=read (), R=read (), if (op==0) printf ("%d\n", Col (L,r)); if (op==1) INV (L,R); if (op==2) Rev (l,r);}} int main () {using namespace solution; Prepare (); Solve (); return 0;}
BZOJ2209: [Jsoi2011] Bracket sequence