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);
auto add = [&](int p, int x) {
for (int i = p; i <= N; i += i & -i) {
bit[i] += x;
}
};
auto sum = [&](int p) {
int s = 0;
for (int i = p; i; i -= i & -i) {
s += bit[i];
}
return s;
};
vector<int> L(N + 1), R(N + 1);
set<tuple<int, int, int>> s, t;
for (int i = 0; i < N; i = f[i]) {
L[A[i]] = i;
R[A[i]] = f[i];
s.emplace(A[i], L[A[i]], R[A[i]]);
add(A[i], R[A[i]] - L[A[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;
s.erase(prev(s.end()));
}
if (T > N / 2) {
auto [v, l, r] = *prev(s.end());
s.erase(prev(s.end()));
int m = r - T + N / 2;
s.emplace(v, l, m);
add(v, m - r);
R[v] = m;
for (int i = m; i < r; i = f[i]) {
L[A[i]] = i;
R[A[i]] = min(r, f[i]);
s.emplace(A[i], L[A[i]], R[A[i]]);
add(A[i], R[A[i]] - L[A[i]]);
}
}
}
for (int k : q[i]) {
int p = queries[k];
int l = 1, r = N;
while (l < r) {
int m = (l + r) / 2;
if (sum(m) > p) {
r = m;
} else {
l = m + 1;
}
}
ans[k] = A[R[l] - sum(l) + 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... |