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.
#include<bits/stdc++.h>
#define MAXN 50010
using namespace std;
int lc[4*MAXN],rc[4*MAXN],mc[4*MAXN],sum[4*MAXN];
int c[MAXN];
void build(int id,int l,int r)
{
if(l==r)
{
sum[id]=mc[id]=lc[id]=rc[id]=c[l];
return;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
sum[id]=sum[id*2]+sum[id*2+1];
lc[id]=max(lc[id*2],sum[id*2]+lc[id*2+1]);
rc[id]=max(rc[id*2+1],sum[id*2+1]+rc[id*2]);
mc[id]=max(max(mc[id*2],mc[id*2+1]),rc[id*2]+lc[id*2+1]);
}
int query(int id,int x,int y,int l,int r,int flag,int &ans)
{
if(l<=x&&y<=r)
{
ans=max(ans,mc[id]);
return flag==-1?lc[id]:rc[id];
}
int mid=(x+y)/2;
if(r<=mid)
return query(id*2,x,mid,l,r,-1,ans);
else if(l>mid)
return query(id*2+1,mid+1,y,l,r,1,ans);
else
{
int ln=query(id*2,x,mid,l,r,1,ans);
int rn=query(id*2+1,mid+1,y,l,r,-1,ans);
ans=max(ln+rn,ans);
if(flag==-1)
return max(lc[id*2],sum[id*2]+rn);
else
return max(rc[id*2+1],sum[id*2+1]+ln);
}
}
void update(int id,int l,int r,int pos,int val)
{
if(l==r)
{
sum[id]=lc[id]=rc[id]=mc[id]=val;
return;
}
int mid=(l+r)/2;
if(pos<=mid)
update(id*2,l,mid,pos,val);
else
update(id*2+1,mid+1,r,pos,val);
sum[id]=sum[id*2]+sum[id*2+1];
lc[id]=max(lc[id*2],sum[id*2]+lc[id*2+1]);
rc[id]=max(rc[id*2+1],sum[id*2+1]+rc[id*2]);
mc[id]=max(max(mc[id*2],mc[id*2+1]),rc[id*2]+lc[id*2+1]);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&c[i]);
}
build(1,1,n);
int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
int ans=c[l];
query(1,1,n,l,r,-1,ans);
printf("%d\n",ans);
}
else
{
c[l]=r;
update(1,1,n,l,r);
}
}
return 0;
}
在GSS1的基础上加入修改操作就行了;