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], b[SIZE], ans[QSIZ];
tuple<int, int, int> ask[QSIZ];
vector<int> v[SIZE];
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;
}
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;
}
bool work() {
int k = sch(n / 2), sum = que(k - 1);
if (sum == n / 2) return 0;
vector<int> nv;
int ncnt = v[k].size() - (n / 2 - sum);
FOR (i, v[k].size() - ncnt, v[k].size() - 1) nv.pb(v[k][i]);
v[k].resize(v[k].size() - ncnt);
upd(k, -ncnt);
int mx = 0, cnt = 0;
for (int i = 0; i < nv.size(); i++) {
if (nv[i] > mx) mx = nv[i], cnt = 0;
v[mx].pb(nv[i]);
cnt++;
if (i == nv.size() - 1 || nv[i + 1] > mx) upd(mx, cnt);
}
return 1;
}
int cal(int p) {
int k = sch(p - 1), sum = que(k - 1);
return v[k][p - sum - 1];
}
void solve() {
cin >> n >> q;
FOR (i, 1, n) cin >> a[i];
FOR (i, 1, q) {
auto &[t, p, id] = ask[i];
cin >> t >> p;
id = i;
}
int mx = 0, cnt = 0;
FOR (i, 1, n) {
if (a[i] > mx) mx = a[i], cnt = 0;
v[mx].pb(a[i]);
cnt++;
if (i == n || a[i + 1] > mx) upd(mx, cnt);
}
sort(ask + 1, ask + q + 1);
int cur = 0, f = 1;
FOR (i, 1, q) {
auto [t, p, id] = ask[i];
while (f && cur < t) {
f &= work();
cur++;
}
ans[id] = cal(p);
}
FOR (i, 1, q) cout << ans[i] << '\n';
}
int main() {
Waimai;
solve();
}
Compilation message (stderr)
Main.cpp: In function 'bool work()':
Main.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < nv.size(); i++) {
| ~~^~~~~~~~~~~
Main.cpp:60:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if (i == nv.size() - 1 || nv[i + 1] > mx) upd(mx, cnt);
| ~~^~~~~~~~~~~~~~~~
# | 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... |