#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() { x = l = r = 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];
node 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 ver, 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[ver].x;
int m = l + r >> 1;
return get(st[ver].l, u, v, l, m) + get(st[ver].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[qr], m, N - 1) - get(ver[ql - 1], 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 'node refine(int)':
index.cpp:19:1: warning: no return statement in function returning non-void [-Wreturn-type]
19 | }
| ^
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)':
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 |
Runtime error |
61 ms |
96596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
61 ms |
96596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
61 ms |
96596 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |