sopj 1716 Can you answer these queries III

题目链接:https://www.spoj.pl/problems/GSS3/

题目大意:求子序列的最大子序列和,且可单点更新某一值.

思路:和Can you answer these queries I一样,但是sum不能离线,需要在线段树中维护,因为值会被更新.

代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

//#define ull unsigned __int64
//#define ll __int64
#define ull unsigned long long
#define ll long long
#define son1 New(p.xl,xm,p.yl,ym),(rt<<2)-2
#define son2 New(p.xl,xm,min(ym+1,p.yr),p.yr),(rt<<2)-1
#define son3 New(min(xm+1,p.xr),p.xr,p.yl,ym),rt<<2
#define son4 New(min(xm+1,p.xr),p.xr,min(ym+1,p.yr),p.yr),rt<<2|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define middle (l+r)>>1
#define MOD 1000000007
#define esp (1e-8)
const int INF=0x3F3F3F3F;
const double DINF=10000.00;
//const double pi=acos(-1.0);
const int N=50010;
int n,m,tot;

ll lmax[N<<2],rmax[N<<2],mmax[N<<2],sum[N<<2];

struct node{
	ll lmax,rmax,mmax,sum;
};

ll max(ll x,ll y){return x>y? x:y;}

void func(ll& r,ll x,ll y,int& p,int xp,int yp){
	if(x>y) r=x,p=xp;
	else r=y,p=yp;
}

void pushUp(int rt){
	int ls=rt<<1,rs=ls|1;
	sum[rt]=sum[ls]+sum[rs];
	lmax[rt]=max(lmax[ls],sum[ls]+lmax[rs]);
	rmax[rt]=max(rmax[rs],sum[rs]+rmax[ls]);
	mmax[rt]=max(max(mmax[ls],mmax[rs]),rmax[ls]+lmax[rs]);
}

void build(int l,int r,int rt){
	if(l==r){
		scanf("%lld",&sum[rt]);
		lmax[rt]=mmax[rt]=rmax[rt]=sum[rt];
		return;
	}
	int mid=middle;
	build(lson),build(rson);
	pushUp(rt);
}

node query(int l,int r,int rt,int L,int R){
	if(L<=l && r<=R){
		node ret;
		ret.sum=sum[rt];
		ret.lmax=lmax[rt];
		ret.rmax=rmax[rt];
		ret.mmax=mmax[rt];
		return ret;
	}
	int mid=middle;
	if(R<=mid) return query(lson,L,R);
	if(mid<L) return query(rson,L,R);
	node lret=query(lson,L,mid);
	node rret=query(rson,mid+1,R);
	node ret;
	ret.sum=lret.sum+rret.sum;
	ret.lmax=max(lret.lmax,lret.sum+rret.lmax);
	ret.rmax=max(rret.rmax,rret.sum+lret.rmax);
	ret.mmax=max(max(lret.mmax,rret.mmax),lret.rmax+rret.lmax);
	return ret;
}

void update(int l,int r,int rt,int p,ll c){
	if(l==r){
		lmax[rt]=mmax[rt]=rmax[rt]=sum[rt]=c;
		return;
	}
	int mid=middle;
	if(p<=mid) update(lson,p,c);
	else update(rson,p,c);
	pushUp(rt);
}

void init(){
	sum[0]=0;
	build(1,n,1);
	scanf("%d",&m);
}

void sof(){
	int i,x,y;
	while(m--){
		scanf("%d%d%d",&i,&x,&y);
		if(i) printf("%lld\n",query(1,n,1,x,y).mmax);
		else update(1,n,1,x,(ll)y);
	}
}

int main(){
	//freopen("1.in","r",stdin);
	//freopen("no pos.out","w",stdout);
	//int T,cas;scanf("%d",&T);for(cas=1;cas<=T;cas++){
	while(~scanf("%d",&n)){
		init(),sof();
	}
	return 0;
}


你可能感兴趣的:(sopj 1716 Can you answer these queries III)