C - Can you answer these queries III(SPOJ-GSS3) (线段树区间更新)

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

细节太多了yyy

这个题要记录前缀和,lmax,rmax更新的时候可能加tot,还要维护区间最大字段和sum
注意不要混了
查询的时候,查询区间的最大子段和更新还是和模板一样
查询的时候返回的是结构体

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int n,m;
int a[100109];

struct st{
	int l,r,lmax,rmax,all,tot;
	#define l(x) t[x].l
	#define r(x) t[x].r
	#define lmax(x) t[x].lmax
	#define rmax(x) t[x].rmax
	#define all(x) t[x].all
	#define tot(x) t[x].tot
}t[100109*4];


void pushup(int p){
	tot(p)=tot(p*2)+tot(p*2+1);
	lmax(p)=max(lmax(p*2),tot(p*2)+lmax(p*2+1));
	rmax(p)=max(rmax(p*2+1),rmax(p*2)+tot(p*2+1));
	all(p)=max(all(p*2),max(all(p*2+1),rmax(p*2)+lmax(p*2+1)));
	
}


void build(int p,int l,int r){
	l(p)=l,r(p)=r;
	if(l==r){
		lmax(p)=rmax(p)=all(p)=tot(p)=a[l];
		return;
	} 
	int mid=(l(p)+r(p))/2;
	if(l<=mid) build(p*2,l,mid);
	if(r>mid) build(p*2+1,mid+1,r);
	pushup(p);
}


void change(int p,int l,int r,int v){
	if(l<=l(p)&&r>=r(p)){
		lmax(p)=rmax(p)=all(p)=tot(p)=v;
		return;
	}
	int mid=(l(p)+r(p))/2;
	if(l<=mid) change(p*2,l,r,v);
	if(r>mid) change(p*2+1,l,r,v);
	pushup(p);
}


st get(int p,int l,int r){
	if(l<=l(p)&&r>=r(p)) return t[p];
	int mid=(l(p)+r(p))/2;
	st tem,tem1,tem2;
	if(r<=mid) return get(p*2,l,r);
	if(l>mid) return get(p*2+1,l,r);
	tem1=get(p*2,l,r);
	tem2=get(p*2+1,l,r);
	tem.tot=tem1.tot+tem2.tot;	
	tem.lmax=max(tem1.lmax,tem1.tot+tem2.lmax);
	tem.rmax=max(tem2.rmax,tem2.tot+tem1.rmax);
	tem.all=max(tem1.rmax+tem2.lmax,max(tem1.all,tem2.all));
	return tem;
}



int main(){
	while(~scanf("%d",&n)){
		for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
		build(1,1,n);
		scanf("%d",&m);		
		for(int i=1;i<=m;i++){
			int zl,ta,tb;
			scanf("%d%d%d",&zl,&ta,&tb);
			if(zl==1){
				printf("%d\n",get(1,ta,tb).all);
			}
			else{
				change(1,ta,ta,tb);
			}
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值