SPOJ GSS3 Can you answer these queries III

本文介绍了一种使用线段树解决区间更新与查询最大子段和的问题。通过构造线段树节点并定义节点属性,实现了区间修改和快速查询区间内的最大连续子序列和的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值