This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: m371
* created: 18.10.2021 15:55:58
**/
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int M = 20 * N;
int st[M], ls[M], rs[M], tsz, root[N];
void modify(int prv, int& node, int l, int r, int x, int y) {
node = ++tsz; ls[node] = ls[prv]; rs[node] = rs[prv]; st[node] = st[prv] + y;
if (l == r) {
return;
}
int mid = l + r >> 1;
if (x <= mid) {
modify(ls[prv], ls[node], l, mid, x, y);
} else {
modify(rs[prv], rs[node], mid + 1, r, x, y);
}
}
int get(int node, int l, int r, int ll, int rr) {
if (l > r || l > rr || r < ll) {
return 0;
}
if (ll <= l && r <= rr) {
return st[node];
}
int mid = l + r >> 1;
return get(ls[node], l, mid, ll, rr) + get(rs[node], mid + 1, r, ll, rr);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, qq;
cin >> n >> qq;
vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
vector<vector<int>> at(N);
for (int i = 0; i < n; i++) {
at[p[i]].push_back(i);
}
for (int i = N - 1; i >= 1; i--) {
root[i] = root[i + 1];
for (int j : at[i]) {
modify(root[i], root[i], 0, n - 1, j, 1);
}
}
while (qq--) {
int l, r;
cin >> l >> r;
--l; --r;
int low = 1, high = N - 1, ans = 0;
while (low <= high) {
int mid = low + high >> 1;
if (get(root[mid], 0, n - 1, l, r) >= mid) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
index.cpp: In function 'void modify(int, int&, int, int, int, int)':
index.cpp:19:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = l + r >> 1;
| ~~^~~
index.cpp: In function 'int get(int, int, int, int, int)':
index.cpp:34:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int mid = l + r >> 1;
| ~~^~~
index.cpp: In function 'int main()':
index.cpp:63:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | int mid = low + high >> 1;
| ~~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |