#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];
struct segtree {
int leaf[N];
int n;
void init(int _n) {
n = _n;
for (int i = 0; i < n; i++) {
leaf[i] = 0;
}
}
void upd(int i, int x) {
leaf[i] = x;
}
array<int, 2> get(int p) {
for (int i = 0; i < n; i++) {
if (p < leaf[i]) {
return {i, p};
} else {
p -= leaf[i];
}
}
}
int get2(int p) {
return leaf[p];
}
} seg;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
int pos[n + 1];
for (int i = 0; i < n; i++) {
cin >> a[i];
pos[a[i]] = 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;
seg.init(n + 1);
for (int i = 0; i < n; i = nxt[i]) {
seg.upd(a[i], nxt[i] - i);
s.insert({a[i], i});
}
bool isover = 0;
for (int j = 0; j <= n; j++) {
for (auto [p, idx] : ask[j]) {
auto [i, v] = seg.get(p);
ans[idx] = a[pos[i] + v];
}
if (isover) continue;
int cur = n / 2;
array<int, 2> V = seg.get(cur);
int i = pos[V[0]];
cur = V[1];
if (cur == 0) {
isover = 1;
continue;
}
int mx = i + seg.get2(a[i]);
seg.upd(a[i], cur);
cur = i + cur;
while (cur < n && seg.get2(a[cur]) == 0) {
int newcur = min(nxt[cur], mx);
seg.upd(a[cur], newcur - cur);
cur = newcur;
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In member function 'std::array<int, 2> segtree::get(int)':
Main.cpp:28:2: warning: control reaches end of non-void function [-Wreturn-type]
28 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
468 ms |
24660 KB |
Output is correct |
2 |
Correct |
421 ms |
23440 KB |
Output is correct |
3 |
Correct |
423 ms |
22988 KB |
Output is correct |
4 |
Correct |
621 ms |
22752 KB |
Output is correct |
5 |
Correct |
643 ms |
25052 KB |
Output is correct |
6 |
Correct |
596 ms |
25688 KB |
Output is correct |
7 |
Correct |
625 ms |
25844 KB |
Output is correct |
8 |
Correct |
583 ms |
24284 KB |
Output is correct |
9 |
Correct |
596 ms |
23288 KB |
Output is correct |
10 |
Correct |
597 ms |
22868 KB |
Output is correct |
11 |
Correct |
614 ms |
23584 KB |
Output is correct |
12 |
Correct |
589 ms |
21656 KB |
Output is correct |
13 |
Correct |
598 ms |
22392 KB |
Output is correct |
14 |
Correct |
617 ms |
24404 KB |
Output is correct |
15 |
Correct |
629 ms |
22768 KB |
Output is correct |
16 |
Correct |
2 ms |
8280 KB |
Output is correct |
17 |
Correct |
379 ms |
21824 KB |
Output is correct |
18 |
Correct |
548 ms |
21688 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3073 ms |
17964 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3046 ms |
11404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
468 ms |
24660 KB |
Output is correct |
2 |
Correct |
421 ms |
23440 KB |
Output is correct |
3 |
Correct |
423 ms |
22988 KB |
Output is correct |
4 |
Correct |
621 ms |
22752 KB |
Output is correct |
5 |
Correct |
643 ms |
25052 KB |
Output is correct |
6 |
Correct |
596 ms |
25688 KB |
Output is correct |
7 |
Correct |
625 ms |
25844 KB |
Output is correct |
8 |
Correct |
583 ms |
24284 KB |
Output is correct |
9 |
Correct |
596 ms |
23288 KB |
Output is correct |
10 |
Correct |
597 ms |
22868 KB |
Output is correct |
11 |
Correct |
614 ms |
23584 KB |
Output is correct |
12 |
Correct |
589 ms |
21656 KB |
Output is correct |
13 |
Correct |
598 ms |
22392 KB |
Output is correct |
14 |
Correct |
617 ms |
24404 KB |
Output is correct |
15 |
Correct |
629 ms |
22768 KB |
Output is correct |
16 |
Correct |
2 ms |
8280 KB |
Output is correct |
17 |
Correct |
379 ms |
21824 KB |
Output is correct |
18 |
Correct |
548 ms |
21688 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Execution timed out |
3073 ms |
17964 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |