You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations:
modify the i-th element in the sequence or for given x y print max{Ai + Ai+1 + … + Aj | x<=i<=j<=y }.
Input
The first line of input contains an integer N. The following line contains N integers, representing the sequence A1…AN.
The third line contains an integer M. The next M lines contain the operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + … + Aj | x<=i<=j<=y }.
Output
For each query, print an integer as the problem required.
Example
Input:
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3
Output:
6
4
-3
细节太多了yyy
这个题要记录前缀和,lmax,rmax更新的时候可能加tot,还要维护区间最大字段和sum
注意不要混了
查询的时候,查询区间的最大子段和更新还是和模板一样
查询的时候返回的是结构体
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int n,m;
int a[100109];
struct st{
int l,r,lmax,rmax,all,tot;
#define l(x) t[x].l
#define r(x) t[x].r
#define lmax(x) t[x].lmax
#define rmax(x) t[x].rmax
#define all(x) t[x].all
#define tot(x) t[x].tot
}t[100109*4];
void pushup(int p){
tot(p)=tot(p*2)+tot(p*2+1);
lmax(p)=max(lmax(p*2),tot(p*2)+lmax(p*2+1));
rmax(p)=max(rmax(p*2+1),rmax(p*2)+tot(p*2+1));
all(p)=max(all(p*2),max(all(p*2+1),rmax(p*2)+lmax(p*2+1)));
}
void build(int p,int l,int r){
l(p)=l,r(p)=r;
if(l==r){
lmax(p)=rmax(p)=all(p)=tot(p)=a[l];
return;
}
int mid=(l(p)+r(p))/2;
if(l<=mid) build(p*2,l,mid);
if(r>mid) build(p*2+1,mid+1,r);
pushup(p);
}
void change(int p,int l,int r,int v){
if(l<=l(p)&&r>=r(p)){
lmax(p)=rmax(p)=all(p)=tot(p)=v;
return;
}
int mid=(l(p)+r(p))/2;
if(l<=mid) change(p*2,l,r,v);
if(r>mid) change(p*2+1,l,r,v);
pushup(p);
}
st get(int p,int l,int r){
if(l<=l(p)&&r>=r(p)) return t[p];
int mid=(l(p)+r(p))/2;
st tem,tem1,tem2;
if(r<=mid) return get(p*2,l,r);
if(l>mid) return get(p*2+1,l,r);
tem1=get(p*2,l,r);
tem2=get(p*2+1,l,r);
tem.tot=tem1.tot+tem2.tot;
tem.lmax=max(tem1.lmax,tem1.tot+tem2.lmax);
tem.rmax=max(tem2.rmax,tem2.tot+tem1.rmax);
tem.all=max(tem1.rmax+tem2.lmax,max(tem1.all,tem2.all));
return tem;
}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++){
int zl,ta,tb;
scanf("%d%d%d",&zl,&ta,&tb);
if(zl==1){
printf("%d\n",get(1,ta,tb).all);
}
else{
change(1,ta,ta,tb);
}
}
}
}