库存
GSS1
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 inline int read() { 6 int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; 7 for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; 8 } 9 10 const int N = 100100; 11 struct Seg{ 12 int S,LS,RS,sum; 13 }T[N<<2]; 14 15 #define Root 1,n,1 16 #define lson l,mid,rt<<1 17 #define rson mid+1,r,rt<<1|1 18 19 void pushup(int rt) { 20 T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum; 21 T[rt].LS = max(T[rt<<1].LS,T[rt<<1].sum+T[rt<<1|1].LS); 22 T[rt].RS = max(T[rt<<1|1].RS,T[rt<<1|1].sum+T[rt<<1].RS); 23 T[rt].S = max(T[rt<<1].RS+T[rt<<1|1].LS,max(T[rt<<1].S,T[rt<<1|1].S)); 24 } 25 void build(int l,int r,int rt) { 26 if (l == r) { 27 T[rt].S = T[rt].LS = T[rt].RS = T[rt].sum = read(); 28 return ; 29 } 30 int mid = (l + r) >> 1; 31 build(lson); 32 build(rson); 33 pushup(rt); 34 } 35 Seg query(int l,int r,int rt,int L,int R) { 36 if (L <= l && r <= R) { 37 return T[rt]; 38 } 39 int mid = (l + r) >> 1; 40 if (L <= mid && R > mid) { 41 Seg res,ll,rr; 42 ll = query(lson,L,R);rr = query(rson,L,R); 43 res.sum = ll.sum + rr.sum; 44 res.LS = max(ll.LS,ll.sum+rr.LS); 45 res.RS = max(rr.RS,rr.sum+ll.RS); 46 res.S = max(ll.RS+rr.LS,max(ll.S,rr.S)); 47 return res; 48 } 49 else if (L <= mid) return query(lson,L,R); 50 else if (R > mid) return query(rson,L,R); 51 } 52 53 int main() { 54 int n = read(); 55 build(Root); 56 int m = read(); 57 while (m--) { 58 int l = read(),r = read(); 59 Seg ans = query(Root,l,r); 60 printf("%d\n",ans.S); 61 } 62 return 0; 63 }