-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMO.java
More file actions
140 lines (109 loc) · 3.09 KB
/
MO.java
File metadata and controls
140 lines (109 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class MO {
static int N;
static int votes[];
static Query q[];
static int sqrt;
static int res[];
static int freq[];
static int blocks;
static int sqrtDec[][];
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt(), g;
while(t-- > 0) {
N = sc.nextInt();
sqrt = (int) Math.sqrt(N);
votes = new int[N];
for(int i = 0; i < N; ++i) votes[i] = sc.nextInt();
g = sc.nextInt();
q = new Query[g];
res = new int[g];
freq = new int[N];
blocks = N / sqrt + 1;
sqrtDec = new int[blocks][N + 1];
for(int i = 0; i < g; ++i)
q[i] = new Query(sc.nextInt(), sc.nextInt(), sc.nextInt(), i);
process();
for(int i = 0; i < g; ++i) out.println(res[i]);
}
out.flush();
out.close();
}
private static void process() {
Arrays.sort(q);
int l = 1, r = 0;
for(int i = 0; i < q.length; ++i) {
while(r < q[i].r) add(++r);
while(l < q[i].l) remove(l++);
while(l > q[i].l) add(--l);
while(r > q[i].r) remove(r--);
res[q[i].id] = getAns(q[i].x);
}
}
static int getAns(int x) {
int ans = -1;
for(int i = 0; i < sqrtDec.length; ++i) {
if(sqrtDec[i][x] > 0) {
for(int j = sqrt * i; j < sqrt * i + sqrt; ++j) {
if(freq[j] == x)
return j;
}
}
}
return ans;
}
static int getBlock(int idx) {
return idx / sqrt;
}
static void add(int idx) {
freq[votes[idx]]++;
sqrtDec[getBlock(votes[idx])][freq[votes[idx]] - 1]--;
sqrtDec[getBlock(votes[idx])][freq[votes[idx]]]++;
}
static void remove(int idx) {
freq[votes[idx]]--;
sqrtDec[getBlock(votes[idx])][freq[votes[idx]] + 1]--;
sqrtDec[getBlock(votes[idx])][freq[votes[idx]]]++;
}
static class Query implements Comparable<Query> {
int l, r, x, id;
public Query(int a, int b, int c, int d) {
l = a;
r = b;
x = c;
id = d;
}
@Override
public int compareTo(Query q) {
if(Integer.compare(l / sqrt, q.l / sqrt) == 0)
return Integer.compare(r, q.r);
return Integer.compare(l / sqrt, q.l / sqrt);
}
}
static class Scanner{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public Scanner(FileReader r){ br = new BufferedReader(r);}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException { return Double.parseDouble(next()); }
public boolean ready() throws IOException {return br.ready();}
}
}