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>
#define sz(v) ((int)(v).size())
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
using VI = vector<int>;
const int NN = 2e5 + 4;
struct BIT {
int C[NN];
void add(int x, int v) {
while(x < NN) C[x] += v, x += x & -x;
// for (int i = x; i < NN; i += i & -i) C[i] += v;
}
int sum(int x) {
int ret = 0;
while (x) ret += C[x], x -= x & -x;
return ret;
}
int kth(int x) {
int pos = 0;
for (int i = 17; i >= 0; i--)
if (pos + (1 << i) < NN && C[pos + (1 << i)] < x)
pos += (1 << i), x -= C[pos];
return pos + 1;
}
};
BIT S;
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n);
vector<int> rev(n + 1);
vector<vector<pi>> v(n + 1);
for (int i = 0; i < n; i++) cin >> a[i], rev[a[i]] = i;
vector<int> ans(q);
for (int i = 0; i < q; i++) {
int t, x;
cin >> t >> x;
t = min(t, n);
if (t == 0)
ans[i] = a[x - 1];
else
v[t].emplace_back(x - 1, i);
}
vector<int> nxt(n);
vector<int> ds(n + 1);
auto SetVal = [&](int x, int v) { S.add(x, v - ds[x]), ds[x] = v; };
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && a[j] < a[i]) j++;
SetVal(a[i], j - i);
i = j;
}
{
vector<int> v;
for (int i = n - 1; i >= 0; i--) {
while (sz(v) && a[v.back()] < a[i]) v.pop_back();
if (sz(v))
nxt[i] = v.back();
else
nxt[i] = n;
v.push_back(i);
}
}
for (int i = 1; i <= n; i++) {
int itf = S.kth(n / 2 + 1);
int its = ds[itf];
int sum = S.sum(itf - 1);
if (sum < n / 2) {
int l = rev[itf];
int r = l + its;
SetVal(a[l], n / 2 - sum);
l += n / 2 - sum;
for (int i = l; i < r; i = nxt[i]) SetVal(a[i], min(r, nxt[i]) - i);
}
for (auto [pos, idx] : v[i]) {
int qos = S.kth(pos + 1);
int sum = S.sum(qos - 1);
ans[idx] = a[rev[qos] + pos - sum];
}
}
for (auto &x : ans) cout << x << "\n";
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:93:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
93 | for (auto [pos, idx] : v[i]) {
| ^
# | 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... |