Can you answer these queries III
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
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5e4+10;
const ll inf=1e18;
int n,m;
ll p;
struct node
{
ll sum,maxs,maxl,maxr;
}a[maxn<<2];
void pushup(int rt)
{
a[rt].sum=a[2*rt].sum+a[2*rt+1].sum;
a[rt].maxl=max(a[2*rt].maxl,a[2*rt].sum+a[2*rt+1].maxl);
a[rt].maxr=max(a[2*rt+1].maxr,a[2*rt+1].sum+a[2*rt].maxr);
a[rt].maxs=max(max(a[2*rt].maxs,a[2*rt+1].maxs),a[2*rt].maxr+a[2*rt+1].maxl);
}
void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%lld",&p);
a[rt].sum=a[rt].maxs=a[rt].maxl=a[rt].maxr=p;
return;
}
int mid=(l+r)/2;
build(l,mid,2*rt);
build(mid+1,r,2*rt+1);
pushup(rt);
}
node query(int x,int y,int l,int r,int rt)
{
if(x<=l&&y>=r)
{
return a[rt];
}
int mid=(l+r)/2;
node L,R,S;
L.sum=L.maxs=L.maxl=L.maxr=-inf;
R.sum=R.maxs=R.maxl=R.maxr=-inf;
S.sum=S.maxs=S.maxl=S.maxr=0;
if(x<=mid)
{
L=query(x,y,l,mid,2*rt);
S.sum+=L.sum;
}
if(y>mid)
{
R=query(x,y,mid+1,r,2*rt+1);
S.sum+=R.sum;
}
S.maxl=max(L.maxl,L.sum+R.maxl);
S.maxr=max(R.maxr,R.sum+L.maxr);
S.maxs=max(max(L.maxs,R.maxs),L.maxr+R.maxl);
return S;
}
void update(int x,int y,int l,int r,int rt)
{
if(l==r)
{
a[rt].sum=a[rt].maxs=a[rt].maxl=a[rt].maxr=y;
return ;
}
int mid=(l+r)/2;
if(x<=mid) update(x,y,l,mid,2*rt);
else update(x,y,mid+1,r,2*rt+1);
pushup(rt);
}
int main()
{
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
int t,x,y;
while(m--)
{
scanf("%d%d%d",&t,&x,&y);
if(t==0)
update(x,y,1,n,1);
else printf("%lld\n",query(x,y,1,n,1).maxs);
}
return 0;
}