http://www.spoj.com/problems/GSS3/

My Code
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 #define lson l,m,rt<<1
 5 #define rson m+1,r,rt<<1|1
 6 #define maxn 50001
 7 struct node
 8 {
 9     int lmax,rmax,max,sum;
10 }setree[maxn<<2];
11 int ans;
12 void pushup(int rt)
13 {
14     setree[rt].lmax=max(setree[rt<<1].lmax,setree[rt<<1].sum+setree[rt<<1|1].lmax);
15     setree[rt].rmax=max(setree[rt<<1|1].rmax,setree[rt<<1|1].sum+setree[rt<<1].rmax);
16     setree[rt].sum=setree[rt<<1].sum+setree[rt<<1|1].sum;
17     setree[rt].max=max(setree[rt<<1].max,setree[rt<<1|1].max);
18     setree[rt].max=max(setree[rt].max,setree[rt<<1].rmax+setree[rt<<1|1].lmax);
19 }
20 void build(int l,int r,int rt)
21 {
22     if(l==r){
23         scanf("%d",&setree[rt].sum);
24         setree[rt].lmax=setree[rt].rmax=setree[rt].max=setree[rt].sum;
25         return;
26     }
27     int m=(l+r)>>1;
28     build(lson);
29     build(rson);
30     pushup(rt);
31 }
32 void update(int l,int r,int rt,int num,int c)
33 {
34     if(l==r){
35         setree[rt].lmax=setree[rt].rmax=setree[rt].max=setree[rt].sum=c;
36         return;
37     }
38     int m=(l+r)>>1;
39     if(num<=m)
40     update(lson,num,c);
41     else
42     update(rson,num,c);
43     pushup(rt);
44 }
45 int query(int l,int r,int rt,int flag,int L,int R)
46 {
47     if(L==l&&r==R){
48         ans=max(ans,setree[rt].max);
49         return flag==-1?setree[rt].lmax:setree[rt].rmax;
50     }
51     int m=(l+r)>>1;
52     if(R<=m)
53     return query(lson,-1,L,R);
54     else if(L>m)
55     return query(rson,1,L,R);
56     else{
57         int ln=query(lson,1,L,m);
58         int rn=query(rson,-1,m+1,R);
59         ans=max(ans,ln+rn);
60         if(flag==-1)
61         return max(setree[rt<<1].lmax,setree[rt<<1].sum+rn);
62         else
63         return max(setree[rt<<1|1].rmax,setree[rt<<1|1].sum+ln);
64     }
65 }
66 int main()
67 {
68     int n;
69     while(~scanf("%d",&n)){
70         build(1,n,1);
71         int m;
72         scanf("%d",&m);
73         while(m--){
74             int op,a,b;
75             scanf("%d%d%d",&op,&a,&b);
76             if(op==0)
77             update(1,n,1,a,b);
78             else{
79                 ans=-(1<<30);
80                 query(1,n,1,0,a,b);
81                 printf("%d\n",ans);
82             }
83         }
84     }
85     return 0;
86 }