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