SPOJ GSS3 Can you answer these queries III[线段树]

SPOJ - GSS3

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

GSS1加个单点修改
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define m ((l+r)>>1)
#define lson o<<1,l,m
#define rson o<<1|1,m+1,r
#define lc o<<1
#define rc o<<1|1
using namespace std;
typedef long long ll;
const int N=5e5+,INF=2e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,op,x,y;
struct node{
int sum,mx,pre,suf;
}t[N<<];
void merge(int o){
t[o].sum=t[lc].sum+t[rc].sum;
t[o].mx=max(t[lc].suf+t[rc].pre,max(t[lc].mx,t[rc].mx));
t[o].pre=max(t[lc].pre,t[lc].sum+t[rc].pre);
t[o].suf=max(t[rc].suf,t[rc].sum+t[lc].suf);
}
void build(int o,int l,int r){
if(l==r) t[o].sum=t[o].mx=t[o].pre=t[o].suf=read();
else{
build(lson);
build(rson);
merge(o);
}
}
int qpre(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].pre;
else if(qr<=m) return qpre(lson,ql,qr);
else return max(qpre(lson,ql,qr),t[lc].sum+qpre(rson,ql,qr));
}
int qsuf(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].suf;
else if(m<ql) return qsuf(rson,ql,qr);
else return max(t[rc].suf,t[rc].sum+qsuf(lson,ql,qr));
}
int qmx(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return t[o].mx;
else{
int ans=-INF;
if(ql<=m) ans=max(ans,qmx(lson,ql,qr));
if(m<qr) ans=max(ans,qmx(rson,ql,qr));
if(ql<=m&&m<qr) ans=max(ans,qsuf(lson,ql,qr)+qpre(rson,ql,qr));
return ans;
}
}
void update(int o,int l,int r,int p,int v){
if(l==r) t[o].sum=t[o].mx=t[o].pre=t[o].suf=v;
else{
if(p<=m) update(lson,p,v);
else update(rson,p,v);
merge(o);
}
}
int main(){
n=read();
build(,,n);
q=read();
for(int i=;i<=q;i++){
op=read();x=read();y=read();
if(op) printf("%d\n",qmx(,,n,x,y));
else update(,,n,x,y);
}
}
 
上一篇:使用微信 SDK 上传图片到七牛


下一篇:使用 EPPlus 封装的 excel 表格导入功能 (二) delegate 委托 --永远滴神