"Bzoj" 2329: [HNOI2011] Brace repair (splay+ special tricks)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=2329

As with the previous question, there is more than one replace operation. OK, just a call.

And then hand it up to WA ..........

Read the puzzle, good magic!

Remember: Future pushdown tag to consider the order!

Because invert and swap operations who first who has no relationship, then consider invert and replace these two conflicting relationships

Why is there a conflict? Because if you replace the mark in the first, invert mark in the back, but invert in the pushdown to perform the qaq, such as sequence (()), originally replaced [2, 3] (, ((), then invert[2, 3], should become ())).

But! What you do in the pushdown is invert first, that is (()), then it becomes (), then replace, becomes (()!!! So wrong!!!

The workaround is to change the tag of replace after executing the invert.

That is, after becoming () (), Replace[2, 3] (into Replace[2, 3]), then ()).!!!

#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 oo=~0u>>1, n=1e5+10;struct node *null;struct node {node *f, *c[2];int s, K, Lmin, Lmax, rmin, Rmax, sum, fix;bool tag, rev;node (int _k=0) {S=1; lmin=lmax=rmin=rmax=k=sum= _k; F=c[0]=c[1]=null; tag=rev=0; fix=0; }void setc (node *x, bool D) {c[d]=x; x->f=this;} BOOL D () {return f->c[1]==this;} void Pushup () {s=c[0]->s+c[1]->s+1;sum=c[0]->sum+c[1]->sum+k;lmin=c[0]->lmin;lmax=c[0]->lmax; Rmin=c[1]->rmin;rmax=c[1]->rmax;int mnl=min (0, C[1]->lmin), mnr=min (0, c[0]->rmin); int Mxl=max (0, c[1]- >lmax), Mxr=max (0, C[0]->rmax); Lmin=min (Lmin, C[0]-&GT;SUM+K+MNL); Lmax=max (Lmax, C[0]-&GT;SUM+K+MXL); rmin= Min (rmin, C[1]-&GT;SUM+K+MNR); Rmax=max (Rmax, C[1]-&GT;SUM+K+MXR);} void Upd1 () {if (this==null) return;if (fix) fix=-fix;tag=!tag; int t;t=lmin; lmin=-lmax; lmax=-t;t=rmin; rmin=-rmax; rmax =-t;k=-k;sum=-sum;} void Upd2 () {if (this==null) Return;rev=!rev;swap (c[0], c[1]), swap (Lmin, rmin), swap (Lmax, rmax);} void upd3 (int _k) {if (this==null) return;fix=_k;k=_k;sum=s*k;lmin=rmin=k<0?sum:k;lmax=rmax=k<0?k:sum;} VOID Pushdown () {if (tag) {C[0]-&GT;UPD1 (); C[1]->upd1 (); tag=0;} if (rev.) {c[0]->upd2 (); C[1]->upd2 (); rev=0;} if (fix) {c[0]->upd3 (fix); c[1]->upd3 (fix); fix=0;}}} *root;void PN (node *x) {printf ("key:%c \ lmin:%d \ t lmax:%d \ t rmin:%d \ t rmax:%d \ t sum:%d \ n", x->k==1? ' (':(x->k==-1 ') ': ' N '), X->lmin, X->lmax, X->rmin, X->rmax, x->sum);} void Pr (node *x) {if (x==null) Return;x->pushdown (); Pr (x->c[0]); if (x->k) printf ("%c", x->k==1? ' (‘:‘)‘); Pr (x->c[1]);} void P (node *x=root) {Pr (x); puts ("");} 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 (); Node *sel (node *x, int k) {if (x==null) return X;x->pushdown (); int s=x->c[0]->s;if (s==k) return x;if (s<k) return sel (x->c[1], k-s-1); 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];} void fix1 () {int l=getint (), R=getint (), node *x=getrange (l, R); X->pushdown (); X->upd1 ();} void Fix2 () {int l=getint (), R=getint (), node *x=getrange (l, R); X->pushdown (); X->upd2 ();} void Fix3 () {int l=getint (), R=getint (), node *x=getrange (l, R), Char C=getchar (), while (c!= ') ' &&c!= ' (') c= GetChar (); int fix=c== ') '? -1:1;x->pushdown (); x->upd3 (fix);} void Ask () {int l=getint (), R=getint (), node *x=getrange (l, R), l= (-x->lmin+1) >>1;r= (x->rmax+1) >>1; printf ("%d\n", L+r);} Char S[n];int N, M, q;void build (int l, int r, node *&x) {if (l>r) Return;int mid= (L+r) >>1;x=new node (s[mid]== '? -1:1), if (l==r) Return;build (L, Mid-1, x->c[0]); Build (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 In (node *x) {x->lmax=x->rmax=-oo;x->lmin=x->rmin=oo;x->sum=0;x->k=0;} void Init () {null=new node (); null->s=0; in (null), Null->f=null->c[0]=null->c[1]=null;root=new node (); In ( root); Root->setc (new Node (), 1); In (root->c[1]); node *x;read (n); Read (m); scanf ("%s", s+1), build (1, N, x), root->c[1]->setc (x, 0); Root->c[1]->pushup (); Root->pushup ();} Char O[10];int main () {init (), while (m--) {scanf ("%s", O), if (o[0]== ' Q ') ask (), else if (o[0]== ' I ') fix1 (); else if (o[0]== ' s ') fix2 (); else fix3 ();//p ();} return 0;}

  

Description

Input Output sample input sample output HINT Source

Splay

"Bzoj" 2329: [HNOI2011] Brace repair (splay+ special tricks)

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.