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;
}
};
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] + 1;
int pos = 0, sum = 0;
for (int i = 17; i >= 0; i--) {
if (pos + (1 << i) <= N && bit[pos + (1 << i)] + sum < p) {
pos += 1 << i;
sum += bit[pos];
}
}
pos++;
ans[k] = A[L[pos] - sum + p - 1];
}
}
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... |