#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define Bit(x, i) ((x >> (i)) & 1)
#define Mask(i) (1 << (i))
#define Cnt(x) __builtin_popcount(x)
#define Cntll(x) __builtin_popcountll(x)
#define Ctz(x) __builtin_ctz(x) // so luong so 0 tinh tu ben phai
#define Ctzll(x) __builtin_ctzll(x)
#define Clz(x) __builtin_clz(x) // so luong so 0 tinh tu ben trai
#define Clzll(x) __builtin_clzll(x)
inline bool maximize(int &u, int v){
return v > u ? u = v, true : false;
}
inline bool minimize(int &u, int v){
return v < u ? u = v, true : false;
}
inline bool maximizell(long long &u, long long v){
return v > u ? u = v, true : false;
}
inline bool minimizell(long long &u, long long v){
return v < u ? u = v, true : false;
}
const int mod = 1e9 + 7;
inline int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
inline void add(int &u, int v){
u += v;
if(u >= mod) u -= mod;
}
inline void sub(int &u, int v){
u -= v;
if(u < 0) u += mod;
}
const int maxN = 2e5 + 5;
const int maxNlogN = maxN * __lg(maxN) + 3;
const int inf = 1e9;
const long long infll = 1e18;
int numVersion, n, q, MAX = 2e5;
int a[maxN];
int Left[maxNlogN], Right[maxNlogN], it[maxNlogN];
int numNode;
int rootVersion[maxN];
void update(int curId, int preId, int l, int r, int p){
if(l == r){
it[curId] = it[preId] + 1;
return;
}
int mid = l + r >> 1;
if(p <= mid){
Left[curId] = ++numNode;
Right[curId] = Right[preId];
update(Left[curId], Left[preId], l, mid, p);
}else{
Left[curId] = Left[preId];
Right[curId] = ++numNode;
update(Right[curId], Right[preId], mid + 1, r, p);
}
it[curId] = it[Left[curId]] + it[Right[curId]];
}
int getSum(int cur, int pre, int l, int r, int u, int v){
if(l >= u && r <= v){
return it[cur] - it[pre];
}
int mid = l + r >> 1;
if(v <= mid) return getSum(Left[cur], Left[pre], l, mid, u, v);
if(u > mid) return getSum(Right[cur], Right[pre], mid + 1, r, u, v);
return getSum(Left[cur], Left[pre], l, mid, u, v) + getSum(Right[cur], Right[pre], mid + 1, r, u, v);
}
void process(){
cin >> n >> q;
FOR(i, 1, n){
cin >> a[i];
rootVersion[i] = ++numNode;
update(rootVersion[i], rootVersion[i - 1], 1, MAX, a[i]);
}
while(q--){
int l, r;
cin >> l >> r;
int lo = 1, hi = MAX, res = 1;
while(lo <= hi){
int mid = lo + hi >> 1;
if(getSum(rootVersion[r], rootVersion[l - 1], 1, MAX, mid, MAX) >= mid){
res = mid;
lo = mid + 1;
}else hi = mid - 1;
}
cout << res << '\n';
}
}
#define LOVE "index"
int main(){
if(fopen(LOVE".inp", "r")){
freopen(LOVE".inp", "r", stdin);
freopen(LOVE".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
// cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
Compilation message (stderr)
index.cpp: In function 'int main()':
index.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(LOVE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
index.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(LOVE".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |