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 Input9 8
2-6 3 5 1-5-3 6 3
Get-sum 5 4
Max-sum
INSERT 8 3-5 7 2
DELETE 12 1
Make-same 3 3 2
REVERSE 3 6
Get-sum 5 4
Max-sum
Sample Output-1
10
1
10
HINT
Finally cut off the splay God problem ...
The main thing is to move a root every time, the operation of the interval card into a complete subtree of the good!
The details have been adjusted for a long time ... = = ... No way, weak, ...>_<.
At first thought the recycling space will be very troublesome, the result is to open a queue to delete the node to traverse once put in is finished.
#include <queue>#include<cstdio>#include<cstdlib>#include<algorithm>using namespaceStd;queue<int>QH;intn,m,l,r,p,ch,a[500000],pos,tot,u,c,f;inlineintRead () {P=0; Ch=getchar (); f=1; while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') p=p*Ten+ch- -, ch=GetChar (); returnp*F;}structtree{intl,r,k,f,s,c,sum,lx,rx,mx; BOOLBo; Tree () {Bo=f=l=r=sum=0; C=1001; }};inlineintMaxintAintb) {returnA>b?a:b;} InlineintMinintAintb) {returnA<b?a:b;}structsplay_tree{intSize,root; Tree t[500001]; Splay_tree () {size=0; root=0; } InlinevoidUpdateintp) {T[P].S=t[t[p].l].s+t[t[p].r].s+1; T[p].sum=t[p].k+t[t[p].l].sum+t[t[p].r].sum; T[P].LX=max (t[t[p].l].lx,t[t[p].r].lx+t[t[p].l].sum+T[P].K); T[p].rx=max (t[t[p].r].rx,t[t[p].l].rx+t[t[p].r].sum+T[P].K); T[p].mx=max (Max (t[t[p].l].mx,t[t[p].r].mx), t[t[p].l].rx+t[t[p].r].lx+T[P].K); } InlinevoidPdintx) { if(t[x].bo) {T[t[x].l].bo^=1; t[t[x].r].bo^=1; Swap (T[X].L,T[X].R); Swap (T[X].LX,T[X].RX); T[x].bo=0; } if(t[x].c!=1001) {T[X].K=t[x].c; T[T[X].L].C=t[t[x].r].c=t[x].c; T[x].sum=t[x].c*T[x].s; if(t[x].c>=0) T[x].lx=t[x].rx=t[x].mx=t[x].sum;Elset[x].lx=t[x].rx=0, t[x].mx=t[x].c; T[X].C=1001; }} inlinevoidLerint&p) { intk=T[P].R; T[K].F=t[p].f; T[P].F=K; T[T[K].L].F=p; T[P].R=T[K].L; T[K].L=p; Update (P); update (k); P=K; } InlinevoidRir (int&p) { intk=T[P].L; T[K].F=t[p].f; T[P].F=K; T[T[K].R].F=p; T[P].L=T[K].R; T[K].R=p; Update (P); update (k); P=K; } Inlinevoidphint&p,BOOLBo) { if(bo) rir (p);Elseler (P); } InlineBOOLgcintP) {returnt[t[p].f].l==p;} InlinevoidRotintp) { if(t[p].f==root) Ph (ROOT,GC (p));Else if(GC (T[P].F)) ph (T[T[T[P].F].F].L,GC (p));Elseph (T[T[T[P].F].F].R,GC (p)); } InlinevoidSplay (intPintf) { while(t[p].f!=f) { if(t[t[p].f].f==f) Rot (p);Else if(GC (T[P].F) ==GC (p)) Rot (T[P].F), Rot (p);Elserot (p), Rot (p); }} inlinevoidBuildint&p,intLintR) { if(L>r)return; if(!qh.empty ()) P=qh.front (), Qh.pop ();Elsep=++size; intMid= (l+r) >>1; T[P].K=A[mid]; Build (T[p].l,l,mid-1); Build (T[p].r,mid+1, R); T[T[P].L].F=t[t[p].r].f=p; if(l==R) {T[p].sum=A[mid]; if(a[mid]>=0) T[p].lx=t[p].rx=a[mid];Elset[p].lx=t[p].rx=0; T[p].mx=A[mid]; T[p].s=1; }ElseUpdate (p); } InlineintFindintXinty) {PD (x); PD (T[X].L);pd(T[X].R); update (x); if(t[t[x].l].s<y-1)returnFind (t[x].r,y-1-t[t[x].l].s); if(t[t[x].l].s==y-1)returnx; returnfind (T[x].l,y); } Inlinevoid inch(intXintN) {u=find (root,x+2); X=find (root,x+1); Splay (x,0); splay (u,x); Build (T[U].L,1, N); T[T[U].L].F=u; Update (U); update (x); } InlinevoidReuse (intx) { if(!x)return; Qh.push (x); Reuse (T[X].L); reuse (T[X].R); T[X].L=t[x].r=t[x].bo=0; t[x].c=1001; } InlinevoidDelintXintN) {u=find (root,x); X=find (root,x+n+1); Splay (U,0); splay (X,u); Reuse (T[X].L); T[X].L=0; Update (x); update (U); } InlinevoidChangeintXintNintc) {u=find (root,x); X=find (root,x+n+1); Splay (U,0); splay (X,u); T[T[X].L].C=C; PD (T[X].L); Update (x); update (U); } InlinevoidTurnintXintN) {u=find (root,x); X=find (root,x+n+1); Splay (U,0); splay (X,u); T[t[x].l].bo^=1; PD (T[X].L); Update (x); update (U); } InlineintQuintXintN) {u=find (root,x); X=find (root,x+n+1); Splay (U,0); splay (X,u); returnt[t[x].l].sum; Update (x); update (U); }}t;Charss[ -];intMain () {registerinti; N=read (); m=read (); for(i=1; i<=n;i++) a[i]=read (); a[0]=-1e8;a[n+1]=-1e8; T.build (T.root,0, n+1); while(m--) {scanf ("%s", SS); if(ss[0]=='I') {pos=read (); tot=read (); for(i=1; i<=tot;i++) a[i]=read (); T.inch(Pos,tot); }Else if(ss[0]=='D') Pos=read (), tot=read (), T.del (Pos,tot); Else if(ss[0]=='M'){ if(ss[2]=='K') Pos=read (), Tot=read (), c=read (), T.change (POS,TOT,C); Elseprintf"%d\n", t.t[t.root].mx); }Else if(ss[0]=='R') Pos=read (), tot=read (), T.turn (Pos,tot); Else if(ss[0]=='G') Pos=read (), Tot=read (), printf ("%d\n", T.qu (Pos,tot)); }}
BZOJ:1500: [NOI2005] Maintenance series