#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);
int rem = q;
for (;;) {
vector<pair<int, int>> mid(rem);
for (int i = 1, cnt = 0; i <= q; ++ i)
if (hi[i] - lo[i] > 1) {
int cur = (lo[i] + hi[i]) >> 1;
mid[cnt++] = {cur, i};
}
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;
if (hi[u] - lo[u] == 1) -- rem;
}
if (rem == 0) break;
}
for (int i = 1; i <= q; ++ i)
cout << lo[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
332 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 |
332 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 |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
332 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 |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
113 ms |
2340 KB |
Output is correct |
12 |
Correct |
115 ms |
2340 KB |
Output is correct |
13 |
Correct |
119 ms |
2340 KB |
Output is correct |
14 |
Correct |
107 ms |
2404 KB |
Output is correct |
15 |
Correct |
105 ms |
2344 KB |
Output is correct |
16 |
Correct |
115 ms |
2336 KB |
Output is correct |
17 |
Correct |
104 ms |
2284 KB |
Output is correct |
18 |
Correct |
108 ms |
2412 KB |
Output is correct |
19 |
Correct |
115 ms |
2380 KB |
Output is correct |
20 |
Correct |
117 ms |
2336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
332 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 |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
113 ms |
2340 KB |
Output is correct |
12 |
Correct |
115 ms |
2340 KB |
Output is correct |
13 |
Correct |
119 ms |
2340 KB |
Output is correct |
14 |
Correct |
107 ms |
2404 KB |
Output is correct |
15 |
Correct |
105 ms |
2344 KB |
Output is correct |
16 |
Correct |
115 ms |
2336 KB |
Output is correct |
17 |
Correct |
104 ms |
2284 KB |
Output is correct |
18 |
Correct |
108 ms |
2412 KB |
Output is correct |
19 |
Correct |
115 ms |
2380 KB |
Output is correct |
20 |
Correct |
117 ms |
2336 KB |
Output is correct |
21 |
Correct |
540 ms |
8492 KB |
Output is correct |
22 |
Correct |
530 ms |
8496 KB |
Output is correct |
23 |
Correct |
548 ms |
8524 KB |
Output is correct |
24 |
Correct |
552 ms |
8508 KB |
Output is correct |
25 |
Correct |
596 ms |
8560 KB |
Output is correct |
26 |
Correct |
574 ms |
8508 KB |
Output is correct |
27 |
Correct |
586 ms |
8492 KB |
Output is correct |
28 |
Correct |
527 ms |
8584 KB |
Output is correct |
29 |
Correct |
534 ms |
8492 KB |
Output is correct |
30 |
Correct |
566 ms |
8448 KB |
Output is correct |