#include <bits/stdc++.h>
using namespace std;
struct fenwick {
int size, logn;
vector<int> fenw;
fenwick(int n) : size(n), logn(__lg(n)), fenw(n + 1) {}
void add(int i, int v) {
for (i++; i <= size; i += i & -i) {
fenw[i] += v;
}
}
int query(int r) {
int ans = 0;
for (; r > 0; r -= r & -r) {
ans += fenw[r];
}
return ans;
}
int lower_bound(int s) {
int pos = 0;
for (int delta = 1 << logn; delta > 0; delta /= 2) {
if (pos + delta <= size && fenw[pos + delta] < s) {
pos += delta;
s -= fenw[pos];
}
}
return pos;
}
};
void solve() {
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int &v : a) {
cin >> v;
v--;
}
int X = n;
vector<vector<pair<int, int>>> queries(X);
for (int i = 0; i < q; i++) {
int t, p;
cin >> t >> p;
p--, t = min(t, X - 1);
queries[t].emplace_back(i, p);
}
vector<int> nxt(n);
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()] < a[i]) {
st.pop();
}
nxt[i] = st.empty() ? n : st.top();
st.push(i);
}
vector<int> answer(q);
for (auto [i, p] : queries[0]) {
answer[i] = a[p];
}
fenwick fenw(n);
vector<int> l(n), r(n);
int pos = 0;
while (pos != n) {
int go = nxt[pos];
if (pos < n / 2 && go > n / 2) {
go = n / 2;
}
l[a[pos]] = pos;
r[a[pos]] = go;
fenw.add(a[pos], go - pos);
pos = go;
}
for (int t = 1; t < X; t++) {
for (auto [i, p] : queries[t]) {
int j = fenw.lower_bound(p + 1);
int sum = fenw.query(j + 1);
answer[i] = a[r[j] - (sum - p)];
}
int j = fenw.lower_bound(n / 2);
int sum = fenw.query(j + 1);
if (sum != 0) {
int first = r[j] - (sum - n / 2), right = r[j];
fenw.add(j, first - r[j]);
r[j] = first;
while (first != right) {
int go = min(right, nxt[first]);
l[a[first]] = first;
r[a[first]] = go;
fenw.add(a[first], go - first);
first = go;
}
}
}
for (int v : answer) {
cout << 1 + v << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
26060 KB |
Output is correct |
2 |
Correct |
272 ms |
24732 KB |
Output is correct |
3 |
Correct |
247 ms |
24056 KB |
Output is correct |
4 |
Correct |
204 ms |
21924 KB |
Output is correct |
5 |
Correct |
217 ms |
26068 KB |
Output is correct |
6 |
Correct |
210 ms |
25880 KB |
Output is correct |
7 |
Correct |
238 ms |
27252 KB |
Output is correct |
8 |
Correct |
206 ms |
24524 KB |
Output is correct |
9 |
Correct |
207 ms |
23600 KB |
Output is correct |
10 |
Correct |
211 ms |
23156 KB |
Output is correct |
11 |
Correct |
228 ms |
23528 KB |
Output is correct |
12 |
Correct |
200 ms |
20980 KB |
Output is correct |
13 |
Correct |
219 ms |
22480 KB |
Output is correct |
14 |
Correct |
252 ms |
25532 KB |
Output is correct |
15 |
Correct |
222 ms |
23292 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
182 ms |
21136 KB |
Output is correct |
18 |
Correct |
207 ms |
21172 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
324 ms |
41456 KB |
Output is correct |
2 |
Correct |
325 ms |
41084 KB |
Output is correct |
3 |
Correct |
248 ms |
33804 KB |
Output is correct |
4 |
Correct |
259 ms |
33624 KB |
Output is correct |
5 |
Correct |
249 ms |
34496 KB |
Output is correct |
6 |
Correct |
267 ms |
33084 KB |
Output is correct |
7 |
Correct |
311 ms |
40356 KB |
Output is correct |
8 |
Correct |
293 ms |
38596 KB |
Output is correct |
9 |
Correct |
279 ms |
34332 KB |
Output is correct |
10 |
Correct |
286 ms |
37544 KB |
Output is correct |
11 |
Correct |
227 ms |
32556 KB |
Output is correct |
12 |
Correct |
239 ms |
32196 KB |
Output is correct |
13 |
Correct |
272 ms |
36912 KB |
Output is correct |
14 |
Correct |
250 ms |
33364 KB |
Output is correct |
15 |
Correct |
300 ms |
38532 KB |
Output is correct |
16 |
Correct |
27 ms |
10064 KB |
Output is correct |
17 |
Correct |
229 ms |
35956 KB |
Output is correct |
18 |
Correct |
204 ms |
30208 KB |
Output is correct |
19 |
Correct |
68 ms |
15172 KB |
Output is correct |
20 |
Correct |
95 ms |
16480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
9368 KB |
Output is correct |
2 |
Correct |
59 ms |
9144 KB |
Output is correct |
3 |
Correct |
62 ms |
8920 KB |
Output is correct |
4 |
Correct |
44 ms |
8472 KB |
Output is correct |
5 |
Correct |
51 ms |
8968 KB |
Output is correct |
6 |
Correct |
44 ms |
8396 KB |
Output is correct |
7 |
Correct |
48 ms |
8900 KB |
Output is correct |
8 |
Correct |
49 ms |
8452 KB |
Output is correct |
9 |
Correct |
48 ms |
8708 KB |
Output is correct |
10 |
Correct |
38 ms |
8076 KB |
Output is correct |
11 |
Correct |
41 ms |
8396 KB |
Output is correct |
12 |
Correct |
39 ms |
8140 KB |
Output is correct |
13 |
Correct |
41 ms |
8028 KB |
Output is correct |
14 |
Correct |
44 ms |
8312 KB |
Output is correct |
15 |
Correct |
38 ms |
8120 KB |
Output is correct |
16 |
Correct |
14 ms |
5076 KB |
Output is correct |
17 |
Correct |
36 ms |
7752 KB |
Output is correct |
18 |
Correct |
32 ms |
7600 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
26060 KB |
Output is correct |
2 |
Correct |
272 ms |
24732 KB |
Output is correct |
3 |
Correct |
247 ms |
24056 KB |
Output is correct |
4 |
Correct |
204 ms |
21924 KB |
Output is correct |
5 |
Correct |
217 ms |
26068 KB |
Output is correct |
6 |
Correct |
210 ms |
25880 KB |
Output is correct |
7 |
Correct |
238 ms |
27252 KB |
Output is correct |
8 |
Correct |
206 ms |
24524 KB |
Output is correct |
9 |
Correct |
207 ms |
23600 KB |
Output is correct |
10 |
Correct |
211 ms |
23156 KB |
Output is correct |
11 |
Correct |
228 ms |
23528 KB |
Output is correct |
12 |
Correct |
200 ms |
20980 KB |
Output is correct |
13 |
Correct |
219 ms |
22480 KB |
Output is correct |
14 |
Correct |
252 ms |
25532 KB |
Output is correct |
15 |
Correct |
222 ms |
23292 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
182 ms |
21136 KB |
Output is correct |
18 |
Correct |
207 ms |
21172 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
324 ms |
41456 KB |
Output is correct |
22 |
Correct |
325 ms |
41084 KB |
Output is correct |
23 |
Correct |
248 ms |
33804 KB |
Output is correct |
24 |
Correct |
259 ms |
33624 KB |
Output is correct |
25 |
Correct |
249 ms |
34496 KB |
Output is correct |
26 |
Correct |
267 ms |
33084 KB |
Output is correct |
27 |
Correct |
311 ms |
40356 KB |
Output is correct |
28 |
Correct |
293 ms |
38596 KB |
Output is correct |
29 |
Correct |
279 ms |
34332 KB |
Output is correct |
30 |
Correct |
286 ms |
37544 KB |
Output is correct |
31 |
Correct |
227 ms |
32556 KB |
Output is correct |
32 |
Correct |
239 ms |
32196 KB |
Output is correct |
33 |
Correct |
272 ms |
36912 KB |
Output is correct |
34 |
Correct |
250 ms |
33364 KB |
Output is correct |
35 |
Correct |
300 ms |
38532 KB |
Output is correct |
36 |
Correct |
27 ms |
10064 KB |
Output is correct |
37 |
Correct |
229 ms |
35956 KB |
Output is correct |
38 |
Correct |
204 ms |
30208 KB |
Output is correct |
39 |
Correct |
68 ms |
15172 KB |
Output is correct |
40 |
Correct |
95 ms |
16480 KB |
Output is correct |
41 |
Correct |
59 ms |
9368 KB |
Output is correct |
42 |
Correct |
59 ms |
9144 KB |
Output is correct |
43 |
Correct |
62 ms |
8920 KB |
Output is correct |
44 |
Correct |
44 ms |
8472 KB |
Output is correct |
45 |
Correct |
51 ms |
8968 KB |
Output is correct |
46 |
Correct |
44 ms |
8396 KB |
Output is correct |
47 |
Correct |
48 ms |
8900 KB |
Output is correct |
48 |
Correct |
49 ms |
8452 KB |
Output is correct |
49 |
Correct |
48 ms |
8708 KB |
Output is correct |
50 |
Correct |
38 ms |
8076 KB |
Output is correct |
51 |
Correct |
41 ms |
8396 KB |
Output is correct |
52 |
Correct |
39 ms |
8140 KB |
Output is correct |
53 |
Correct |
41 ms |
8028 KB |
Output is correct |
54 |
Correct |
44 ms |
8312 KB |
Output is correct |
55 |
Correct |
38 ms |
8120 KB |
Output is correct |
56 |
Correct |
14 ms |
5076 KB |
Output is correct |
57 |
Correct |
36 ms |
7752 KB |
Output is correct |
58 |
Correct |
32 ms |
7600 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
604 ms |
48124 KB |
Output is correct |
62 |
Correct |
533 ms |
46760 KB |
Output is correct |
63 |
Correct |
526 ms |
45136 KB |
Output is correct |
64 |
Correct |
392 ms |
44300 KB |
Output is correct |
65 |
Correct |
424 ms |
46408 KB |
Output is correct |
66 |
Correct |
383 ms |
44204 KB |
Output is correct |
67 |
Correct |
388 ms |
43996 KB |
Output is correct |
68 |
Correct |
390 ms |
42164 KB |
Output is correct |
69 |
Correct |
429 ms |
44460 KB |
Output is correct |
70 |
Correct |
345 ms |
40516 KB |
Output is correct |
71 |
Correct |
302 ms |
42056 KB |
Output is correct |
72 |
Correct |
345 ms |
41004 KB |
Output is correct |
73 |
Correct |
325 ms |
41548 KB |
Output is correct |
74 |
Correct |
343 ms |
43604 KB |
Output is correct |
75 |
Correct |
341 ms |
42160 KB |
Output is correct |
76 |
Correct |
28 ms |
10060 KB |
Output is correct |
77 |
Correct |
236 ms |
36296 KB |
Output is correct |
78 |
Correct |
250 ms |
35316 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
340 KB |
Output is correct |