#include <bits/stdc++.h>
using namespace std;
const int B = 500;
struct FenwickTree {
vector<int> tree;
FenwickTree() {
tree.resize((int)2e5 + 5, 0);
}
void update(int x, int sign) {
for (; x; x -= x & -x)
tree[x] += sign;
}
int get(int x) {
int res = 0;
for (; x <= (int)2e5; x += x & -x)
res += tree[x];
return res;
}
};
int n, q;
int a[200005];
int l[200005], r[200005], id[200005], ans[200005];
FenwickTree ft;
bool comp(int u, int v) {
if (l[u] / B != l[v] / B)
return l[u] / B < l[v] / B;
return r[u] < r[v];
}
void add(int u, int sign) {
ft.update(a[u], sign);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
for (int i = 1; i <= q; ++ i) {
cin >> l[i] >> r[i];
id[i] = i;
}
sort(id + 1, id + 1 + q, comp);
for (int i = 1, curL = 2, curR = 1; i <= q; ++ i) {
int u = id[i];
while (curL > l[u]) add(-- curL, 1);
while (curL < l[u]) add(curL ++, -1);
while (curR < r[u]) add(++ curR, 1);
while (curR > r[u]) add(curR --, -1);
int lo = 0, hi = n + 1;
while (hi - lo > 1) {
int mid = (hi + lo) >> 1;
if (ft.get(mid) >= mid)
lo = mid;
else
hi = mid;
}
ans[u] = lo;
}
for (int i = 1; i <= q; ++ i)
cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
3 ms |
1100 KB |
Output is correct |
5 |
Correct |
3 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
3 ms |
1100 KB |
Output is correct |
5 |
Correct |
3 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
146 ms |
3168 KB |
Output is correct |
12 |
Correct |
146 ms |
3164 KB |
Output is correct |
13 |
Correct |
148 ms |
3268 KB |
Output is correct |
14 |
Correct |
150 ms |
3108 KB |
Output is correct |
15 |
Correct |
150 ms |
3164 KB |
Output is correct |
16 |
Correct |
147 ms |
3140 KB |
Output is correct |
17 |
Correct |
150 ms |
3168 KB |
Output is correct |
18 |
Correct |
145 ms |
3140 KB |
Output is correct |
19 |
Correct |
145 ms |
3116 KB |
Output is correct |
20 |
Correct |
153 ms |
3172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
3 |
Correct |
2 ms |
1100 KB |
Output is correct |
4 |
Correct |
3 ms |
1100 KB |
Output is correct |
5 |
Correct |
3 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
146 ms |
3168 KB |
Output is correct |
12 |
Correct |
146 ms |
3164 KB |
Output is correct |
13 |
Correct |
148 ms |
3268 KB |
Output is correct |
14 |
Correct |
150 ms |
3108 KB |
Output is correct |
15 |
Correct |
150 ms |
3164 KB |
Output is correct |
16 |
Correct |
147 ms |
3140 KB |
Output is correct |
17 |
Correct |
150 ms |
3168 KB |
Output is correct |
18 |
Correct |
145 ms |
3140 KB |
Output is correct |
19 |
Correct |
145 ms |
3116 KB |
Output is correct |
20 |
Correct |
153 ms |
3172 KB |
Output is correct |
21 |
Correct |
789 ms |
9844 KB |
Output is correct |
22 |
Correct |
797 ms |
9800 KB |
Output is correct |
23 |
Correct |
791 ms |
9852 KB |
Output is correct |
24 |
Correct |
792 ms |
9848 KB |
Output is correct |
25 |
Correct |
776 ms |
9796 KB |
Output is correct |
26 |
Correct |
792 ms |
9852 KB |
Output is correct |
27 |
Correct |
778 ms |
9916 KB |
Output is correct |
28 |
Correct |
783 ms |
9852 KB |
Output is correct |
29 |
Correct |
784 ms |
9924 KB |
Output is correct |
30 |
Correct |
829 ms |
9796 KB |
Output is correct |