#include <bits/stdc++.h>
using namespace std;
int n, q, arr[202020];
int ans[1010101];
int res[202020];
int qnum = 0;
struct query {
int time, idx, ord;
bool operator<(const query &other) const {
if (time == other.time) return idx < other.idx;
return time > other.time;
}
} queries[1010101];
struct segment {
int first, st, ed, sz;
bool operator<(const segment &other) const {
return first > other.first;
}
};
set<segment> segs;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= q; i++) {
cin >> queries[i].time >> queries[i].idx;
queries[i].ord = i;
}
sort(queries + 1, queries + q + 1);
while (1) {
if (queries[qnum].time > 0) break;
ans[queries[qnum].ord] = arr[queries[qnum].idx];
qnum++;
}
int v = arr[1], s = 1;
for (int i = 2; i <= n/2; i++) {
if (v < arr[i]) {
segs.insert({v, s, i - 1, i - s});
v = arr[i];
s = i;
}
}
segs.insert({v, s, n/2, n/2 - s + 1});
v = arr[n/2 + 1], s = n/2 + 1;
for (int i = n/2 + 2; i <= n; i++) {
if (v < arr[i]) {
segs.insert({v, s, i - 1, i - s});
v = arr[i];
s = i;
}
}
segs.insert({v, s, n, n - s + 1});
int cnt = 0, p = n;
for (int t = 1; t <= queries[q].time; t++) {
if (queries[qnum].time == t) {
for (set<segment>::iterator iter = segs.begin(); iter != segs.end(); iter++) {
for (int i = iter->ed; i >= iter->st; i--) {
res[p--] = arr[i];
}
}
}
while (1) {
if (queries[qnum].time != t) break;
ans[queries[qnum].ord] = res[queries[qnum].idx];
qnum++;
}
while (1) {
if (cnt + segs.begin()->sz > n/2) break;
cnt += segs.begin()->sz;
for (int i = segs.begin()->ed; i >= segs.begin()->st; i--) {
res[p--] = arr[i];
}
segs.erase(segs.begin());
}
segment div = *segs.begin();
segs.erase(segs.begin());
segs.insert({div.first, div.st, div.st + cnt + div.sz - n/2 - 1, cnt + div.sz - n/2});
s = div.st + cnt + div.sz - n/2; v = arr[s];
for (int i = s; i <= div.ed; i++) {
if (v < arr[i]) {
segs.insert({v, s, i - 1, i - s});
v = arr[i];
s = i;
}
}
segs.insert({v, s, div.ed, div.ed - s + 1});
}
for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
325 ms |
14068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2768 ms |
41784 KB |
Output is correct |
2 |
Correct |
388 ms |
46840 KB |
Output is correct |
3 |
Correct |
1981 ms |
35672 KB |
Output is correct |
4 |
Correct |
303 ms |
32152 KB |
Output is correct |
5 |
Correct |
318 ms |
33728 KB |
Output is correct |
6 |
Correct |
294 ms |
30916 KB |
Output is correct |
7 |
Correct |
353 ms |
38672 KB |
Output is correct |
8 |
Correct |
363 ms |
37464 KB |
Output is correct |
9 |
Correct |
333 ms |
32944 KB |
Output is correct |
10 |
Correct |
389 ms |
34996 KB |
Output is correct |
11 |
Correct |
296 ms |
28700 KB |
Output is correct |
12 |
Correct |
268 ms |
28912 KB |
Output is correct |
13 |
Correct |
320 ms |
34132 KB |
Output is correct |
14 |
Correct |
338 ms |
29788 KB |
Output is correct |
15 |
Correct |
315 ms |
35788 KB |
Output is correct |
16 |
Correct |
18 ms |
3148 KB |
Output is correct |
17 |
Correct |
289 ms |
43408 KB |
Output is correct |
18 |
Correct |
244 ms |
26716 KB |
Output is correct |
19 |
Correct |
78 ms |
8264 KB |
Output is correct |
20 |
Execution timed out |
3055 ms |
8364 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
3668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
325 ms |
14068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |