fork download
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.PrintWriter;
  4. import java.util.InputMismatchException;
  5.  
  6. class FREQUENT {
  7. static int n;
  8.  
  9. public static void main(String[] args) {
  10. InputReader s = new InputReader(System.in);
  11. n = s.nextInt();
  12. while (true) {
  13. int q = s.nextInt();
  14. long[] a = new long[n];
  15. long count = 0, num = Integer.MAX_VALUE;
  16. for (int i = 0; i < n; i++) {
  17. a[i] = s.nextLong();
  18. if (a[i] != num) {
  19. count++;
  20. num = a[i];
  21. }
  22. }
  23. num = a[0];
  24. long[] left = new long[(int) count];
  25. long[] right = new long[(int) count];
  26. long[] pre_tree = new long[(int) count];
  27. left[0] = 0;
  28. int index = 0;
  29. count = 1;
  30. for (int i = 0; i < n - 1; i++) {
  31. if (a[i] == a[i + 1])
  32. count++;
  33. else {
  34. // count++;
  35. pre_tree[index] = count;
  36. count = 1;
  37. right[index] = i;
  38. left[++index] = i + 1;
  39. }
  40.  
  41. }
  42. pre_tree[index] = count;
  43.  
  44. count = 1;
  45. right[index] = a.length - 1;
  46. long tree[];
  47. int temp = pre_tree.length;
  48. if ((temp & (temp - 1)) == 0)
  49. tree = new long[2 * temp - 1];
  50. else {
  51. temp = (int) (Math.log(temp) / Math.log(2));
  52. temp++;
  53. temp = 1 << temp;
  54. tree = new long[2 * temp - 1];
  55.  
  56. }
  57. for (int i = 0; i < tree.length; i++) {
  58. tree[i] = (Integer.MIN_VALUE);
  59. }
  60. make_tree(pre_tree, tree, 0, pre_tree.length - 1, 0);
  61. while (q-- > 0) {
  62. long l = s.nextLong(), r = s.nextLong();
  63. l--;
  64. r--;
  65. long ans = 0;
  66. int ql = binarySearch(left, l);
  67. int qr = binarySearch(right, r);
  68. if (qr == -1) {
  69. ans = r - l + 1;
  70. } else if (left[ql] != l && right[qr] != r) {
  71.  
  72. long max1 = l - right[ql] + 1, max3 = r - left[qr + 1] + 1;
  73. long max2 = query(tree, ql + 1, qr, 0, pre_tree.length - 1, 0);
  74. ans = Math.max(Math.max(max1, max2), max3);
  75.  
  76. } else if (left[ql] == l && right[qr] != r) {
  77. long max1 = query(tree, ql, qr, 0, pre_tree.length - 1, 0);
  78. long max2 = r - left[qr + 1] + 1;
  79. ans = Math.max(max1, max2);
  80. } else if (left[ql] != l && right[qr] == r) {
  81. long max1 = query(tree, ql + 1, qr, 0, pre_tree.length - 1, 0);
  82. long max2 = right[ql] - l + 1;
  83. ans = Math.max(max1, max2);
  84. } else {
  85. ans = query(tree, ql, qr, 0, pre_tree.length - 1, 0);
  86. }
  87. w.println(ans);
  88. }
  89. n = s.nextInt();
  90. if (n == 0)
  91. break;
  92. }
  93. w.close();
  94.  
  95. }
  96.  
  97. static int binarySearch(long a[], long key) {
  98. int low = 0, high = a.length - 1;
  99. while (low <= high) {
  100. int mid = (low + high) / 2;
  101. if (a[mid] == key) {
  102. high = mid;
  103. break;
  104. } else if (a[mid] > key)
  105. high = mid - 1;
  106. else
  107. low = mid + 1;
  108. }
  109. return high;
  110.  
  111. }
  112.  
  113. static void make_tree(long[] a, long[] tree, int low, int high, int pos) {
  114.  
  115. if (low == high) {
  116. tree[pos] = a[low];
  117. return;
  118. }
  119. int mid = (low + high) >> 1;
  120. make_tree(a, tree, low, mid, 2 * pos + 1);
  121. make_tree(a, tree, mid + 1, high, 2 * pos + 2);
  122. if (tree[2 * pos + 1] > tree[2 * pos + 2])
  123. tree[pos] = tree[2 * pos + 1];
  124. else
  125. tree[pos] = tree[2 * pos + 2];
  126. }
  127.  
  128. static long query(long[] tree, long qlow, long qhigh, long low, long high, long pos) {
  129. if (qlow <= low && qhigh >= high)
  130. return tree[(int) pos];
  131. if (qlow > high || qhigh < low) {
  132. return (Integer.MIN_VALUE);
  133. }
  134. long mid = (low + high) >> 1;
  135. long q1 = query(tree, qlow, qhigh, low, mid, 2 * pos + 1);
  136. long q2 = query(tree, qlow, qhigh, mid + 1, high, 2 * pos + 2);
  137. return Math.max(q1, q2);
  138.  
  139. }
  140.  
  141. }
  142.  
  143. class InputReader {
  144.  
  145. private final InputStream st;
  146. private final byte[] buf = new byte[8192];
  147. private int cc, sc;
  148. private SpaceCharFilter f;
  149.  
  150. public InputReader(InputStream st) {
  151. this.st = st;
  152. }
  153.  
  154. public int t() {
  155. if (sc == -1)
  156. throw new InputMismatchException();
  157. if (cc >= sc) {
  158. cc = 0;
  159. try {
  160. sc = st.read(buf);
  161. } catch (IOException e) {
  162. throw new InputMismatchException();
  163. }
  164. if (sc <= 0)
  165. return -1;
  166. }
  167. return buf[cc++];
  168. }
  169.  
  170. public int nextInt() {
  171. int c = t();
  172. while (isSpaceChar(c)) {
  173. c = t();
  174. }
  175. int sgn = 1;
  176. if (c == '-') {
  177. sgn = -1;
  178. c = t();
  179. }
  180. int res = 0;
  181. do {
  182. res *= 10;
  183. res += c - '0';
  184. c = t();
  185. } while (!isSpaceChar(c));
  186. return res * sgn;
  187. }
  188.  
  189. public long nextLong() {
  190. int c = t();
  191. while (isSpaceChar(c)) {
  192. c = t();
  193. }
  194. int sgn = 1;
  195. if (c == '-') {
  196. sgn = -1;
  197. c = t();
  198. }
  199. long res = 0;
  200. do {
  201. res *= 10;
  202. res += c - '0';
  203. c = t();
  204. } while (!isSpaceChar(c));
  205. return res * sgn;
  206. }
  207.  
  208. public int[] nextIntArray(int n) {
  209. int a[] = new int[n];
  210. for (int i = 0; i < n; i++) {
  211. a[i] = nextInt();
  212. }
  213. return a;
  214. }
  215.  
  216. public String readString() {
  217. int c = t();
  218. while (isSpaceChar(c)) {
  219. c = t();
  220. }
  221. StringBuilder res = new StringBuilder();
  222. do {
  223. res.appendCodePoint(c);
  224. c = t();
  225. } while (!isSpaceChar(c));
  226. return res.toString();
  227. }
  228.  
  229. public String nextLine() {
  230. int c = t();
  231. while (isSpaceChar(c))
  232. c = t();
  233. StringBuilder res = new StringBuilder();
  234. do {
  235. res.appendCodePoint(c);
  236. c = t();
  237. } while (!isEndOfLine(c));
  238. return res.toString();
  239. }
  240.  
  241. public boolean isSpaceChar(int c) {
  242. if (f != null)
  243. return f.isSpaceChar(c);
  244. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  245. }
  246.  
  247. private boolean isEndOfLine(int c) {
  248. return c == '\n' || c == '\r' || c == -1;
  249. }
  250.  
  251. public interface SpaceCharFilter {
  252. public boolean isSpaceChar(int ch);
  253. }
  254. }
Runtime error #stdin #stdout #stderr 0.04s 4386816KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.InputMismatchException
	at InputReader.t(Main.java:157)
	at InputReader.nextInt(Main.java:174)
	at FREQUENT.main(Main.java:12)