// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n"
#define fi first
#define se second
const int M=998244353;
const int inf = 1e14;
const int LOG=18;
const int N=2e5+5;
int n , m , c , w , k , t=1 , q=1 , x , y , z , l , r;
int d[N];
int ud=sqrt(N);
bool comp(pair<int,int>x,pair<int,int>y){
if (x.fi/ud<y.fi/ud){
return 1;
}
else if(y.fi/ud<x.fi/ud){
return 0;
}
else{
if (x.se<y.se){
return 1;
}
else{
return 0;
}
}
}
void solve(){
cin >> n >> q;
vector<int>a(n+1);
for (int i=1;i<=n;i++){
cin >> a[i];
}
map<pair<int,int>,vector<int>>dl;
vector<pair<int,int>>qu(q);
for (int i=0;i<q;i++){
cin >> qu[i].fi >> qu[i].se;
dl[qu[i]].push_back(i);
}
sort(qu.begin(),qu.end(),comp);
int l=1;
int r=1;
int h=0;
int cn=0;
vector<int>ans(n);
for (auto [x,y]:qu){
while (r<=y){
d[a[r]]++;
if (a[r]>=h)cn++;
while (cn-d[h]>h){
cn-=d[h];
h++;
}
r++;
}
while (l>x){
l--;
d[a[l]]++;
if (a[l]>=h)cn++;
while (cn-d[h]>h){
cn-=d[h];
h++;
}
}
while (r>y+1){
r--;
d[a[r]]--;
if (a[r]>=h)cn--;
while (cn<h){
h--;
cn+=d[h];
}
}
while (l<x){
d[a[l]]--;
if (a[l]>=h)cn--;
while (cn<h){
h--;
cn+=d[h];
}
l++;
}
ans[dl[{x,y}].back()]=h;
dl[{x,y}].pop_back();
}
for (int i=0;i<q;i++){
cout << ans[i] << endl;
}
}
signed main()
{
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
cout << fixed << setprecision(9);
srand(time(0));
// int t=1;
// cin >> t;
for (int _=1;_<=t;_++){
solve();
q++;
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |