#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, int l = 0, int 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:11:5: error: 'node::node(int, int, int)' cannot be overloaded with 'node::node(int, int, int)'
11 | node(int x, int l, int r) : x(x), l(l), r(r) {}
| ^~~~
index.cpp:10:5: note: previous declaration 'node::node(int, int, int)'
10 | node(int x = 0, int l = 0, int r = 0) {}
| ^~~~
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;
| ~~^~~