fork(1) download
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. class utkarsh {
  6.  
  7.  
  8. class node{
  9. int SUM, P, M, S, B;
  10. node(int h){
  11. SUM = P = M = S = B = h;
  12. }
  13. }
  14.  
  15. int a[], n;
  16. node tree[];
  17.  
  18. int max(int x, int y){
  19. return x > y ? x : y;
  20. }
  21.  
  22. void build(int N, int s, int e){
  23. if(s == e){
  24. tree[N] = new node(a[s]);
  25. return;
  26. }
  27. int mid, lc, rc;
  28. mid = s + (e - s) / 2;
  29. lc = 2 * N + 1;
  30. rc = 2 * N + 2;
  31. build(lc, s, mid);
  32. build(rc, mid + 1, e);
  33. tree[N] = new node(0);
  34. tree[N].SUM = tree[lc].SUM + tree[rc].SUM;
  35. tree[N].P = max(tree[lc].P, tree[lc].SUM + tree[rc].P);
  36. tree[N].M = tree[lc].S + tree[rc].P;
  37. tree[N].S = max(tree[rc].S, tree[rc].SUM + tree[lc].S);
  38. tree[N].B = max(tree[N].SUM, max(tree[N].P, max(tree[N].M, max(tree[N].S, max(tree[lc].B, tree[rc].B)))));
  39. }
  40.  
  41. node query(int N, int s, int e, int l, int r){
  42. if(s > r || l > e){
  43. return new node(-200100);
  44. }
  45. if(s >= l && e <= r){
  46. return tree[N];
  47. }
  48. int mid = s + (e - s) / 2;
  49. if(r <= mid){
  50. return query(2*N + 1, s, mid, l, r);
  51. }else if(l > mid){
  52. return query(2*N + 2, mid + 1, e, l, r);
  53. }else{
  54. node LC, RC, disha;
  55. LC = query(2*N + 1, s, mid, l, r);
  56. RC = query(2*N + 2, mid + 1, e, l, r);
  57. disha = new node(0);
  58. disha.SUM = LC.SUM + RC.SUM;
  59. disha.P = max(LC.P, LC.SUM + RC.P);
  60. disha.M = LC.S + RC.P;
  61. disha.S = max(RC.S, RC.SUM + LC.S);
  62. disha.B = max(disha.SUM, max(disha.P, max(disha.M, max(disha.S, max(LC.B, RC.B)))));
  63. return disha;
  64. }
  65. }
  66.  
  67. void solve(){
  68. //Enter code here utkarsh
  69. int i, q;
  70. n = ni();
  71. a = new int[n];
  72. tree = new node[4 * n];
  73. for(i = 0; i < n; i++){
  74. a[i] = ni();
  75. }
  76. build(0, 0, n-1);
  77. q = ni();
  78. while(q-- > 0){
  79. out.println(query(0, 0, n-1, ni() - 1, ni() - 1).B);
  80. }
  81. }
  82.  
  83. public static void main(String[] args) { new utkarsh().run();
  84. }
  85. void run(){ is = System.in; out = new PrintWriter(System.out); solve(); out.flush();
  86. }
  87.  
  88. byte input[] = new byte[1024];
  89. int len = 0, ptr = 0;
  90.  
  91. int readByte(){ if(ptr >= len){ ptr = 0; try{ len = is.read(input); }catch(IOException e){ throw new InputMismatchException(); } if(len <= 0){ return -1; } } return input[ptr++];
  92. }
  93. boolean isSpaceChar(int c){ return !( c >= 33 && c <= 126 );
  94. }
  95. int skip(){ int b = readByte(); while(b != -1 && isSpaceChar(b)){ b = readByte(); } return b;
  96. }
  97. int ni(){ int n = 0,b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')){ b = readByte(); } if(b == '-'){ minus = true; b = readByte(); } if(b == -1){ return -1; } while(b >= '0' && b <= '9'){ n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n;
  98. }
  99. long nl(){ long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')){ b = readByte(); } if(b == '-'){ minus = true; b = readByte(); } while(b >= '0' && b <= '9'){ n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n;
  100. }
  101. }
Success #stdin #stdout 0.04s 4386816KB
stdin
3 
-1 2 3
1
1 2
stdout
2