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. parent.bestsum=Math.max(left_child.bestsum, Math.max(right_child.bestsum,left_child.bestrightsum+right_child.bestleftsum));
  66. }
  67.  
  68. public SegmentTree query(int a, int b) {
  69. if (a == start && b == end)
  70. return this;
  71. int mid = (start + end) >> 1;
  72. if (a > mid)
  73. return right_child.query(a, b);
  74. else if (b <= mid)
  75. return left_child.query(a, b);
  76. SegmentTree st = new SegmentTree();
  77. join(st, left_child.query(a, mid), right_child.query(mid + 1, b));
  78. return st;
  79. }
  80.  
  81. public void update(int index, int value) {
  82. if (index == start && index == end) {
  83. bestleftsum = bestrightsum = bestsum = sum = value;
  84. return;
  85. }
  86. int mid = (start + end) >> 1;
  87. if (index <= mid)
  88. left_child.update(index, value);
  89. else
  90. right_child.update(index, value);
  91. join(this, left_child, right_child);
  92. }
  93. }
  94.  
  95. static class Reader {
  96. final private int BUFFER_SIZE = 1 << 16;
  97. private DataInputStream din;
  98. private byte[] buffer;
  99. private int bufferPointer, bytesRead;
  100.  
  101. public Reader() {
  102. din = new DataInputStream(System.in);
  103. buffer = new byte[BUFFER_SIZE];
  104. bufferPointer = bytesRead = 0;
  105. }
  106.  
  107. public Reader(String file_name) throws IOException {
  108. din = new DataInputStream(new FileInputStream(file_name));
  109. buffer = new byte[BUFFER_SIZE];
  110. bufferPointer = bytesRead = 0;
  111. }
  112.  
  113. public String readLine() throws IOException {
  114. byte[] buf = new byte[64];
  115. int cnt = 0, c;
  116. while ((c = read()) != -1) {
  117. if (c == '\n')
  118. break;
  119. buf[cnt++] = (byte) c;
  120. }
  121. return new String(buf, 0, cnt);
  122. }
  123.  
  124. public int nextInt() throws IOException {
  125. int ret = 0;
  126. byte c = read();
  127. while (c <= ' ')
  128. c = read();
  129. boolean neg = (c == '-');
  130. if (neg)
  131. c = read();
  132. do {
  133. ret = ret * 10 + c - '0';
  134. } while ((c = read()) >= '0' && c <= '9');
  135. if (neg)
  136. return -ret;
  137. return ret;
  138. }
  139.  
  140. public long nextLong() throws IOException {
  141. long ret = 0;
  142. byte c = read();
  143. while (c <= ' ')
  144. c = read();
  145. boolean neg = (c == '-');
  146. if (neg)
  147. c = read();
  148. do {
  149. ret = ret * 10 + c - '0';
  150. } while ((c = read()) >= '0' && c <= '9');
  151. if (neg)
  152. return -ret;
  153. return ret;
  154. }
  155.  
  156. public double nextDouble() throws IOException {
  157. double ret = 0, div = 1;
  158. byte c = read();
  159. while (c <= ' ')
  160. c = read();
  161. boolean neg = (c == '-');
  162. if (neg)
  163. c = read();
  164. do {
  165. ret = ret * 10 + c - '0';
  166. } while ((c = read()) >= '0' && c <= '9');
  167. if (c == '.')
  168. while ((c = read()) >= '0' && c <= '9')
  169. ret += (c - '0') / (div *= 10);
  170. if (neg)
  171. return -ret;
  172. return ret;
  173. }
  174.  
  175. private void fillBuffer() throws IOException {
  176. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  177. if (bytesRead == -1)
  178. buffer[0] = -1;
  179. }
  180.  
  181. private byte read() throws IOException {
  182. if (bufferPointer == bytesRead)
  183. fillBuffer();
  184. return buffer[bufferPointer++];
  185. }
  186.  
  187. public void close() throws IOException {
  188. if (din == null)
  189. return;
  190. din.close();
  191. }
  192. }
  193. }
  194. /*
  195.  * 4 1 2 3 4 4 1 1 3 0 3 -3 1 2 4 1 3 3
  196.  */
  197. /*
  198.  * 10 -200 3 4 -200 6 2 4 -200 5 6 4 1 1 10 0 1 -100 1 1 5 1 1 10
  199.  */
Success #stdin #stdout 0.07s 380160KB
stdin
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3
stdout
6
4
-3