#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 1e9+7;
const int inf = 1e18;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pii,null_type,less_equal<pii>,rb_tree_tag,tree_order_statistics_node_update>ordered_multiset;
int bp(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int MI(int n, int p){
return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
return x * 1ull * y % p;
}
int di(int x,int y, int p){
return mul(x, MI(y, p), p);
}
int n , m , k , q;
void er(ordered_multiset &t, pii v){
int ord = t.order_of_key(v);
auto it = t.find_by_order(ord);
t.erase(it);
}
int N = 2e5+5;
int ud = sqrt(N);
bool comp(vector<int>x,vector<int>y){
if (x[0]/ud<y[0]/ud){
return 1;
}
else if(y[0]/ud<x[0]/ud){
return 0;
}
else{
if (x[1]<y[1]){
return 1;
}
else{
return 0;
}
}
}
void solve(int testcase_number){
cin>>n>>q;
input(int,p,n);
vector<vector<int>>ord;
for(int i =0 ;i<q;i++){
int l,r;
cin>>l>>r;
ord.pb({l-1,r-1,i});
}
sort(all(ord),comp);
vector<int>ans(q,0);
ordered_multiset ms;
// ms.insert({1,1});
// for(auto i:ms){
// cout<<i.fi<<" "<<i.se<<endl;;
// }
// cout<<endl;
// // ms.erase({1,1});
// for(auto i:ms){
// cout<<i.fi<<" "<<i.se<<endl;;
// }
// cout<<endl;
int l = -1;
int r = -1;
for(auto i:ord){
int tl = i[0];
int tr = i[1];
if(l == -1){
l = tl;
r = tl;
ms.insert({p[tl],tl});
}
while(l < tl){
er(ms,{p[l],l});
// ms.erase({p[l],l});
l++;
}
while(r > tr){
er(ms,{p[r],r});
// ms.erase({p[r],r});
r--;
}
while(l > tl){
l--;
ms.insert({p[l],l});
}
while(r < tr){
r++;
ms.insert({p[r],r});
}
// cout<<l<<" "<<r<<" "<<tl<<" "<<tr<<endl;
// print(ms)
// ms.erase({2ll,1});
// for(auto i:ms){
// cout<<i.fi<<" "<<i.se<<endl;;
// }
// cout<<endl;
int low = 0;
int hi = n+1;
while(hi-low > 1){
int mid = (hi+low)/2;
int grt = ms.size()-ms.order_of_key({mid,-1});
if(grt>=mid){
low = mid;
}
else{
hi = mid;
}
}
ans[i[2]] = low;
}
for(int i:ans){
cout<<i<<endl;
}
// print(ans);
}
signed main(){
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);
cout << fixed<<setprecision(9);
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++){
solve(i);
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |