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;
#define ll long long
const int N = 200'005;
vector<array<int, 2>> ask[N];
int a[N], ans[1'000'005];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < q; i++) {
int t, p;
cin >> t >> p;
t = min(t, n);
ask[t].push_back({p - 1, i});
}
int nxt[n]{};
stack<int> st;
a[n] = 1e9;
st.push(n);
for (int i = n - 1; i >= 0; i--) {
while (a[i] > a[st.top()]) st.pop();
nxt[i] = st.top();
st.push(i);
}
set<array<int, 2>> s;
int cnt[n]{};
for (int i = 0; i < n; i = nxt[i]) {
cnt[i] = nxt[i] - i;
s.insert({a[i], i});
}
bool isover = 0;
for (int j = 0; j <= n; j++) {
for (auto [p, idx] : ask[j]) {
for (auto [v, i] : s) {
if (p < cnt[i]) {
ans[idx] = a[i + p];
break;
} else {
p -= cnt[i];
}
}
}
if (isover) continue;
int cur = n / 2;
for (auto [v, i] : s) {
if (cur < cnt[i]) {
if (cur == 0) {
isover = 1;
break;
}
cnt[i] = cur;
cur = i + cur;
while (cur < n && cnt[cur] == 0) {
cnt[cur] = nxt[cur] - cur;
s.insert({a[cur], cur});
cur = nxt[cur];
}
break;
} else {
cur -= cnt[i];
}
}
}
for (int i = 0; i < q; i++) {
cout << ans[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... |