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;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> A(N);
for (int &x : A) {
cin >> x;
}
vector<int> queries(Q), ans(Q);
vector<vector<int>> q(N + 1);
for (int i = 0; i < Q; i++) {
int t, p;
cin >> t >> p;
t = min(t, N);
p--;
queries[i] = p;
if (t == 0) {
ans[i] = A[p];
} else {
q[t].push_back(i);
}
}
vector<int> f(N, N), stk;
for (int i = N - 1; i >= 0; i--) {
while (!stk.empty() && A[stk.back()] < A[i]) {
stk.pop_back();
}
if (!stk.empty()) {
f[i] = stk.back();
}
stk.push_back(i);
}
vector<int> bit(N + 1);
set<tuple<int, int, int>> s, t;
for (int i = 0; i < N; i = f[i]) {
s.emplace(A[i], i, f[i]);
bit[A[i]] += f[i] - i;
}
int T = N;
for (int i = 1; i <= N; i++) {
if (T > N / 2) {
while (1) {
auto [_, l, r] = *prev(s.end());
if (T - r + l < N / 2) {
break;
}
T -= r - l;
t.insert(*prev(s.end()));
s.erase(prev(s.end()));
}
if (T > N / 2) {
auto [v, l, r] = *prev(s.end());
s.erase(prev(s.end()));
bit[v] -= r - l;
int m = r - T + N / 2;
s.emplace(v, l, m);
bit[v] += m - l;
for (int i = m; i < r; i = f[i]) {
s.emplace(A[i], i, min(r, f[i]));
bit[A[i]] += min(r, f[i]) - i;
}
}
}
for (int k : q[i]) {
int p = queries[k];
int l = 1, r = N;
while (l < r) {
int m = (l + r) / 2, s = 0;
for (int j = 1; j <= m; j++) {
s += bit[j];
}
if (s > p) {
r = m;
} else {
l = m + 1;
}
}
int a = 0;
for (int j = 1; j <= l; j++) {
a += bit[j];
}
auto [v, L, R] = *s.lower_bound({l, 0, 0});
if (v == l) {
ans[k] = A[R - a + p];
} else {
auto [v, L, R] = *t.lower_bound({l, 0, 0});
ans[k] = A[R - a + p];
}
}
}
for (int x : ans) {
cout << x << "\n";
}
return 6/22;
}
# | 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... |