fork download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. class GSS3 {
  7. static int array[];
  8.  
  9. public static void main(String[] args) throws IOException {
  10. // TODO Auto-generated method stub
  11. GSS3.Reader r = new GSS3.Reader();
  12. int N = r.nextInt();
  13. array = new int[N + 1];
  14. for (int i = 1; i <= N; i++) {
  15. array[i] = r.nextInt();
  16. }
  17. SegmentTree st = new SegmentTree(1, N);
  18. int Q = r.nextInt();
  19. StringBuilder sb = new StringBuilder();
  20. for (int i = 0; i < Q; i++) {
  21. int choice = r.nextInt();
  22. int left = r.nextInt();
  23. int right = r.nextInt();
  24. if (choice == 1) {
  25. sb.append(st.query(left, right).bestsum + "\n");
  26. } else {
  27. st.update(left, right);
  28. }
  29. }
  30. pr.write(sb.toString());
  31. pr.flush();
  32. }
  33.  
  34. static class SegmentTree {
  35. SegmentTree left_child, right_child;
  36. int bestleftsum, bestrightsum, bestsum, sum, start, end;
  37.  
  38. public SegmentTree() {
  39. }
  40.  
  41. public SegmentTree(int a, int b) {
  42. start = a;
  43. end = b;
  44. if (start != end) {
  45. int mid = (start + end) >> 1;
  46. left_child = new SegmentTree(start, mid);
  47. right_child = new SegmentTree(mid + 1, end);
  48. join(this, left_child, right_child);
  49. } else {
  50. bestleftsum = bestrightsum = bestsum = sum = GSS3.array[start];
  51. }
  52. }
  53.  
  54. public void join(SegmentTree parent, SegmentTree left_child, SegmentTree right_child) {
  55. parent.sum = left_child.sum + right_child.sum;
  56. parent.bestleftsum = Math.max(left_child.bestleftsum, left_child.sum + right_child.bestleftsum);
  57. parent.bestrightsum = Math.max(right_child.bestrightsum, right_child.sum + left_child.bestrightsum);
  58. parent.bestsum = Math.max(
  59. left_child.bestleftsum,
  60. Math.max(
  61. right_child.bestrightsum,
  62. Math.max(left_child.bestrightsum, Math.max(right_child.bestleftsum, left_child.bestrightsum
  63. + right_child.bestleftsum))));
  64. }
  65.  
  66. public SegmentTree query(int a, int b) {
  67. if (a == start && b == end)
  68. return this;
  69. int mid = (start + end) >> 1;
  70. if (a > mid)
  71. return right_child.query(a, b);
  72. else if (b <= mid)
  73. return left_child.query(a, b);
  74. SegmentTree st = new SegmentTree();
  75. join(st, left_child.query(a, mid), right_child.query(mid + 1, b));
  76. return st;
  77. }
  78.  
  79. public void update(int index, int value) {
  80. if (index == start && index == end) {
  81. bestleftsum = bestrightsum = bestsum = sum = value;
  82. return;
  83. }
  84. int mid = (start + end) >> 1;
  85. if (index <= mid)
  86. left_child.update(index, value);
  87. else
  88. right_child.update(index, value);
  89. join(this, left_child, right_child);
  90. }
  91. }
  92.  
  93. static class Reader {
  94. final private int BUFFER_SIZE = 1 << 16;
  95. private DataInputStream din;
  96. private byte[] buffer;
  97. private int bufferPointer, bytesRead;
  98.  
  99. public Reader() {
  100. din = new DataInputStream(System.in);
  101. buffer = new byte[BUFFER_SIZE];
  102. bufferPointer = bytesRead = 0;
  103. }
  104.  
  105. public Reader(String file_name) throws IOException {
  106. din = new DataInputStream(new FileInputStream(file_name));
  107. buffer = new byte[BUFFER_SIZE];
  108. bufferPointer = bytesRead = 0;
  109. }
  110.  
  111. public String readLine() throws IOException {
  112. byte[] buf = new byte[64];
  113. int cnt = 0, c;
  114. while ((c = read()) != -1) {
  115. if (c == '\n')
  116. break;
  117. buf[cnt++] = (byte) c;
  118. }
  119. return new String(buf, 0, cnt);
  120. }
  121.  
  122. public int nextInt() throws IOException {
  123. int ret = 0;
  124. byte c = read();
  125. while (c <= ' ')
  126. c = read();
  127. boolean neg = (c == '-');
  128. if (neg)
  129. c = read();
  130. do {
  131. ret = ret * 10 + c - '0';
  132. } while ((c = read()) >= '0' && c <= '9');
  133. if (neg)
  134. return -ret;
  135. return ret;
  136. }
  137.  
  138. public long nextLong() throws IOException {
  139. long ret = 0;
  140. byte c = read();
  141. while (c <= ' ')
  142. c = read();
  143. boolean neg = (c == '-');
  144. if (neg)
  145. c = read();
  146. do {
  147. ret = ret * 10 + c - '0';
  148. } while ((c = read()) >= '0' && c <= '9');
  149. if (neg)
  150. return -ret;
  151. return ret;
  152. }
  153.  
  154. public double nextDouble() throws IOException {
  155. double ret = 0, div = 1;
  156. byte c = read();
  157. while (c <= ' ')
  158. c = read();
  159. boolean neg = (c == '-');
  160. if (neg)
  161. c = read();
  162. do {
  163. ret = ret * 10 + c - '0';
  164. } while ((c = read()) >= '0' && c <= '9');
  165. if (c == '.')
  166. while ((c = read()) >= '0' && c <= '9')
  167. ret += (c - '0') / (div *= 10);
  168. if (neg)
  169. return -ret;
  170. return ret;
  171. }
  172.  
  173. private void fillBuffer() throws IOException {
  174. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  175. if (bytesRead == -1)
  176. buffer[0] = -1;
  177. }
  178.  
  179. private byte read() throws IOException {
  180. if (bufferPointer == bytesRead)
  181. fillBuffer();
  182. return buffer[bufferPointer++];
  183. }
  184.  
  185. public void close() throws IOException {
  186. if (din == null)
  187. return;
  188. din.close();
  189. }
  190. }
  191. }
  192. /*
  193.  * 4 1 2 3 4 4 1 1 3 0 3 -3 1 2 4 1 3 3
  194.  */
  195. /*
  196.  * 10 -200 3 4 -200 6 2 4 -200 5 6 4 1 1 10 0 1 -100 1 1 5 1 1 10
  197.  */
Success #stdin #stdout 0.07s 380160KB
stdin
10
-200 3 4 -200 6 2 4 -200 5 6
4
1 1 10
0 1 100
1 1 5
1 1 10
stdout
12
107
107