Http://uoj.ac/problem/31
To commemorate the great splay ...
There's a mistake in that find,!!!!!!!!!!!!!.
Remember later: Be sure to think it over!
I won't write it, it's too easy.
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int n=200005;struct node *null;struct node {node *c[2], *f;int S, K, sum;bool rev;node (int _k=0) {c[0]=c[1]=f=null; s=1; k=sum=_k; rev=0;} void Pushup () {s=1+c[0]->s+c[1]->s; sum=k+c[0]->sum+c[1]->sum;} void SetC (node *x, bool D) {c[d]=x; x->f=this;} BOOL D () {return f->c[1]==this;} void Upd () {if (this==null) Return;rev=!rev;swap (c[0], c[1]);} void Pushdown () {if (rev) {rev=0;c[0]->upd (); c[1]->upd ();}}} *root;void Rot (node *x) {node *f=x->f;f->pushdown (); X->pushdown (); bool D=x->d (); f->f->setc (x, F >d ()); F->setc (X->c[!d], D); X->setc (f,!d); F->pushup (); if (F==root) root=x;} void splay (node *x, node *f=null) {X->pushdown (); while (x->f!=f) {if (x->f->f==f) rot (x); else x->d () ==x- >f->d ()? (Rot (x->f), rot (x)):(rot (x), rot (x)); X->pushup ();} void Pr (node *x) {if (x==null) return; Pr (x->c[0]);p rintf ("%c", x->k? ' (‘:‘)‘); Pr (x->c[1]);} void P (node *x=root) {Pr (x); puts ("");} void Init () {null=new node; null->s=0; null->c[0]=null->c[1]=null->f=null; Root=null; }void Build (char *s, int l, int r, node *&x) {if (l>r) Return;int mid= (L+r) >>1;x=new node (s[mid]== ' ('); if (l==r ) Return;build (S, L, Mid-1, x->c[0]); Build (S, mid+1, R, X->c[1]), if (l<=mid-1) x->c[0]->f=x;if (mid+1< =R) X->c[1]->f=x;x->pushup ();} void Build (char *s) {int Len=strlen (s); Node *x=null;root=new node (); x=new node (); Root->setc (x, 1); x=new node (); build (s-1, 1, Len, x); root->c[1]->setc (x, 0); Root->c[1]->pushup (); Root->pushup ();} node* Find (node *x) {if (x==null) return Null;x->pushdown (); if (x->c[0]->sum==0 && x->k) return x;// Here, ah ah if (x->c[0]->sum) return find (X->c[0]); return find (X->c[1]);} Node* SEL (node *x, int k) {if (x==null) return Null;x->pushdown (); int s=x->c[0]->s;if (S==K) return x;if (s<k) r Eturn sel (x->c[1], k-s-1), Else return sel (x->c[0], k);} node* getrange (int l, int r) {splay (sel (root, L-1)), splay (sel (Root, r+1), root); return root->c[1]->c[0];} int GetPos (int l, INT r) {node *x=getrange (l, R); X=find (x);//dbg (x->k); splay (x); return x->c[0]->s;} void fix (int l, int r) {node *x=getrange (l, R);//p (x); x->upd (); Root->c[1]->pushup (); Root->pushup ();} int Getinf (int l) {node *r=getrange (L, L); return r->k;} Char s[n];int ans;int x[n], y[n], N, len;//int ifind (int now) {//Int. ret=now;//while (Ret<=len) if (s[ret]== ' (') retur n ret; else ++ret;//return 0;//}//void fix (int l, int r) {//while (L<R) swap (S[l], s[r]), ++L,--r;//}int main () {scanf ("% S ", s+1); init (); Len=strlen (s+1);//dbg (s+1); N=len>>1;build (s+1);//dbg (GetPos (2, Len));//fix (1, GetPos (2, Len) );//node *xx=sel (root, 1); DBG (Xx->s); P (); For1 (i, 1, n) {//printf ("%c\n", Getinf (i)? ' (': ') '), if (Getinf (i)!=1) {++ans;int pos=getpos (i+1, Len), if (!pos) {puts ("error"); return 0;} X[ans]=i;y[ans]=pos;fix (i, POS);//dbg (s+1);} P ();} printf ("%d\n", ans); For1 (i, 1, ans) {printf ("%d%d\n", X[i], y[i]);} return 0;}
"Uoj" "UR #2" pig man again battle bracket sequence (splay/greedy)