#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 200'005;
vector<array<int, 2>> ask[N];
int a[N], ans[1'000'005];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < q; i++) {
int t, p;
cin >> t >> p;
t = min(t, n);
ask[t].push_back({p - 1, i});
}
int nxt[n]{};
stack<int> st;
a[n] = 1e9;
st.push(n);
for (int i = n - 1; i >= 0; i--) {
while (a[i] > a[st.top()]) st.pop();
nxt[i] = st.top();
st.push(i);
}
set<array<int, 2>> s;
int cnt[n]{};
for (int i = 0; i < n; i = nxt[i]) {
cnt[i] = nxt[i] - i;
s.insert({a[i], i});
}
bool isover = 0;
for (int j = 0; j <= n; j++) {
// for (auto [v, i] : s) {
// cout << cnt[i] << ' ';
// }
// cout << '\n';
for (auto [p, idx] : ask[j]) {
for (auto [v, i] : s) {
if (p < cnt[i]) {
ans[idx] = a[i + p];
break;
} else {
p -= cnt[i];
}
}
}
if (isover) continue;
int cur = n / 2;
for (auto [v, i] : s) {
if (cur < cnt[i]) {
if (cur == 0) {
isover = 1;
break;
}
int mx = i + cnt[i];
cnt[i] = cur;
cur = i + cur;
while (cur < n && cnt[cur] == 0) {
int newcur = min(nxt[cur], mx);
cnt[cur] = newcur - cur;
s.insert({a[cur], cur});
cur = newcur;
}
break;
} else {
cur -= cnt[i];
}
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
933 ms |
24148 KB |
Output is correct |
2 |
Correct |
1860 ms |
30108 KB |
Output is correct |
3 |
Correct |
1770 ms |
29364 KB |
Output is correct |
4 |
Correct |
202 ms |
28368 KB |
Output is correct |
5 |
Correct |
430 ms |
31280 KB |
Output is correct |
6 |
Correct |
302 ms |
31312 KB |
Output is correct |
7 |
Correct |
574 ms |
32256 KB |
Output is correct |
8 |
Correct |
411 ms |
30104 KB |
Output is correct |
9 |
Correct |
360 ms |
29032 KB |
Output is correct |
10 |
Correct |
402 ms |
28760 KB |
Output is correct |
11 |
Correct |
342 ms |
29284 KB |
Output is correct |
12 |
Correct |
211 ms |
26612 KB |
Output is correct |
13 |
Correct |
293 ms |
28020 KB |
Output is correct |
14 |
Correct |
461 ms |
30544 KB |
Output is correct |
15 |
Correct |
279 ms |
28472 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
1947 ms |
26764 KB |
Output is correct |
18 |
Correct |
147 ms |
27460 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3100 ms |
23572 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3045 ms |
16120 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
933 ms |
24148 KB |
Output is correct |
2 |
Correct |
1860 ms |
30108 KB |
Output is correct |
3 |
Correct |
1770 ms |
29364 KB |
Output is correct |
4 |
Correct |
202 ms |
28368 KB |
Output is correct |
5 |
Correct |
430 ms |
31280 KB |
Output is correct |
6 |
Correct |
302 ms |
31312 KB |
Output is correct |
7 |
Correct |
574 ms |
32256 KB |
Output is correct |
8 |
Correct |
411 ms |
30104 KB |
Output is correct |
9 |
Correct |
360 ms |
29032 KB |
Output is correct |
10 |
Correct |
402 ms |
28760 KB |
Output is correct |
11 |
Correct |
342 ms |
29284 KB |
Output is correct |
12 |
Correct |
211 ms |
26612 KB |
Output is correct |
13 |
Correct |
293 ms |
28020 KB |
Output is correct |
14 |
Correct |
461 ms |
30544 KB |
Output is correct |
15 |
Correct |
279 ms |
28472 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
1947 ms |
26764 KB |
Output is correct |
18 |
Correct |
147 ms |
27460 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Execution timed out |
3100 ms |
23572 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |