The sequence A[1],a[2],...,a[n] is given. (a[i]≤15007,1≤n≤50000). The query definition is as follows: Query (x, y) =max{a[i]+a[i+1]+...+a[j];x≤i≤j≤y}. Given a m query, the program must output the results of these queries.
This is a maximum sub-segment and can be directly removed with a segment tree
And here we learn a magical thing called the cat tree.
Can be done after preprocessing query $o (1) $
1 //Minamoto2#include <iostream>3#include <cstdio>4 using namespacestd;5 #defineGetc () (p1==p2&& (p2= (p1=buf) +fread (Buf,1,1<<21,stdin), P1==P2)? eof:*p1++)6 Charbuf[1<< +],*p1=buf,*p2=buf;7 intRead () {8 #defineNum ch-' 0 '9 CharChBOOLflag=0;intRes;Ten while(!isdigit (ch=getc ())) One(ch=='-') && (flag=true); A for(Res=num;isdigit (Ch=getc ()); res=res*Ten+num); -(flag) && (res=-res); - #undefNum the returnRes; - } - Charsr[1<< +],z[ -];intc=-1, Z; -InlinevoidOt () {fwrite (SR),1, c+1, stdout), c=-1;} + voidPrintintx) { - if(c>1<< -) Ot ();if(x<0) sr[++c]= $, x=-x; + while(z[++z]=x%Ten+ -, x/=Ten); A while(Sr[++c]=z[z],--z); sr[++c]='\ n'; at } - Const intN=5e+5; - intn,m,a[n],len,log[n],pos[n],p[ +][n],s[ +][n]; - voidBuildintPpintLintRintd) { - if(L==R)return(void) (pos[l]=pp); - intMid= (l+r) >>1, PREP,SM; inp[d][mid]=s[d][mid]=sm=prep=A[mid]; - if(sm<0) sm=0; to for(inti=mid-1; i>=l;--i) { +prep+=a[i],sm+=A[i]; -S[d][i]=max (s[d][i+1],prep), theP[d][i]=max (p[d][i+1],sm); * if(sm<0) sm=0; $ }Panax NotoginsengP[d][mid+1]=s[d][mid+1]=sm=prep=a[mid+1]; - if(sm<0) sm=0; the for(intI=mid+2; i<=r;++i) { +prep+=a[i],sm+=A[i]; AS[d][i]=max (s[d][i-1],prep), theP[d][i]=max (p[d][i-1],sm); + if(sm<0) sm=0; - } $Build (pp<<1, l,mid,d+1); $Build (pp<<1|1, mid+1, r,d+1); - } - intQueryintLintR) { the if(L==R)returnA[l]; - intd=log[pos[l]]-log[pos[l]^Pos[r]];Wuyi returnMax (Max (P[d][l],p[d][r]), s[d][l]+s[d][r]); the } - intMain () { Wu //freopen ("testdata.in", "R", stdin); -N=read (); for(intI=1; i<=n;++i) a[i]=read (); Aboutlen=2; while(len<n) len<<=1; $ for(intI=2,l=len<<1; i<=l;++i) log[i]=log[i>>1]+1; -Build1,1Len1); -m=read (); - while(m--){ A intL=read (), r=read (); + Print (query (l,r)); the } - returnOt (),0; $}
SP1043 Gss1-can You answer these queries I (cat tree)