#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int N = 2e5 + 10;
const int BLOCK_SIZE = 450;
int p[N];
int fenwick[N];
int ans[N];
class QUERY {
public :
int l, r, i;
QUERY() : l(0), r(0), i(0) {}
QUERY(int l_, int r_, int i_) : l(l_), r(r_), i(i_) {}
bool operator<(const QUERY &o) const {
return make_pair((l + BLOCK_SIZE - 1) / BLOCK_SIZE, r) < make_pair((o.l + BLOCK_SIZE - 1) / BLOCK_SIZE, o.r);
}
};
class Fenwick {
public :
void update(int idx, int v) {
for(; idx < N; idx += (idx & -idx)) {
fenwick[idx] += v;
}
}
int query(int idx) {
int res = 0;
for(; idx > 0; idx -= (idx & -idx)) {
res += fenwick[idx];
}
return res;
}
}fw;
int main() {
int n, q;
scanf(" %d %d", &n, &q);
for(int i = 1; i <= n; i++) {
scanf(" %d", &p[i]);
}
vector <QUERY> query;
for(int i = 1; i <= q; i++) {
int l, r;
scanf(" %d %d", &l, &r);
query.push_back(QUERY(l, r, i));
}
sort(query.begin(), query.end());
int cur_l = 1, cur_r = 0;
for(auto [l, r, i] : query) {
while(cur_l > l) {
fw.update(p[--cur_l], 1);
}
while(cur_r < r) {
fw.update(p[++cur_r], 1);
}
while(cur_l < l) {
fw.update(p[cur_l++], -1);
}
while(cur_r > r) {
fw.update(p[cur_r--], -1);
}
int le = 1, hi = r - l + 1, pos = 1;
while(le <= hi) {
int mid = (le + hi) / 2;
if(fw.query(n) - fw.query(mid - 1) < mid) {
hi = mid - 1;
}
else {
le = mid + 1;
pos = mid;
}
}
ans[i] = pos;
}
for(int i = 1; i <= q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message
index.cpp: In function 'int main()':
index.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf(" %d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~
index.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf(" %d", &p[i]);
| ~~~~~^~~~~~~~~~~~~~
index.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf(" %d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
316 KB |
Output is correct |
11 |
Correct |
202 ms |
2652 KB |
Output is correct |
12 |
Correct |
205 ms |
2696 KB |
Output is correct |
13 |
Correct |
201 ms |
2696 KB |
Output is correct |
14 |
Correct |
203 ms |
2696 KB |
Output is correct |
15 |
Correct |
205 ms |
2724 KB |
Output is correct |
16 |
Correct |
201 ms |
2612 KB |
Output is correct |
17 |
Correct |
205 ms |
2716 KB |
Output is correct |
18 |
Correct |
216 ms |
2668 KB |
Output is correct |
19 |
Correct |
198 ms |
2764 KB |
Output is correct |
20 |
Correct |
200 ms |
2628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
316 KB |
Output is correct |
11 |
Correct |
202 ms |
2652 KB |
Output is correct |
12 |
Correct |
205 ms |
2696 KB |
Output is correct |
13 |
Correct |
201 ms |
2696 KB |
Output is correct |
14 |
Correct |
203 ms |
2696 KB |
Output is correct |
15 |
Correct |
205 ms |
2724 KB |
Output is correct |
16 |
Correct |
201 ms |
2612 KB |
Output is correct |
17 |
Correct |
205 ms |
2716 KB |
Output is correct |
18 |
Correct |
216 ms |
2668 KB |
Output is correct |
19 |
Correct |
198 ms |
2764 KB |
Output is correct |
20 |
Correct |
200 ms |
2628 KB |
Output is correct |
21 |
Correct |
855 ms |
9908 KB |
Output is correct |
22 |
Correct |
897 ms |
9956 KB |
Output is correct |
23 |
Correct |
909 ms |
9892 KB |
Output is correct |
24 |
Correct |
895 ms |
9972 KB |
Output is correct |
25 |
Correct |
910 ms |
9968 KB |
Output is correct |
26 |
Correct |
993 ms |
9968 KB |
Output is correct |
27 |
Correct |
989 ms |
9964 KB |
Output is correct |
28 |
Correct |
834 ms |
10060 KB |
Output is correct |
29 |
Correct |
829 ms |
9948 KB |
Output is correct |
30 |
Correct |
846 ms |
9972 KB |
Output is correct |