Description
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
Hint
Added by: | Bin Jin |
Date: | 2007-08-03 |
Time limit: | 0.330s |
Source limit: | 5000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: C++ 5 |
Resource: | own problem |
比gss1多了一个修改,其实就是把build复制一遍。
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005;
int n,m;
long long data[N];
struct node
{
int l,r;
long long sum,tot,lm,rm;
}a[4*N];
void build(int num,int l,int r)
{
a[num].l=l,a[num].r=r;
if(l==r)
{
a[num].sum=a[num].tot=a[num].lm=a[num].rm=data[l];
return ;
}
int mid=(l+r)/2;
build(2*num,l,mid);
build(2*num+1,mid+1,r);
a[num].sum=a[2*num].sum+a[2*num+1].sum;
a[num].lm=max(a[2*num].lm,a[2*num].sum+a[2*num+1].lm);
a[num].rm=max(a[2*num+1].rm,a[2*num+1].sum+a[2*num].rm);
a[num].tot=max(max(a[2*num].tot,a[2*num+1].tot),a[2*num].rm+a[2*num+1].lm);
}
void chg(int num,int pos,long long x)
{
if(a[num].l>pos||a[num].r<pos)
return ;
if(a[num].l==a[num].r)
{
a[num].sum=a[num].tot=a[num].lm=a[num].rm=x;
return ;
}
chg(2*num,pos,x);
chg(2*num+1,pos,x);
a[num].sum=a[2*num].sum+a[2*num+1].sum;
a[num].lm=max(a[2*num].lm,a[2*num].sum+a[2*num+1].lm);
a[num].rm=max(a[2*num+1].rm,a[2*num+1].sum+a[2*num].rm);
a[num].tot=max(max(a[2*num].tot,a[2*num+1].tot),a[2*num].rm+a[2*num+1].lm);
}
node query(int num,int l,int r)
{
if(a[num].l>=l&&a[num].r<=r)
return a[num];
if(a[2*num].r>=r)
return query(2*num,l,r);
if(a[2*num+1].l<=l)
return query(2*num+1,l,r);
node u1,u2,res;
u1=query(2*num,l,r);
u2=query(2*num+1,l,r);
res.sum=u1.sum+u2.sum;
res.lm=max(u1.lm,u1.sum+u2.lm);
res.rm=max(u2.rm,u2.sum+u1.rm);
res.tot=max(max(u1.tot,u2.tot),u1.rm+u2.lm);
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&data[i]);
build(1,1,n);
scanf("%d",&m);
int k,x,y;
while(m--)
{
scanf("%d%d%d",&k,&x,&y);
if(k==0)
{
chg(1,x,y);
}
else
printf("%lld\n",query(1,x,y).tot);
}
return 0;
}