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;
const int mxN = 2e5+2;
int L[mxN], R[mxN], ans[mxN], bit[mxN], c = mxN - 1;
vector<int> d[mxN];
void add(int idx, int x) {
while (idx < mxN) {
bit[idx] += x;
idx |= (idx + 1);
}
}
int sum(int idx) {
int s = 0;
while (idx >= 0) {
s += bit[idx];
idx &= (idx + 1);
--idx;
}
return s;
}
int sum(int l, int r) {
return l ? sum(r) - sum(l-1) : sum(r);
}
void pbs(int l, int r, vector<int> &b) {
if (r < l || b.empty()) return;
if (l == r) {
for (auto u : b) {
ans[u] = l;
}
return;
}
int mid = (l + r)/2;
while (c > mid-1) {
for (auto u : d[c]) {
add(u, 1);
}
--c;
}
while (c < mid-1) {
++c;
for (auto u : d[c]) {
add(u, -1);
}
}
vector<int> le, ri;
for (auto x : b) {
if (sum(L[x], R[x]) >= mid) {
ri.push_back(x);
}
else {
le.push_back(x);
}
}
b.clear();
pbs(l, mid, le);
pbs(mid+1, r, ri);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
d[x].push_back(i);
}
vector<int> b;
for (int i = 0; i < q; ++i) {
cin >> L[i] >> R[i];
--L[i], --R[i];
b.push_back(i);
}
pbs(0, n+1, b);
for (int i = 0; i < q; ++i) {
cout << ans[i]-1 << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |