Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. import java.io.OutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.PrintWriter;
  6. import java.util.Arrays;
  7. import java.io.BufferedWriter;
  8. import java.io.Writer;
  9. import java.io.OutputStreamWriter;
  10. import java.util.InputMismatchException;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13.  
  14. /**
  15. * Built using CHelper plug-in
  16. * Actual solution is at the top
  17. */
  18. public class Main {
  19. public static void main(String[] args) {
  20. InputStream inputStream = System.in;
  21. OutputStream outputStream = System.out;
  22. InputReader in = new InputReader(inputStream);
  23. OutputWriter out = new OutputWriter(outputStream);
  24. GCDQuery solver = new GCDQuery();
  25. solver.solve(1, in, out);
  26. out.close();
  27. }
  28.  
  29. static class GCDQuery {
  30. int[][] ans;
  31. int[] last;
  32.  
  33. void q(int div, int idx) {
  34. if (idx != last[div] && last[div] != -1)
  35. ans[last[div]][idx] = Math.max(ans[last[div]][idx], div);
  36. last[div] = idx;
  37. }
  38.  
  39. public void solve(int testNumber, InputReader in, OutputWriter out) {
  40. int n = in.nextInt(), q = in.nextInt();
  41. int[] arr = in.readIntArray(n);
  42. ans = new int[n][n];
  43. last = new int[100000001];
  44. Arrays.fill(last, -1);
  45. for (int i = 0; i < n; i++) {
  46. for (int div = 1; div * div <= arr[i]; div++) {
  47. if (arr[i] % div == 0) {
  48. q(div, i);
  49. q(arr[i] / div, i);
  50. }
  51. }
  52. }
  53. for (int i = n - 1; i >= 0; i--) {
  54. for (int j = i + 1; j < n; j++) {
  55. ans[i][j] = AUtils.max(ans[i][j - 1], ans[i + 1][j], ans[i][j]);
  56. }
  57. }
  58. while (q-- > 0) {
  59. int l = in.nextInt() - 1, r = in.nextInt() - 1;
  60. out.println(ans[l][r]);
  61. }
  62.  
  63. }
  64.  
  65. }
  66.  
  67. static class InputReader {
  68. private InputStream stream;
  69. private byte[] buf = new byte[1 << 16];
  70. private int curChar;
  71. private int numChars;
  72.  
  73. public InputReader(InputStream stream) {
  74. this.stream = stream;
  75. }
  76.  
  77. public int[] readIntArray(int tokens) {
  78. int[] ret = new int[tokens];
  79. for (int i = 0; i < tokens; i++) {
  80. ret[i] = nextInt();
  81. }
  82. return ret;
  83. }
  84.  
  85. public int read() {
  86. if (this.numChars == -1) {
  87. throw new InputMismatchException();
  88. } else {
  89. if (this.curChar >= this.numChars) {
  90. this.curChar = 0;
  91.  
  92. try {
  93. this.numChars = this.stream.read(this.buf);
  94. } catch (IOException var2) {
  95. throw new InputMismatchException();
  96. }
  97.  
  98. if (this.numChars <= 0) {
  99. return -1;
  100. }
  101. }
  102.  
  103. return this.buf[this.curChar++];
  104. }
  105. }
  106.  
  107. public int nextInt() {
  108. int c;
  109. for (c = this.read(); isSpaceChar(c); c = this.read()) {
  110. ;
  111. }
  112.  
  113. byte sgn = 1;
  114. if (c == 45) {
  115. sgn = -1;
  116. c = this.read();
  117. }
  118.  
  119. int res = 0;
  120.  
  121. while (c >= 48 && c <= 57) {
  122. res *= 10;
  123. res += c - 48;
  124. c = this.read();
  125. if (isSpaceChar(c)) {
  126. return res * sgn;
  127. }
  128. }
  129.  
  130. throw new InputMismatchException();
  131. }
  132.  
  133. public static boolean isSpaceChar(int c) {
  134. return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
  135. }
  136.  
  137. }
  138.  
  139. static class OutputWriter {
  140. private final PrintWriter writer;
  141.  
  142. public OutputWriter(OutputStream outputStream) {
  143. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  144. }
  145.  
  146. public OutputWriter(Writer writer) {
  147. this.writer = new PrintWriter(writer);
  148. }
  149.  
  150. public void close() {
  151. writer.close();
  152. }
  153.  
  154. public void println(int i) {
  155. writer.println(i);
  156. }
  157.  
  158. }
  159.  
  160. static class AUtils {
  161. public static <E extends Comparable<E>> E max(E... arr) {
  162. E res = arr[0];
  163. for (E x : arr) if (x.compareTo(res) > 0) res = x;
  164. return res;
  165. }
  166.  
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement