1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intan[50010];8 int Get[50010];9 structNodeTen { One intL,r; A intLmax,rmax,ma; - intsum; -}bn[200000]; the - voidBuildintKintLintR) - { -Bn[k].l=l; +Bn[k].r=R; -bn[k].sum=Get[r]-Get[l1]; + if(l==R) A { atBn[k].ma=An[l]; -bn[k].lmax=An[l]; -bn[k].rmax=An[l]; - return ; - } - intlk=k*2; in intrk=lk+1; - intMid= (L+R)/2; to build (Lk,l,mid); +Build (rk,mid+1, R); -Bn[k].lmax=max (bn[lk].lmax,bn[lk].sum+Bn[rk].lmax); theBn[k].rmax=max (bn[rk].rmax,bn[rk].sum+Bn[lk].rmax); *Bn[k].ma=Max (Bn[k].lmax,bn[k].rmax); $Bn[k].ma=Max (bn[k].ma,bn[lk].ma);Panax NotoginsengBn[k].ma=Max (bn[k].ma,bn[rk].ma); -Bn[k].ma=max (bn[k].ma,bn[lk].rmax+Bn[rk].lmax); thebn[k].sum=bn[lk].sum+bn[rk].sum; + } A theNode Search (intKintLintR) + { - if(bn[k].l==l&&bn[k].r==R) $ { $ //cout<< "L" <<l<< "R" <<r<<endl; - //cout<<bn[k].ma<< "<<bn[k].sum<<" "<<bn[k].lmax<<" "<<bn[k].rmax& lt;<endl; - returnBn[k]; the } - intlk=k*2;Wuyi intrk=lk+1; the if(bn[lk].r>=R) - { Wu //cout<< "Kk2" <<endl; - returnsearch (lk,l,r); About } $ Else if(bn[rk].l<=l) - { - //cout<< "Kk1" <<endl; - returnsearch (rk,l,r); A } + Else the { - //cout<< "KK" <<endl; $Node a=search (LK,L,BN[LK].R); theNode b=search (rk,bn[rk].l,r); the Node ret; theret.sum=a.sum+b.sum; theRet.lmax=max (a.lmax,a.sum+B.lmax); -Ret.rmax=max (b.rmax,b.sum+A.rmax); inRet.ma=Max (Ret.lmax,ret.rmax); theRet.ma=Max (ret.ma,b.ma); theRet.ma=Max (ret.ma,a.ma); AboutRet.ma=max (ret.ma,a.rmax+B.lmax); the //cout<< "s" <<a.lmax<< "" <<a.rmax<< "<<a.ma<<" "<<a.sum<< Endl; the returnret; the } + } - the intMain ()Bayi { the intN; the while(SCANF ("%d", &n)! =EOF) - { -Memset (AN,0,sizeof(an)); theMemsetGet,0,sizeof(Get)); the for(intI=1; i<=n;i++) the { thescanf"%d",&an[i]); - } the for(intI=1; i<=n;i++) the { the Get[i]=Get[I1]+An[i];94 } theBuild1,1,50010); the intm; thescanf"%d",&m);98 intb; About for(intI=0; i<m;i++) - {101scanf"%d%d",&a,&b);102printf"%d\n", Search (1, A, b). Ma);103 }104 } the return 0;106}
View Code
No modification interval Query BNU Can answer these queries I