|
| 1 | +```java |
| 2 | +import java.awt.image.AreaAveragingScaleFilter; |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class IOController { |
| 7 | + BufferedReader br; |
| 8 | + BufferedWriter bw; |
| 9 | + StringTokenizer st; |
| 10 | + |
| 11 | + public IOController() { |
| 12 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 14 | + st = new StringTokenizer(""); |
| 15 | + } |
| 16 | + |
| 17 | + String nextLine() throws Exception { |
| 18 | + String line = br.readLine(); |
| 19 | + st = new StringTokenizer(line); |
| 20 | + return line; |
| 21 | + } |
| 22 | + |
| 23 | + String nextToken() throws Exception { |
| 24 | + while (!st.hasMoreTokens()) nextLine(); |
| 25 | + return st.nextToken(); |
| 26 | + } |
| 27 | + |
| 28 | + int nextInt() throws Exception { |
| 29 | + return Integer.parseInt(nextToken()); |
| 30 | + } |
| 31 | + |
| 32 | + long nextLong() throws Exception { |
| 33 | + return Long.parseLong(nextToken()); |
| 34 | + } |
| 35 | + |
| 36 | + double nextDouble() throws Exception { |
| 37 | + return Double.parseDouble(nextToken()); |
| 38 | + } |
| 39 | + |
| 40 | + void close() throws Exception { |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + } |
| 44 | + |
| 45 | + void write(String content) throws Exception { |
| 46 | + bw.write(content); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +public class Main { |
| 52 | + |
| 53 | + static IOController io; |
| 54 | + |
| 55 | + // |
| 56 | + |
| 57 | + static int N, M; |
| 58 | + static int sqrt; |
| 59 | + static int[] a; |
| 60 | + static int[][] queries; |
| 61 | + |
| 62 | + public static void main(String[] args) throws Exception { |
| 63 | + |
| 64 | + io = new IOController(); |
| 65 | + |
| 66 | + init(); |
| 67 | + solve(); |
| 68 | + |
| 69 | + io.close(); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + static void init() throws Exception { |
| 74 | + |
| 75 | + N = io.nextInt(); |
| 76 | + a = new int[N]; |
| 77 | + for(int i=0;i<N;i++) a[i] = io.nextInt(); |
| 78 | + sqrt = (int)Math.sqrt(N); |
| 79 | + |
| 80 | + M = io.nextInt(); |
| 81 | + queries = new int[M][]; |
| 82 | + for(int i=0;i<M;i++) queries[i] = new int[]{io.nextInt()-1,io.nextInt()-1,i}; |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + static void solve() throws Exception { |
| 87 | + |
| 88 | + Arrays.sort(queries, (a,b) -> { |
| 89 | + int s = a[0] / sqrt, e = b[0] / sqrt; |
| 90 | + if(s == e) return a[1]-b[1]; |
| 91 | + return s-e; |
| 92 | + }); |
| 93 | + |
| 94 | + int[] cnt = new int[100001]; |
| 95 | + int[] ccnt = new int[100001]; |
| 96 | + ccnt[0] = 1; |
| 97 | + int[] ans = new int[M]; |
| 98 | + int s = queries[0][0], e = queries[0][1], max = 0; |
| 99 | + for(int i=s;i<=e;i++) { |
| 100 | + ccnt[cnt[a[i]]]--; |
| 101 | + if(ccnt[max] == 0) max--; |
| 102 | + ccnt[++cnt[a[i]]]++; |
| 103 | + if(cnt[a[i]] > max) max = cnt[a[i]]; |
| 104 | + } |
| 105 | + ans[queries[0][2]] = max; |
| 106 | + |
| 107 | + for(int i=1;i<M;i++) { |
| 108 | + int ns = queries[i][0], ne = queries[i][1], idx = queries[i][2]; |
| 109 | + if(ns > e) { |
| 110 | + for(int k=s;k<=e;k++) { |
| 111 | + ccnt[cnt[a[k]]]--; |
| 112 | + if(ccnt[max] == 0) max--; |
| 113 | + ccnt[--cnt[a[k]]]++; |
| 114 | + if(cnt[a[k]] > max) max = cnt[a[k]]; |
| 115 | + } |
| 116 | + s = ns; |
| 117 | + e = ns; |
| 118 | + ccnt[cnt[a[s]]]--; |
| 119 | + if(ccnt[max] == 0) max--; |
| 120 | + ccnt[++cnt[a[s]]]++; |
| 121 | + if(cnt[a[s]] > max) max = cnt[a[s]]; |
| 122 | + } |
| 123 | + while(s < ns) { |
| 124 | + ccnt[cnt[a[s]]]--; |
| 125 | + if(ccnt[max] == 0) max--; |
| 126 | + ccnt[--cnt[a[s]]]++; |
| 127 | + if(cnt[a[s]] > max) max = cnt[a[s]]; |
| 128 | + s++; |
| 129 | + } |
| 130 | + while(ns < s) { |
| 131 | + s--; |
| 132 | + ccnt[cnt[a[s]]]--; |
| 133 | + if(ccnt[max] == 0) max--; |
| 134 | + ccnt[++cnt[a[s]]]++; |
| 135 | + if(cnt[a[s]] > max) max = cnt[a[s]]; |
| 136 | + } |
| 137 | + while(e < ne) { |
| 138 | + e++; |
| 139 | + ccnt[cnt[a[e]]]--; |
| 140 | + if(ccnt[max] == 0) max--; |
| 141 | + ccnt[++cnt[a[e]]]++; |
| 142 | + if(cnt[a[e]] > max) max = cnt[a[e]]; |
| 143 | + } |
| 144 | + while(ne < e) { |
| 145 | + ccnt[cnt[a[e]]]--; |
| 146 | + if(ccnt[max] == 0) max--; |
| 147 | + ccnt[--cnt[a[e]]]++; |
| 148 | + if(cnt[a[e]] > max) max = cnt[a[e]]; |
| 149 | + e--; |
| 150 | + } |
| 151 | + ans[idx] = max; |
| 152 | + s = ns; |
| 153 | + e = ne; |
| 154 | + } |
| 155 | + |
| 156 | + for(int v:ans) io.write(v + "\n"); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | +} |
| 161 | +``` |
0 commit comments