#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200001;
struct Node {
Node *l, *r;
int sum = 0;
Node(Node *left, Node *right) : l(left), r(right) {
if (l) sum += l->sum;
if (r) sum += r->sum;
}
Node(int s) : sum(s) {}
};
Node *build(int tl, int tr) {
if (tl == tr) {
return new Node(0);
} else {
int tm = (tl + tr) / 2;
return new Node(build(tl, tm), build(tm+1, tr));
}
}
Node *upd(Node *v, int tl, int tr, int pos) {
if (tl == tr) {
return new Node(v->sum+1);
} else {
int tm = (tl + tr) / 2;
if (pos <= tm) {
return new Node(upd(v->l, tl, tm, pos), v->r);
} else {
return new Node(v->l, upd(v->r, tm+1, tr, pos));
}
}
}
int qry(Node *v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (l == tl && r == tr) {
return v->sum;
} else {
int tm = (tl + tr) / 2;
return qry(v->l, tl, tm, l, min(tm, r)) + qry(v->r, tm+1, tr, max(tm+1, l), r);
}
}
const int MAXV = 200001;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, q; cin >> n >> q;
vector<Node *> roots = {build(1, MAXV)};
for (int i = 1; i <= n; i++) {
int x; cin >> x;
roots.push_back(upd(roots.back(), 1, MAXV, x));
}
while (q--) {
int l, r; cin >> l >> r;
int lo = 1, hi = r-l+1;
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
int cnt = qry(roots[r], 1, MAXV, mid, MAXV) - qry(roots[l-1], 1, MAXV, mid, MAXV);
if (cnt >= mid) {
lo = mid;
} else {
hi = mid - 1;
}
}
cout << lo << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
13396 KB |
Output is correct |
2 |
Correct |
17 ms |
13340 KB |
Output is correct |
3 |
Correct |
16 ms |
13416 KB |
Output is correct |
4 |
Correct |
16 ms |
13396 KB |
Output is correct |
5 |
Correct |
17 ms |
13396 KB |
Output is correct |
6 |
Correct |
17 ms |
13380 KB |
Output is correct |
7 |
Correct |
16 ms |
13384 KB |
Output is correct |
8 |
Correct |
15 ms |
13396 KB |
Output is correct |
9 |
Correct |
16 ms |
13408 KB |
Output is correct |
10 |
Correct |
16 ms |
13408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
13396 KB |
Output is correct |
2 |
Correct |
17 ms |
13340 KB |
Output is correct |
3 |
Correct |
16 ms |
13416 KB |
Output is correct |
4 |
Correct |
16 ms |
13396 KB |
Output is correct |
5 |
Correct |
17 ms |
13396 KB |
Output is correct |
6 |
Correct |
17 ms |
13380 KB |
Output is correct |
7 |
Correct |
16 ms |
13384 KB |
Output is correct |
8 |
Correct |
15 ms |
13396 KB |
Output is correct |
9 |
Correct |
16 ms |
13408 KB |
Output is correct |
10 |
Correct |
16 ms |
13408 KB |
Output is correct |
11 |
Correct |
362 ms |
43652 KB |
Output is correct |
12 |
Correct |
397 ms |
43576 KB |
Output is correct |
13 |
Correct |
331 ms |
43544 KB |
Output is correct |
14 |
Correct |
349 ms |
43652 KB |
Output is correct |
15 |
Correct |
396 ms |
43588 KB |
Output is correct |
16 |
Correct |
355 ms |
43560 KB |
Output is correct |
17 |
Correct |
348 ms |
43636 KB |
Output is correct |
18 |
Correct |
415 ms |
43504 KB |
Output is correct |
19 |
Correct |
349 ms |
43716 KB |
Output is correct |
20 |
Correct |
357 ms |
43488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
13396 KB |
Output is correct |
2 |
Correct |
17 ms |
13340 KB |
Output is correct |
3 |
Correct |
16 ms |
13416 KB |
Output is correct |
4 |
Correct |
16 ms |
13396 KB |
Output is correct |
5 |
Correct |
17 ms |
13396 KB |
Output is correct |
6 |
Correct |
17 ms |
13380 KB |
Output is correct |
7 |
Correct |
16 ms |
13384 KB |
Output is correct |
8 |
Correct |
15 ms |
13396 KB |
Output is correct |
9 |
Correct |
16 ms |
13408 KB |
Output is correct |
10 |
Correct |
16 ms |
13408 KB |
Output is correct |
11 |
Correct |
362 ms |
43652 KB |
Output is correct |
12 |
Correct |
397 ms |
43576 KB |
Output is correct |
13 |
Correct |
331 ms |
43544 KB |
Output is correct |
14 |
Correct |
349 ms |
43652 KB |
Output is correct |
15 |
Correct |
396 ms |
43588 KB |
Output is correct |
16 |
Correct |
355 ms |
43560 KB |
Output is correct |
17 |
Correct |
348 ms |
43636 KB |
Output is correct |
18 |
Correct |
415 ms |
43504 KB |
Output is correct |
19 |
Correct |
349 ms |
43716 KB |
Output is correct |
20 |
Correct |
357 ms |
43488 KB |
Output is correct |
21 |
Correct |
2498 ms |
136324 KB |
Output is correct |
22 |
Execution timed out |
2555 ms |
136244 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |