#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
const int K = 20;
const int inf = 1e9;
struct node {
int x, l, r;
node(int x = 0) {}
node(int x, int l, int r) : x(x), l(l), r(r) {}
};
int n, q, a[N], nNode, nver, ver[N];
node st[N * K];
void refine(int cur) {
st[cur].x = st[st[cur].l].x + st[st[cur].r].x;
}
int update(int pos, int val, int old, int l = 1, int r = N - 1) {
int cur = ++nNode;
if(l == r) {
st[cur] = node(st[old].x + val, 0, 0);
return cur;
}
int m = l + r >> 1;
if(pos <= m) {
st[cur].l = update(pos, val, st[old].l, l, m);
st[cur].r = st[old].r;
}
else {
st[cur].l = st[old].l;
st[cur].r = update(pos, val, st[old].r, m + 1, r);
}
refine(cur);
return cur;
}
int get(int ver1, int ver2, int u, int v, int l = 1, int r = N - 1) {
if(u > r || v < l) return 0;
if(u <= l && r <= v) return st[ver2].x - st[ver1].x;
int m = l + r >> 1;
return get(st[ver1].l, st[ver2].l, u, v, l, m) + get(st[ver1].r, st[ver2].r, u, v, m + 1, r);
}
void solve() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
ver[i] = update(a[i], 1, ver[i - 1]);
}
while(q--) {
int ql, qr; cin >> ql >> qr;
int l = 1, r = 2e5, ans;
while(l <= r) {
int m = l + r >> 1;
if(get(ver[ql - 1], ver[qr], m, N - 1) >= m) ans = m, l = m + 1;
else r = m - 1;
}
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) solve();
return 0;
}
Compilation message
index.cpp: In function 'int update(int, int, int, int, int)':
index.cpp:27:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int m = l + r >> 1;
| ~~^~~
index.cpp: In function 'int get(int, int, int, int, int, int)':
index.cpp:43:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int m = l + r >> 1;
| ~~^~~
index.cpp: In function 'void solve()':
index.cpp:57:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int m = l + r >> 1;
| ~~^~~
index.cpp:61:24: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | cout << ans << '\n';
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2648 KB |
Output is correct |
6 |
Correct |
4 ms |
2904 KB |
Output is correct |
7 |
Correct |
3 ms |
2648 KB |
Output is correct |
8 |
Correct |
3 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2648 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2648 KB |
Output is correct |
6 |
Correct |
4 ms |
2904 KB |
Output is correct |
7 |
Correct |
3 ms |
2648 KB |
Output is correct |
8 |
Correct |
3 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2648 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
120 ms |
13608 KB |
Output is correct |
12 |
Correct |
121 ms |
13404 KB |
Output is correct |
13 |
Correct |
134 ms |
13408 KB |
Output is correct |
14 |
Correct |
121 ms |
13592 KB |
Output is correct |
15 |
Correct |
120 ms |
13392 KB |
Output is correct |
16 |
Correct |
124 ms |
13396 KB |
Output is correct |
17 |
Correct |
121 ms |
13396 KB |
Output is correct |
18 |
Correct |
131 ms |
13428 KB |
Output is correct |
19 |
Correct |
143 ms |
13392 KB |
Output is correct |
20 |
Correct |
128 ms |
13868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2648 KB |
Output is correct |
6 |
Correct |
4 ms |
2904 KB |
Output is correct |
7 |
Correct |
3 ms |
2648 KB |
Output is correct |
8 |
Correct |
3 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2648 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
120 ms |
13608 KB |
Output is correct |
12 |
Correct |
121 ms |
13404 KB |
Output is correct |
13 |
Correct |
134 ms |
13408 KB |
Output is correct |
14 |
Correct |
121 ms |
13592 KB |
Output is correct |
15 |
Correct |
120 ms |
13392 KB |
Output is correct |
16 |
Correct |
124 ms |
13396 KB |
Output is correct |
17 |
Correct |
121 ms |
13396 KB |
Output is correct |
18 |
Correct |
131 ms |
13428 KB |
Output is correct |
19 |
Correct |
143 ms |
13392 KB |
Output is correct |
20 |
Correct |
128 ms |
13868 KB |
Output is correct |
21 |
Correct |
635 ms |
48372 KB |
Output is correct |
22 |
Correct |
667 ms |
48456 KB |
Output is correct |
23 |
Correct |
679 ms |
48320 KB |
Output is correct |
24 |
Correct |
628 ms |
48504 KB |
Output is correct |
25 |
Correct |
685 ms |
48444 KB |
Output is correct |
26 |
Correct |
696 ms |
48416 KB |
Output is correct |
27 |
Correct |
604 ms |
48516 KB |
Output is correct |
28 |
Correct |
650 ms |
48316 KB |
Output is correct |
29 |
Correct |
652 ms |
46860 KB |
Output is correct |
30 |
Correct |
677 ms |
47032 KB |
Output is correct |