fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. struct segtree{
  5. ll s, bs, bps, bss;
  6. }st[200020];
  7. ll a[50005];
  8. segtree merge(segtree A, segtree B)
  9. {
  10. segtree C;
  11. C.s=A.s+B.s;
  12. C.bps=max(A.bps, A.s+B.bps);
  13. C.bss=max(B.bss, B.s+A.bss);
  14. C.bs=max(max(B.s, A.s), A.bss+B.bps);
  15. return C;
  16. }
  17. void build(ll start, ll end, ll node)
  18. {
  19. if(start==end)
  20. {
  21. st[node].s=st[node].bps=st[node].bss=st[node].bs=a[start];
  22. return ;
  23. }
  24. ll mid=(start+end)/2;
  25. build(start, mid, 2*node);
  26. build(mid+1, end, 2*node+1);
  27. st[node]=merge(st[2*node], st[2*node+1]);
  28. }
  29. segtree query(ll start, ll end, ll l, ll r, ll node)
  30. {
  31. if(start>=l&&end<=r) return st[node];
  32. ll mid=(start+end)/2;
  33. if(mid>=r) return query(start, mid, l, r, 2*node);
  34. else if(mid<l) return query(mid+1, end, l, r, 2*node+1);
  35. else
  36. return merge(query(start, mid, l, r, 2*node), query(mid+1, end, l,r, 2*node+1));
  37. }
  38. int main()
  39. {
  40. ios_base::sync_with_stdio(false);
  41. cin.tie(0);
  42. cout.tie(0);
  43. ll n;
  44. cin>>n;
  45. for(ll i=1;i<=n;i++)
  46. cin>>a[i];
  47. build(1, n, 1);
  48. ll q;
  49. cin>>q;
  50. while(q--)
  51. {
  52. ll x, y;
  53. cin>>x>>y;
  54. segtree ss=query(1, n, x, y, 1);
  55. cout<<ss.bs<<endl;
  56. }
  57. }
Runtime error #stdin #stdout 0s 7744KB
stdin
Standard input is empty
stdout
Standard output is empty