#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, SQRT = sqrt(N);
int n, q, arr[N], cur, l, r, a, b, cnt[N], cntbig, ans[N];
struct Query{
int l, r, i;
bool operator < (const Query &scnd) const{
if(l / SQRT == scnd.l / SQRT) return r < scnd.r;
else return l / SQRT < scnd.l / SQRT;
}
};
Query queries[N];
void fixcur(){
while(cntbig - cnt[cur] >= cur + 1) cntbig -= cnt[cur], cur++;
while(cntbig < cur) cur--, cntbig += cnt[cur];
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> arr[i];
for(int i = 1; i <= q; i++){
cin >> queries[i].l >> queries[i].r;
queries[i].i = i;
}
sort(queries + 1, queries + q + 1);
a = 0, b = -1;
for(int i = 1; i <= q; i++){
l = queries[i].l, r = queries[i].r;
while(b + 1 <= r){
b++;
cnt[arr[b]]++;
if(arr[b] >= cur) cntbig++;
fixcur();
}
while(b > r){
cnt[arr[b]]--;
if(arr[b] >= cur) cntbig--;
b--;
fixcur();
}
while(a - 1 >= l){
a--;
cnt[arr[a]]++;
if(arr[a] >= cur) cntbig++;
fixcur();
}
while(a < l){
cnt[arr[a]]--;
if(arr[a] >= cur) cntbig--;
a++;
fixcur();
}
ans[queries[i].i] = cur;
}
for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
1 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 |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
1 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 |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
111 ms |
1732 KB |
Output is correct |
12 |
Correct |
108 ms |
1764 KB |
Output is correct |
13 |
Correct |
121 ms |
1744 KB |
Output is correct |
14 |
Correct |
104 ms |
1744 KB |
Output is correct |
15 |
Correct |
108 ms |
1744 KB |
Output is correct |
16 |
Correct |
111 ms |
1728 KB |
Output is correct |
17 |
Correct |
107 ms |
1732 KB |
Output is correct |
18 |
Correct |
105 ms |
1752 KB |
Output is correct |
19 |
Correct |
104 ms |
1776 KB |
Output is correct |
20 |
Correct |
103 ms |
1684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
1 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 |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
111 ms |
1732 KB |
Output is correct |
12 |
Correct |
108 ms |
1764 KB |
Output is correct |
13 |
Correct |
121 ms |
1744 KB |
Output is correct |
14 |
Correct |
104 ms |
1744 KB |
Output is correct |
15 |
Correct |
108 ms |
1744 KB |
Output is correct |
16 |
Correct |
111 ms |
1728 KB |
Output is correct |
17 |
Correct |
107 ms |
1732 KB |
Output is correct |
18 |
Correct |
105 ms |
1752 KB |
Output is correct |
19 |
Correct |
104 ms |
1776 KB |
Output is correct |
20 |
Correct |
103 ms |
1684 KB |
Output is correct |
21 |
Correct |
475 ms |
6780 KB |
Output is correct |
22 |
Correct |
452 ms |
9880 KB |
Output is correct |
23 |
Correct |
462 ms |
9924 KB |
Output is correct |
24 |
Correct |
479 ms |
9836 KB |
Output is correct |
25 |
Correct |
442 ms |
9796 KB |
Output is correct |
26 |
Correct |
454 ms |
9864 KB |
Output is correct |
27 |
Correct |
488 ms |
9836 KB |
Output is correct |
28 |
Correct |
450 ms |
9792 KB |
Output is correct |
29 |
Correct |
443 ms |
9840 KB |
Output is correct |
30 |
Correct |
461 ms |
9828 KB |
Output is correct |