#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2539 ms |
24780 KB |
Output is correct |
2 |
Correct |
2455 ms |
23820 KB |
Output is correct |
3 |
Correct |
2294 ms |
23280 KB |
Output is correct |
4 |
Execution timed out |
3069 ms |
18580 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3048 ms |
34252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3061 ms |
8676 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2539 ms |
24780 KB |
Output is correct |
2 |
Correct |
2455 ms |
23820 KB |
Output is correct |
3 |
Correct |
2294 ms |
23280 KB |
Output is correct |
4 |
Execution timed out |
3069 ms |
18580 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |