SPOJ GSS3 Can you answer these queries III

来源:互联网 发布:matlab floyd算法 编辑:程序博客网 时间:2024/05/16 18:35

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.



参见http://blog.csdn.net/sdfzyhx/article/details/51388593,SPOJ GSS1。

增加修改而已。

#include<cstdio>struct node{int lch,rch,l,r,lmax,rmax,max,sum;}t[200010];struct ans{int l,r,max,s;};int n,nn,a[50010];int mx(int x,int y){return x>y?x:y;}void build(int p,int l,int r){t[p].l=l;t[p].r=r;if (l==r){t[p].lmax=t[p].rmax=t[p].max=t[p].sum=a[l];return;}int mid=(t[p].l+t[p].r)/2;t[p].lch=++nn;build(nn,l,mid);t[p].rch=++nn;build(nn,mid+1,r);t[p].sum=t[t[p].lch].sum+t[t[p].rch].sum;t[p].lmax=mx(t[t[p].lch].lmax,t[t[p].lch].sum+t[t[p].rch].lmax);t[p].rmax=mx(t[t[p].rch].rmax,t[t[p].rch].sum+t[t[p].lch].rmax);t[p].max=mx(mx(t[t[p].lch].max,t[t[p].rch].max),t[t[p].lch].rmax+t[t[p].rch].lmax);}ans find(int p,int l,int r){ans al,ar,a;if (t[p].l==l&&t[p].r==r){a.l=t[p].lmax;a.r=t[p].rmax;a.max=t[p].max;a.s=t[p].sum;return a;}int mid=(t[p].l+t[p].r)/2;if (r<=mid) return find(t[p].lch,l,r);if (l>=mid+1) return find(t[p].rch,l,r);al=find(t[p].lch,l,mid);ar=find(t[p].rch,mid+1,r);a.l=mx(al.l,al.s+ar.l);a.r=mx(ar.r,ar.s+al.r);a.s=al.s+ar.s;a.max=mx(mx(al.max,ar.max),al.r+ar.l);return a;}void modi(int p,int q,int x){if (t[p].l==t[p].r){t[p].max=t[p].lmax=t[p].rmax=t[p].sum=x;return;}int mid=(t[p].l+t[p].r)/2;if (q<=mid) modi(t[p].lch,q,x);else modi(t[p].rch,q,x);t[p].sum=t[t[p].lch].sum+t[t[p].rch].sum;t[p].lmax=mx(t[t[p].lch].lmax,t[t[p].lch].sum+t[t[p].rch].lmax);t[p].rmax=mx(t[t[p].rch].rmax,t[t[p].rch].sum+t[t[p].lch].rmax);t[p].max=mx(mx(t[t[p].lch].max,t[t[p].rch].max),t[t[p].lch].rmax+t[t[p].rch].lmax);}int main(){int i,j,k,m,p,q,x,y,z;bool b;scanf("%d",&n);for (i=1;i<=n;i++)  scanf("%d",&a[i]);nn=1;build(1,1,n);scanf("%d",&m);for (i=1;i<=m;i++){scanf("%d%d%d",&b,&x,&y);if (b) printf("%d\n",find(1,x,y).max);else modi(1,x,y);}}


0 0