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