SPOJ GSS3 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.



参见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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值