[bzoj1500] [NOI2005 Repair Series] (splay interval operation)

Source: Internet
Author: User

Description

Input

The 1th line of input contains two numbers n and M (m≤20), n denotes the number of numbers in the initial sequence, and M indicates the number of operations to be performed.
Line 2nd contains n numbers that describe the initial sequence.
The following m lines, one command per line, are shown in the table in the problem description.
At any time, the number of numbers in a sequence is 500 000, and any digit in the sequence is within [-1 000, 1 000].
The total number of inserted numbers does not exceed 4 000 000, the input file size does not exceed 20MBytes.

Output

For Get-sum and max-sum operations in the input data, print the results sequentially to the output file, one row for each answer (number).

Sample Input
9 82-6 3 5 1-5-3 6 3GET-sum5 4MAX-Suminsert8 3-5 7 2DELETE A 1 Make-same3 3 2REVERSE3 6GET-sum5 4MAX-sum
Sample Output
-11
HINT

Solution
#include <cstdio>#include<iostream>#defineN 500010#defineINF 0x3f3f3f3f#defineMid ((x>>1) + (y>>1) + (x&y&1))using namespaceStd;inlineintRin () {intx=0, C=getchar (), f=1;  for(;c< -|| C> $; c=GetChar ())if(! (c^ $)) f=-1;  for(;c> -&&c< -; c=GetChar ()) x= (x<<1) + (x<<3) +c- -; returnx*F;}structnt{NT*ch[2],*p; BOOLRev;intCoV; intsize,sum,v,lmx,rmx,mmx; BOOLD () {return  This==p->ch[1];} voidSetC (Nt*c,intd) {Ch[d]=C; C->p= This; }  voidRevIt () {Rev^=1;    Swap (LMX,RMX); Swap (ch[0],ch[1]); }  voidCovit (intCO) {CoV=v=Co; Sum=co*size; Co>0? lmx=rmx=mmx=sum:lmx=rmx=mmx=Co; }  voidPu () {size=1+ch[0]->size+ch[1]->size; Sum=v+ch[0]->sum+ch[1]->sum; LMX=max (ch[0]->lmx,ch[0]->sum+v+max (ch[1]-&GT;LMX,0)); RMX=max (ch[1]->rmx,ch[1]->sum+v+max (ch[0]-&GT;RMX,0)); MMX=max (Max (ch[0]->mmx,ch[1]-&GT;MMX), Max (ch[0]-&GT;RMX,0) +v+max (ch[1]-&GT;LMX,0)); }  voidRelax () {if(rev) ch[0]->revIt (), ch[1]->revIt (); if(cov^inf) ch[0]->Covit (CoV), ch[1]->Covit (CoV); Rev=0; CoV=inf; }};nt*NULL=NewNT (); NT*root=NULL;intn,a[n],m;Charsign[Ten];nt*newnode (Nt*p,intv) {NT*o=NewNT (); o->size=1; o->v=o->sum=o->lmx=o->rmx=o->mmx=v; o->ch[0]=o->ch[1]=NULL; o->cov=inf; o->p=p; returno;}voidRot (nt*&o) {NT*p=o->p; P-relax (); o-relax (); BOOLD=o->d (); P-&GT;P-&GT;SETC (o,p->d ()); P-&GT;SETC (o->ch[!d],d); o-&GT;SETC (p,!d); P->pu ();o->Pu (); if(p==root) root=o;}voidSplay (nt*o,nt*p) {   while(o->p!=p)if(o->p->p==p) rot (o); Elseo->d () ^o->p->d ()? (Rot (o), Rot (o)):(rot (o->p), Rot (o)); o-Pu ();} NT*build (intXinty) {  if(x>y)return NULL; NT*o=NewNode (O,a[mid]); o-&GT;SETC (Build (x,mid-1),0); o-&GT;SETC (Build (mid+1, y),1); o-Pu (); returno;}voidDel (nt*&o) {  if(o->ch[0]!=NULL) del (o->ch[0]); if(o->ch[1]!=NULL) del (o->ch[1]); Deleteo;} NT*kth (intk) {   for(nt*o=root;;) {o-relax (); if(k<=o->ch[0]->size) o=o->ch[0]; Else{k-=o->ch[0]->size+1; if(!k)returno; o=o->ch[1]; }  }}intMain () {n=rin (), m=Rin ();  for(intI=1; i<=n;i++) a[i]=Rin (); Root=build (0, n+1); Root->p=NULL; intx, Y, Z  while(m--) {scanf ("%s", sign); Switch(sign[2]){     Case'S': x=rin (), y=Rin ();  for(intI=1; i<=y;i++) a[i]=Rin (); Splay (KTH (x+1),NULL); Splay (KTH (x+2), root); Root->ch[1]-&GT;SETC (Build (1, y),0); Root->ch[1]->Pu (); Root-Pu ();  Break;  Case'L': x=rin (), y=Rin (); Splay (KTH (x),NULL); Splay (KTH (x+y+1), root); Del (Root->ch[1]->ch[0]); Root->ch[1]->ch[0]=NULL; Root->ch[1]->Pu (); Root-Pu ();  Break;  Case'K': x=rin (), Y=rin (), z=Rin (); Splay (KTH (x),NULL); Splay (KTH (x+y+1), root); Root->ch[1]->ch[0]->Covit (z); Root->ch[1]->Pu (); Root-Pu ();  Break;  Case'T': x=rin (), y=Rin (); Splay (KTH (x),NULL); Splay (KTH (x+y+1), root); printf ("%d\n", root->ch[1]->ch[0]->sum);  Break;  Case'V': x=rin (), y=Rin (); Splay (KTH (x),NULL); Splay (KTH (x+y+1), root); Root->ch[1]->ch[0]->revIt (); Root->ch[1]->Pu (); Root-Pu ();  Break;  Case'X': Splay (KTH (1),NULL); Splay (kth (Root-size), root); printf ("%d\n", root->ch[1]->ch[0]->MMX);  Break; default: Break; }  }  return 0;}

[bzoj1500] [NOI2005 Repair Series] (splay interval operation)

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.