fork(1) download
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.StringTokenizer;
  4. public class Main {
  5. private int[] array;
  6. private NodeI[] heap;
  7. int size;
  8.  
  9. public static void main(String args[]) throws IOException {
  10. MyScanner sc = new MyScanner();
  11. out = new PrintWriter(new BufferedOutputStream(System.out), true);
  12.  
  13. int i = 0, N, Q, l, r;
  14. long answer;
  15. N = sc.nextInt();
  16. int[] array = new int[N];
  17. while (i < N) {
  18. array[i++] = sc.nextInt();
  19. }
  20. Main gss1 = new Main(array);
  21. Q = sc.nextInt();
  22. i = 0;
  23. while (i++ < Q) {
  24. l = sc.nextInt();
  25. r = sc.nextInt();
  26. answer = gss1.query(1, l - 1, r - 1).getMS();
  27. out.println(answer + "\n");
  28. }
  29.  
  30. }
  31. public static PrintWriter out;
  32. public static class MyScanner {
  33.  
  34. public MyScanner() {
  35. }
  36.  
  37. String next() {
  38. while (st == null || !st.hasMoreElements()) {
  39. try {
  40. st = new StringTokenizer(br.readLine());
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. return st.nextToken();
  46. }
  47.  
  48. int nextInt() { return Integer.parseInt(next()); }
  49. }
  50.  
  51. class NodeI {
  52. int from, to, len;
  53. long pre, suf, sum, mS;
  54.  
  55. public NodeI() {
  56. }
  57.  
  58. NodeI(int from, int to, int len) {
  59. this.from = from;
  60. this.to = to;
  61. this.len = len;
  62. }
  63.  
  64. NodeI(NodeI node) {
  65. this.from = node.from;
  66. this.to = node.to;
  67. this.len = node.len;
  68. this.pre = node.pre;
  69. this.suf = node.suf;
  70. this.sum = node.sum;
  71. this.mS = node.mS;
  72. }
  73.  
  74. long getMS() { return mS; }
  75.  
  76. void setMS(long mS) { this.mS = mS; }
  77.  
  78. long getSum() {
  79. return sum;
  80. }
  81.  
  82. void setSum(long sum) {
  83. this.sum = sum;
  84. }
  85.  
  86. long getSuf() {
  87. return suf;
  88. }
  89.  
  90. void setSuf(long suf) {
  91. this.suf = suf;
  92. }
  93.  
  94. void setPre(long pre) {
  95. this.pre = pre;
  96. }
  97. }
  98.  
  99. void initLeaf(int n, Integer integer) {
  100. NodeI node = heap[n];
  101. int index = node.from;
  102. int value = array[index];
  103. node.setPre(value);
  104. node.setSuf(value);
  105. node.setSum(value);
  106. node.setMS(value);
  107. }
  108. NodeI balance(NodeI node, NodeI lft, NodeI ryt) {
  109. node.setPre(Math.max(lft.pre, lft.getSum() + ryt.pre));
  110. node.setSuf(Math.max(ryt.getSuf(), ryt.getSum() + lft.getSuf()));
  111. node.setMS(Math.max(Math.max(lft.getMS(), ryt.getMS()), lft.getSuf() + ryt.pre));
  112. node.setSum(lft.getSum() + ryt.getSum());
  113. return node;
  114. }
  115. Main(int[] array) {
  116. this.array = Arrays.copyOf(array, array.length);
  117. this.size = ((int)Math.pow(2.0,Math.floor((Math.log((double)array.length)/Math.log(2.0))+1))<<1);
  118. this.heap = new NodeI[this.size];
  119. build(1, 0, array.length);
  120. }
  121. void build(int n, int from, int size) {
  122. heap[n] = new NodeI(from, from + size - 1, size);
  123. if (size == 1) {
  124. this.initLeaf(n, array[from]);
  125. } else {
  126. build(n<<1, from, size >> 1);
  127. build((n<<1)+1, from + (size >> 1), size - (size >> 1));
  128. blnce(n);
  129. }
  130. }
  131. protected NodeI blnce(int n) {
  132. NodeI node = heap[n];
  133. if (node.from == node.to) {
  134. return node;
  135. }
  136. NodeI lft = heap[n << 1];
  137. NodeI ryt = heap[(n<<1) + 1];
  138. return balance(node, lft, ryt);
  139. }
  140. NodeI query(int n, int l, int r) {
  141. if (l == heap[n].from && r == heap[n].to) {
  142. return heap[n];
  143. }
  144. int rcFrom = heap[n].from + (heap[n].len >> 1);
  145. if (r < rcFrom) {
  146. return query(n << 1, l, r);
  147. }
  148. if (l >= rcFrom) {
  149. return query((n << 1) + 1, l, r);
  150. }
  151. NodeI ln = query(n << 1, l, rcFrom - 1);
  152. NodeI rn = query( (n << 1) + 1, rcFrom, r);
  153. NodeI copyNode = new NodeI();
  154. return balance(copyNode, ln, rn);
  155. }
  156. }
  157.  
  158.  
  159.  
Success #stdin #stdout 0.05s 4575232KB
stdin
3 
-1 2 3
1
1 2
stdout
2