This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,fma,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define int long long
using namespace std;
mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=2e5;
int n,q,block,a[N+5],ans[N+5],FT[N+5];
struct Node{
int l,r,idx;
};
bool comp(Node a, Node b){
if (a.l/block!=b.l/block) return a.l/block<b.l/block;
return a.r<b.r;
};
void Update(int pos, int val){
for (;pos<=N;pos+=pos&(-pos)) FT[pos]+=val;
}
int Get(int pos){
int val=0;
for (;pos;pos-=pos&(-pos)) val+=FT[pos];
return val;
}
vector <Node> v;
void Solve(){
cin>>n>>q;
for (int i=1;i<=n;++i) cin>>a[i];
for (int i=1;i<=q;++i){
int l,r;
cin>>l>>r;
v.push_back({l,r,i});
}
block=sqrt(n);
sort(v.begin(),v.end(),comp);
int x=1,y=0;
for (int i=0;i<q;++i){
while (x<v[i].l){
Update(a[x],-1);
++x;
}
while (x>v[i].l){
--x;
Update(a[x],1);
}
while (y<v[i].r){
++y;
Update(a[y],1);
}
while (y>v[i].r){
Update(a[y],-1);
--y;
}
int left=1,right=y-x+1;
while (left<right){
int md=left+right>>1;
if (Get(N)-Get(md)>=md+1) left=md+1;
else right=md;
}
ans[v[i].idx]=left;
}
for (int i=1;i<=q;++i) cout<<ans[i]<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen ("FILE.INP","r",stdin);
// freopen ("FILE.OUT","w",stdout);
int t=1;
// cin>>t;
while (t--) Solve();
cerr<<"\nTIME: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
}
Compilation message (stderr)
index.cpp: In function 'void Solve()':
index.cpp:55:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int md=left+right>>1;
| ~~~~^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |