This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#ifdef N_N_C
#include "debug.h"
#else
#define cebug(...) "Arya"
#endif
#define ll long long
const int N=2e5+5;
const int mod=1e9+7;
const int block=350;
struct Query{
int l,r,id;
bool operator < (const Query &o){
if(this->l/block==o.l/block) return this->r<o.r;
return this->l/block<o.l/block;
}
}q[N];
struct BIT{
#define gb(x) (x&(-x))
int n;
vector <int> bit;
void init(int _n){
bit.resize(_n,0);
n=_n;
}
void update(int pos, int val){
int i=pos;
for (; i; i-=gb(i)) bit[i]+=val;
}
int get(int pos){
int ans=0,i=pos;
for (; i<n; i+=gb(i)) ans+=bit[i];
return ans;
}
}fw;
int n,m,a[N],ans,saki[N];
void add(int i){
fw.update(a[i],1);
while(fw.get(ans+1)>=ans+1) ans++;
}
void del(int i){
fw.update(a[i],-1);
while(fw.get(ans)<ans) ans--;
}
void sol(){
fw.init(N);
cin>>n>>m;
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=1; i<=m; i++) cin>>q[i].l>>q[i].r,q[i].id=i;
sort(q+1,q+m+1);
int l=1,r=0;
for(int i=1; i<=m; i++){
while(r<q[i].r) add(++r);
while(r>q[i].r) del(r--);
while(l<q[i].l) del(l++);
while(l>q[i].l) add(--l);
saki[q[i].id]=ans;
}
for(int i=1; i<=m; i++) cout<<saki[i]<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
int tt=1;
//cin>>tt;
while(tt--){
sol();
}
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
/** /\_/\
* (= ._.)
* / >💖 \>💕
**/
Compilation message (stderr)
index.cpp:79:9: warning: "/*" within comment [-Wcomment]
79 | /** /\_/\
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |