Guest User

FREQUENT VALUES

a guest
Apr 28th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4.  
  5. import static java.lang.Integer.max;
  6.  
  7. class FrequentValues_FastIO {
  8.  
  9.     private static int a[] = new int[100001];
  10.     private static int tree[] = new int[200005];
  11.     private static Map<Integer, Integer> firstOccurence = new HashMap<>();
  12.     private static Map<Integer, Integer> lastOccurence = new HashMap<>();
  13.  
  14.     private static Map<Integer, Integer> freq = new HashMap<>();
  15.     public static void main(String[] args) throws IOException {
  16.         Reader reader = new Reader();
  17.         int n, q;
  18.  
  19.         while (true) {
  20.             n = reader.nextInt();
  21.             if (n == 0) {
  22.                 break;
  23.             }
  24.             q = reader.nextInt();
  25.             for (int i = 0; i < n; i++) {
  26.                 a[i] = reader.nextInt();
  27.                 freq.putIfAbsent(a[i], 0);
  28.                 freq.compute(a[i], (k, v) -> ++v);
  29.             }
  30.             for (int i = 0; i < n; i++) {
  31.                 lastOccurence.put(a[i], i);
  32.                 firstOccurence.put(a[n - i - 1], n - i - 1);
  33.             }
  34.             constructTree(freq, n);
  35.             for (int i = 0; i < q; i++) {
  36.  
  37.                 int u = reader.nextInt();
  38.                 int v = reader.nextInt();
  39.                 --u;
  40.                 --v;
  41.                 System.out.println(maxOccurenceInRange(a, n, u, v));
  42.             }
  43.             freq.clear();
  44.         }
  45.     }
  46.  
  47.     private static int maxOccurenceInRange(int[] a, int n, int u, int v) {
  48.  
  49.         int newU = u;
  50.         int newV = v;
  51.         if (a[u] == a[v]) {
  52.             return v - u + 1;
  53.         }
  54.  
  55.         if (u > 0 && a[u] == a[u - 1]) {
  56.             newU = lastOccurence.get(a[u]) + 1;
  57.         }
  58.  
  59.         if (v < n && a[v] == a[v + 1]) {
  60.             newV = firstOccurence.get(a[v]) - 1;
  61.         }
  62.  
  63.         return max(max(lastOccurence.get(a[u]) - u + 1, v - firstOccurence.get(a[v]) + 1),
  64.                 query(newU, newV, n));
  65.     }
  66.  
  67.     private static int query(int u, int v, int n) {
  68.         u += n;
  69.         v += n;
  70.         int ans = 0;
  71.         for (; u <= v; u >>= 1, v >>= 1) {
  72.             if ((u & 1) == 1) {
  73.                 ans = max(ans, tree[u--]);
  74.             }
  75.             if ((v & 1) != 1) {
  76.                 ans = max(ans, tree[v--]);
  77.             }
  78.         }
  79.         return ans;
  80.     }
  81.     private static void constructTree(Map<Integer, Integer> freq, int n) {
  82.         for (int i = 0; i < n; i++) {
  83.             tree[n + i] = freq.get(a[i]);
  84.         }
  85.         for (int i = n - 1; i > 0; i--) {
  86.             tree[i] = max(tree[i << 1], tree[(i << 1) + 1]);
  87.         }
  88.     }
  89.  
  90.     static class Reader
  91.     {
  92.         final private int BUFFER_SIZE = 1 << 16;
  93.         private DataInputStream din;
  94.         private byte[] buffer;
  95.         private int bufferPointer, bytesRead;
  96.  
  97.         public Reader()
  98.         {
  99.             din = new DataInputStream(System.in);
  100.             buffer = new byte[BUFFER_SIZE];
  101.             bufferPointer = bytesRead = 0;
  102.         }
  103.  
  104.         public Reader(String file_name) throws IOException
  105.         {
  106.             din = new DataInputStream(new FileInputStream(file_name));
  107.             buffer = new byte[BUFFER_SIZE];
  108.             bufferPointer = bytesRead = 0;
  109.         }
  110.  
  111.         public String readLine() throws IOException
  112.         {
  113.             byte[] buf = new byte[64]; // line length
  114.             int cnt = 0, c;
  115.             while ((c = read()) != -1)
  116.             {
  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.         {
  126.             int ret = 0;
  127.             byte c = read();
  128.             while (c <= ' ')
  129.                 c = read();
  130.             boolean neg = (c == '-');
  131.             if (neg)
  132.                 c = read();
  133.             do
  134.             {
  135.                 ret = ret * 10 + c - '0';
  136.             }  while ((c = read()) >= '0' && c <= '9');
  137.  
  138.             if (neg)
  139.                 return -ret;
  140.             return ret;
  141.         }
  142.  
  143.         public long nextLong() throws IOException
  144.         {
  145.             long ret = 0;
  146.             byte c = read();
  147.             while (c <= ' ')
  148.                 c = read();
  149.             boolean neg = (c == '-');
  150.             if (neg)
  151.                 c = read();
  152.             do {
  153.                 ret = ret * 10 + c - '0';
  154.             }
  155.             while ((c = read()) >= '0' && c <= '9');
  156.             if (neg)
  157.                 return -ret;
  158.             return ret;
  159.         }
  160.  
  161.         public double nextDouble() throws IOException
  162.         {
  163.             double ret = 0, div = 1;
  164.             byte c = read();
  165.             while (c <= ' ')
  166.                 c = read();
  167.             boolean neg = (c == '-');
  168.             if (neg)
  169.                 c = read();
  170.  
  171.             do {
  172.                 ret = ret * 10 + c - '0';
  173.             }
  174.             while ((c = read()) >= '0' && c <= '9');
  175.  
  176.             if (c == '.')
  177.             {
  178.                 while ((c = read()) >= '0' && c <= '9')
  179.                 {
  180.                     ret += (c - '0') / (div *= 10);
  181.                 }
  182.             }
  183.  
  184.             if (neg)
  185.                 return -ret;
  186.             return ret;
  187.         }
  188.  
  189.         private void fillBuffer() throws IOException
  190.         {
  191.             bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  192.             if (bytesRead == -1)
  193.                 buffer[0] = -1;
  194.         }
  195.  
  196.         private byte read() throws IOException
  197.         {
  198.             if (bufferPointer == bytesRead)
  199.                 fillBuffer();
  200.             return buffer[bufferPointer++];
  201.         }
  202.  
  203.         public void close() throws IOException
  204.         {
  205.             if (din == null)
  206.                 return;
  207.             din.close();
  208.         }
  209.     }
  210. }
Add Comment
Please, Sign In to add comment