GSS3 - Can you answer these queries III
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
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int maxn=50010; 6 const int INF=1000000000; 7 int n,Q,a[maxn]; 8 9 struct Node{ 10 int lx,rx,mx,sum; 11 Node(int _lx=-INF,int _rx=-INF,int _mx=-INF){ 12 lx=_lx;rx=_rx;mx=_mx; 13 } 14 }T[maxn<<2]; 15 16 void Push_up(Node &x,Node l,Node r){ 17 x.sum=l.sum+r.sum; 18 x.lx=max(l.lx,l.sum+r.lx); 19 x.rx=max(r.rx,r.sum+l.rx); 20 x.mx=max(max(l.mx,r.mx),l.rx+r.lx); 21 } 22 23 void Build(int x,int l,int r){ 24 if(l==r){ 25 T[x].lx=T[x].rx=max(a[l],0); 26 T[x].mx=T[x].sum=a[l]; 27 return; 28 } 29 int mid=(l+r)>>1; 30 Build(x<<1,l,mid); 31 Build(x<<1|1,mid+1,r); 32 Push_up(T[x],T[x<<1],T[x<<1|1]); 33 } 34 35 void Modify(int x,int l,int r,int g){ 36 if(l==r){ 37 T[x].lx=T[x].rx=max(a[l],0); 38 T[x].mx=T[x].sum=a[l]; 39 return; 40 } 41 int mid=(l+r)>>1; 42 if(mid>=g)Modify(x<<1,l,mid,g); 43 else Modify(x<<1|1,mid+1,r,g); 44 Push_up(T[x],T[x<<1],T[x<<1|1]); 45 } 46 47 Node Query(int x,int l,int r,int a,int b){ 48 if(l>=a&&r<=b) 49 return T[x]; 50 int mid=(l+r)>>1; 51 Node L,R,ret; 52 if(mid>=a)L=Query(x<<1,l,mid,a,b); 53 if(mid<b)R=Query(x<<1|1,mid+1,r,a,b); 54 Push_up(ret,L,R); 55 return ret; 56 } 57 58 int main(){ 59 scanf("%d",&n); 60 for(int i=1;i<=n;i++) 61 scanf("%d",&a[i]); 62 Build(1,1,n); 63 scanf("%d",&Q); 64 int tp,x,y; 65 while(Q--){ 66 scanf("%d%d%d",&tp,&x,&y); 67 if(!tp) 68 a[x]=y,Modify(1,1,n,x); 69 else 70 printf("%d\n",Query(1,1,n,x,y).mx); 71 } 72 return 0; 73 }
水题。
时间: 2024-10-13 02:59:50