This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 2e5 + 5;
const int QSIZ = 1e6 + 5;
int n, q;
int a[SIZE], p[SIZE], nxt[SIZE], cnt[SIZE];
int ans[QSIZ];
tuple<int, int, int> ask[QSIZ];
int bit[SIZE];
void upd(int pos, int x) {
for (; pos <= n; pos += pos & -pos) bit[pos] += x;
}
int que(int pos) {
int re = 0;
for (; pos; pos -= pos & -pos) re += bit[pos];
return re;
}
pair<int, int> sch(int x) {
int pos = 0, sum = 0;
for (int i = __lg(n); i >= 0; i--) if (pos + (1 << i) <= n && sum + bit[pos + (1 << i)] <= x) {
sum += bit[pos += 1 << i];
}
return {pos + 1, sum};
}
bool work() {
auto [k, sum] = sch(n / 2);
if (sum == n / 2) return 0;
int len = cnt[k] - (n / 2 - sum);
upd(k, -len);
cnt[k] -= len;
for (int x = a[p[k] + (n / 2 - sum)]; x && !cnt[x]; x = nxt[x]) {
cnt[x] = (nxt[x] && !cnt[nxt[x]] ? p[nxt[x]] - p[x] : len);
len -= cnt[x];
upd(x, cnt[x]);
}
return 1;
}
int cal(int i) {
auto [k, sum] = sch(i - 1);
return a[p[k] + (i - sum - 1)];
}
void solve() {
cin >> n >> q;
FOR (i, 1, n) cin >> a[i], p[a[i]] = i;
FOR (i, 1, q) {
auto &[t, k, id] = ask[i];
cin >> t >> k;
id = i;
}
sort(ask + 1, ask + q + 1);
vector<int> st;
for (int i = n; i >= 1; i--) {
while (st.size() && a[i] > st.back()) st.pop_back();
if (st.size()) nxt[a[i]] = st.back();
st.pb(a[i]);
}
for (int x = a[1]; x; x = nxt[x]) {
cnt[x] = (nxt[x] ? p[nxt[x]] : n + 1) - p[x];
upd(x, cnt[x]);
}
int cur = 0, f = 1;
FOR (i, 1, q) {
auto &[t, k, id] = ask[i];
while (f && cur < t) {
cur++;
f &= work();
}
ans[id] = cal(k);
}
FOR (i, 1, q) cout << ans[i] << '\n';
}
int main() {
Waimai;
solve();
}
# | 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... |