#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
const int MAXN = 200005;
basic_string<int> t[4*MAXN];
int a[MAXN];
void build(int tx, int tl, int tr){
// cerr << tl << ' ' << tr << '\n';
if(tl == tr){
t[tx] = {a[tl]};
return;
}
int tm = (tl+tr)/2;
build(tx*2, tl, tm); build(tx*2+1, tm+1, tr);
merge(t[tx*2].begin(), t[tx*2].end(), t[tx*2+1].begin(), t[tx*2+1].end(), back_inserter(t[tx]));
}
int query(int tx, int tl, int tr, int l, int r, int x){
if(r < tl || l > tr){
return 0;
}
if(l == tl && r == tr){
return t[tx].end() - lower_bound(t[tx].begin(), t[tx].end(), x);
}
int tm = (tl+tr)/2;
return query(tx*2, tl, tm, l, min(r, tm), x) + query(tx*2+1, tm+1, tr, max(l, tm+1), r, x);
}
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
inline void readInt(int& x) {
x = 0;
char ch;
do{
ch = getchar_unlocked();
}
while (ch < '0' || ch > '9');
while (ch >= '0' && ch <= '9'){
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar_unlocked();
}
}
int main(){
cin.tie(0); ios_base::sync_with_stdio(0);
int n, q;
readInt(n); readInt(q);
for(int i=1; i<=n; i++){
readInt(a[i]);
}
build(1, 1, n);
while(q--){
int l, r;
readInt(l); readInt(r);
int bl=1, br=r-l+1, bm;
while(bl < br){
// cerr << bl << ' ' << br << ' ' << bm << '\n';
bm = (bl+br)/2;
if(br-bl == 1){
bm = br;
}
if(query(1, 1, n, l, r, bm) < bm){
br = bm-1;
}
else{
bl = bm;
}
}
cout << bl << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |