您的位置:首页 > 产品设计 > UI/UE

SPOJ GSS3 Can you answer these queries III

2016-05-12 23:47 381 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: