#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, Q = 1e6 + 5;
int n, q;
int a[N], nxt[N], pos[N], s[4 * N], res[Q];
void upd(int i, int x, int id = 1, int l = 1, int r = n) {
if (l == r) {
s[id] = x;
return;
}
int m = (l + r) / 2;
if (i <= m) {
upd(i, x, id * 2, l, m);
} else {
upd(i, x, id * 2 + 1, m + 1, r);
}
s[id] = s[id * 2] + s[id * 2 + 1];
}
array<int, 2> walk(int k) {
int id = 1, l = 1, r = n;
while (l ^ r) {
int m = (l + r) / 2;
id *= 2;
if (s[id] >= k) {
r = m;
} else {
l = m + 1;
k -= s[id];
id += 1;
}
}
return {k, l};
}
void add(int i, int j) {
while (i < j) {
int k = min(j, nxt[i]);
upd(a[i], k - i);
i = k;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> q;
vector<int> st;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pos[a[i]] = i;
while (st.size() && a[st.back()] < a[i]) {
nxt[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
while (st.size()) {
nxt[st.back()] = n + 1;
st.pop_back();
}
vector<array<int, 3>> queries;
for (int i = 1; i <= q; ++i) {
int t, p; cin >> t >> p;
queries.push_back({min(t, n), p, i});
}
sort(queries.begin(), queries.end());
add(1, n);
bool c = 1;
int m = n / 2;
for (int i = 0, j = 0; i <= n; ++i) {
while (j < q && queries[j][0] == i) {
auto [t, p, id] = queries[j++];
auto [l, x] = walk(p);
res[id] = a[pos[x] + l - 1];
}
if (!c) {
continue;
}
auto [l, x] = walk(m);
if (l == nxt[pos[x]] - pos[x]) {
c = 0;
continue;
}
add(pos[x] + l, nxt[pos[x]]);
nxt[pos[x]] = pos[x] + l;
upd(x, l);
}
for (int i = 1; i <= q; ++i) {
cout << res[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
278 ms |
27812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
337 ms |
42028 KB |
Output is correct |
2 |
Correct |
311 ms |
40492 KB |
Output is correct |
3 |
Correct |
289 ms |
33320 KB |
Output is correct |
4 |
Correct |
293 ms |
32864 KB |
Output is correct |
5 |
Correct |
279 ms |
33476 KB |
Output is correct |
6 |
Correct |
269 ms |
32420 KB |
Output is correct |
7 |
Correct |
298 ms |
40096 KB |
Output is correct |
8 |
Correct |
277 ms |
38572 KB |
Output is correct |
9 |
Correct |
291 ms |
33444 KB |
Output is correct |
10 |
Incorrect |
279 ms |
37536 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
6724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
278 ms |
27812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |