#include <bits/stdc++.h>
using namespace std;
const int B = 500;
struct FenwickTree {
int n;
vector<int> tree;
FenwickTree(int _n) {
n = _n;
tree.resize(n+5, 0);
}
void update(int x) {
for (; x <= n; x += x & -x)
tree[x] ++;
}
int get(int x) {
int res = 0;
for (; x; x -= x & -x)
res += tree[x];
return res;
}
};
int n, q;
pair<int, int> a[200005];
int l[200005], r[200005];
int lo[200005], hi[200005];
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; ++ i) {
cin >> a[i].first;
a[i].second = i;
}
for (int i = 1; i <= q; ++ i) {
cin >> l[i] >> r[i];
lo[i] = 0, hi[i] = n + 1;
}
sort(a + 1, a + 1 + n);
for (;;) {
vector<pair<int, int>> mid;
for (int i = 1; i <= q; ++ i)
if (hi[i] - lo[i] > 1) {
int cur = (lo[i] + hi[i]) >> 1;
mid.push_back({cur, i});
}
if (mid.empty()) break;
sort(mid.begin(), mid.end());
reverse(mid.begin(), mid.end());
FenwickTree ft(n);
int run = n;
for (auto& pii : mid) {
while (run >= 1 && a[run].first >= pii.first) {
ft.update(a[run].second);
-- run;
}
int u = pii.second;
int cur = ft.get(r[u]) - ft.get(l[u]-1);
if (cur >= pii.first)
lo[u] = pii.first;
else
hi[u] = pii.first;
}
}
for (int i = 1; i <= q; ++ i)
cout << lo[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
112 ms |
2536 KB |
Output is correct |
12 |
Correct |
114 ms |
2548 KB |
Output is correct |
13 |
Correct |
125 ms |
2532 KB |
Output is correct |
14 |
Correct |
115 ms |
2696 KB |
Output is correct |
15 |
Correct |
111 ms |
2540 KB |
Output is correct |
16 |
Correct |
120 ms |
2704 KB |
Output is correct |
17 |
Correct |
111 ms |
2544 KB |
Output is correct |
18 |
Correct |
113 ms |
2712 KB |
Output is correct |
19 |
Correct |
123 ms |
2520 KB |
Output is correct |
20 |
Correct |
121 ms |
2536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
112 ms |
2536 KB |
Output is correct |
12 |
Correct |
114 ms |
2548 KB |
Output is correct |
13 |
Correct |
125 ms |
2532 KB |
Output is correct |
14 |
Correct |
115 ms |
2696 KB |
Output is correct |
15 |
Correct |
111 ms |
2540 KB |
Output is correct |
16 |
Correct |
120 ms |
2704 KB |
Output is correct |
17 |
Correct |
111 ms |
2544 KB |
Output is correct |
18 |
Correct |
113 ms |
2712 KB |
Output is correct |
19 |
Correct |
123 ms |
2520 KB |
Output is correct |
20 |
Correct |
121 ms |
2536 KB |
Output is correct |
21 |
Correct |
564 ms |
8872 KB |
Output is correct |
22 |
Correct |
559 ms |
8740 KB |
Output is correct |
23 |
Correct |
581 ms |
8752 KB |
Output is correct |
24 |
Correct |
576 ms |
8756 KB |
Output is correct |
25 |
Correct |
599 ms |
8860 KB |
Output is correct |
26 |
Correct |
560 ms |
8748 KB |
Output is correct |
27 |
Correct |
613 ms |
8760 KB |
Output is correct |
28 |
Correct |
563 ms |
8840 KB |
Output is correct |
29 |
Correct |
563 ms |
8816 KB |
Output is correct |
30 |
Correct |
586 ms |
8740 KB |
Output is correct |