This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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) {
nxt[i] = min(j, nxt[i]);
upd(a[i], nxt[i] - i);
i = nxt[i];
}
}
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 + 1);
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |