#include <bits/stdc++.h>
using namespace std;
template<class T, int sz> struct persistentSegTree{
static const int maxSz = sz * 31;
T seg[maxSz]; int idx = 1, lc[maxSz], rc[maxSz];
void dup(int &i){
lc[idx] = lc[i];
rc[idx] = rc[i];
seg[idx] = seg[i];
i = idx; idx++;
}
void upd(int p, int &i, int l = 0, int r = 2e5 + 5){
dup(i);
if (l == r){ seg[i] = 1; return; }
int mid = (l + r) / 2;
if (p <= mid) upd(p, lc[i], l, mid);
else upd(p, rc[i], mid + 1, r);
seg[i] = seg[lc[i]] + seg[rc[i]];
}
T qry(int ql, int qr, int i, int l = 0, int r = 2e5 + 5){
if (qr < l or ql > r or !i) return 0;
if (l >= ql and r <= qr) return seg[i];
int mid = (l + r) / 2;
return qry(ql, qr, lc[i], l, mid) + qry(ql, qr, rc[i], mid + 1, r);
}
};
const int mx = 2e5 + 5;
int n, q, root[mx]; vector<int> nums[mx]; persistentSegTree<int, mx> pseg;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++){
int p; cin >> p;
nums[p].push_back(i);
}
for (int p = mx - 2; p; p--){ //p[i] <= 2e5
root[p] = root[p + 1];
for (int i : nums[p])
pseg.upd(i, root[p]);
}
while (q--){
int l, r; cin >> l >> r;
int lo = 0, hi = mx;
while (lo < hi){
int mid = (lo + hi + 1) / 2;
//How many elems >= mid in [l...r]?
(pseg.qry(l, r, root[mid]) >= mid) ? lo = mid : hi = mid - 1;
}
cout<<lo<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5972 KB |
Output is correct |
2 |
Correct |
6 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6064 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
6072 KB |
Output is correct |
7 |
Correct |
6 ms |
6072 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
6036 KB |
Output is correct |
10 |
Correct |
6 ms |
6076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5972 KB |
Output is correct |
2 |
Correct |
6 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6064 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
6072 KB |
Output is correct |
7 |
Correct |
6 ms |
6072 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
6036 KB |
Output is correct |
10 |
Correct |
6 ms |
6076 KB |
Output is correct |
11 |
Correct |
223 ms |
18872 KB |
Output is correct |
12 |
Correct |
207 ms |
18832 KB |
Output is correct |
13 |
Correct |
210 ms |
18840 KB |
Output is correct |
14 |
Correct |
206 ms |
18920 KB |
Output is correct |
15 |
Correct |
210 ms |
19032 KB |
Output is correct |
16 |
Correct |
199 ms |
18764 KB |
Output is correct |
17 |
Correct |
204 ms |
18868 KB |
Output is correct |
18 |
Correct |
206 ms |
18872 KB |
Output is correct |
19 |
Correct |
200 ms |
18812 KB |
Output is correct |
20 |
Correct |
223 ms |
18808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5972 KB |
Output is correct |
2 |
Correct |
6 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6064 KB |
Output is correct |
5 |
Correct |
6 ms |
5972 KB |
Output is correct |
6 |
Correct |
6 ms |
6072 KB |
Output is correct |
7 |
Correct |
6 ms |
6072 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
6036 KB |
Output is correct |
10 |
Correct |
6 ms |
6076 KB |
Output is correct |
11 |
Correct |
223 ms |
18872 KB |
Output is correct |
12 |
Correct |
207 ms |
18832 KB |
Output is correct |
13 |
Correct |
210 ms |
18840 KB |
Output is correct |
14 |
Correct |
206 ms |
18920 KB |
Output is correct |
15 |
Correct |
210 ms |
19032 KB |
Output is correct |
16 |
Correct |
199 ms |
18764 KB |
Output is correct |
17 |
Correct |
204 ms |
18868 KB |
Output is correct |
18 |
Correct |
206 ms |
18872 KB |
Output is correct |
19 |
Correct |
200 ms |
18812 KB |
Output is correct |
20 |
Correct |
223 ms |
18808 KB |
Output is correct |
21 |
Correct |
1158 ms |
58528 KB |
Output is correct |
22 |
Correct |
1185 ms |
58524 KB |
Output is correct |
23 |
Correct |
1172 ms |
58532 KB |
Output is correct |
24 |
Correct |
1203 ms |
58664 KB |
Output is correct |
25 |
Correct |
1205 ms |
58676 KB |
Output is correct |
26 |
Correct |
1237 ms |
58592 KB |
Output is correct |
27 |
Correct |
1222 ms |
58608 KB |
Output is correct |
28 |
Correct |
1190 ms |
58688 KB |
Output is correct |
29 |
Correct |
1266 ms |
58564 KB |
Output is correct |
30 |
Correct |
1187 ms |
58628 KB |
Output is correct |