#include <bits/stdc++.h>
using namespace std;
const int LOG = 20;
struct Fenwick {
int n;
vector<int> bit;
vector<int> a;
Fenwick(int n) : n(n), bit(n + 1, 0), a(n, 0) {}
void add(int i, int delta) {
a[i] += delta;
for (i++; i <= n; i += i & -i) {
bit[i] += delta;
}
}
int sum(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
int get(int i) {
return a[i];
}
int kth(int k) {
int sum = 0;
int cur = 0;
for (int i = LOG - 1; i >= 0; i--) {
if (cur + (1 << i) <= n && sum + bit[cur + (1 << i)] <= k) {
cur += (1 << i);
sum += bit[cur];
}
}
return cur;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n), ia(n);
vector<vector<array<int, 2>>> mp(n + 1);
vector<int> ans(q);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
ia[a[i]] = i;
}
for (int id = 0; id < q; id++) {
int t, i;
cin >> t >> i;
i--;
t = min(t, n);
mp[t].push_back({ i, id });
}
Fenwick fen(n);
for (int i = 0; i < n; ) {
int anc = i;
while (i < n && a[anc] >= a[i]) {
i++;
}
int len = i - anc;
fen.add(a[anc], len);
}
function<void()> cut = [&]() {
int head = fen.kth(n / 2);
int pos = fen.sum(head);
int fromLen = fen.get(head);
int toLen = n / 2 - pos;
int newHead = a[ia[head] + toLen];
int newLen = fromLen - toLen;
fen.add(head, -fromLen + toLen);
fen.add(newHead, newLen);
};
function<int(int)> kth = [&](int k) {
int head = fen.kth(k);
int pos = fen.sum(head);
int toLen = k - pos;
return a[ia[head] + toLen];
};
for (int t = 0; t <= n; t++) {
for (auto [i, id] : mp[t]) {
ans[id] = kth(i);
}
cut();
}
for (auto x : ans) {
cout << x + 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
200 ms |
18524 KB |
Output is correct |
2 |
Correct |
209 ms |
17232 KB |
Output is correct |
3 |
Correct |
193 ms |
16796 KB |
Output is correct |
4 |
Correct |
181 ms |
15808 KB |
Output is correct |
5 |
Correct |
196 ms |
18868 KB |
Output is correct |
6 |
Correct |
211 ms |
19728 KB |
Output is correct |
7 |
Incorrect |
201 ms |
19892 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
40892 KB |
Output is correct |
2 |
Correct |
280 ms |
39864 KB |
Output is correct |
3 |
Correct |
225 ms |
34676 KB |
Output is correct |
4 |
Correct |
241 ms |
33000 KB |
Output is correct |
5 |
Correct |
227 ms |
34948 KB |
Output is correct |
6 |
Correct |
211 ms |
32448 KB |
Output is correct |
7 |
Incorrect |
261 ms |
39712 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
9044 KB |
Output is correct |
2 |
Correct |
52 ms |
8456 KB |
Output is correct |
3 |
Correct |
49 ms |
8528 KB |
Output is correct |
4 |
Correct |
40 ms |
8152 KB |
Output is correct |
5 |
Correct |
44 ms |
8792 KB |
Output is correct |
6 |
Correct |
42 ms |
8140 KB |
Output is correct |
7 |
Incorrect |
50 ms |
8528 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
200 ms |
18524 KB |
Output is correct |
2 |
Correct |
209 ms |
17232 KB |
Output is correct |
3 |
Correct |
193 ms |
16796 KB |
Output is correct |
4 |
Correct |
181 ms |
15808 KB |
Output is correct |
5 |
Correct |
196 ms |
18868 KB |
Output is correct |
6 |
Correct |
211 ms |
19728 KB |
Output is correct |
7 |
Incorrect |
201 ms |
19892 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |