Submission #493203

#TimeUsernameProblemLanguageResultExecution timeMemory
493203inksamuraiIndex (COCI21_index)C++17
110 / 110
1489 ms145948 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _3cSpNGp ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;

struct node{
	int val=0;
	node *l,*r;
	node(int _val) : val(_val),l(nullptr),r(nullptr){}
	node(node *_l,node *_r) : val(0),l(_l),r(_r){
		if(l) val+=l->val;
		if(r) val+=r->val;
	}
};
 
node* roots[200006];
 
node* build(int l,int r){
	if(l==r-1)
		return new node(0);
	int m=(l+r)>>1;
	return new node(build(l,m),build(m,r));
}
 
node* update(node* v,int l,int r,int pos,int val){
	if(l==r-1){
		return new node(val);
	}
	int m=(l+r)>>1;
	if(pos<m)
		return new node(update(v->l,l,m,pos,val),v->r);
	return new node(v->l,update(v->r,m,r,pos,val));
}
 
int prod(node* v,int l,int r,int _l,int _r){
	if(l>=_r or r<=_l)
		// _e
		return 0;
	if(_l<=l and r<=_r)
		return v->val;
	int m=(l+r)>>1;
	return prod(v->l,l,m,_l,_r)+prod(v->r,m,r,_l,_r);
}

int get(node* v,int l,int r,int pos){
	// cout<<pos<<"\n";
	if(l==r-1) 
		return v->val;
	int m=(l+r)>>1;
	if(pos<m)
		return get(v->l,l,m,pos);
	return get(v->r,m,r,pos);
}
 

int main(){
_3cSpNGp;
	int n,q;
	cin>>n>>q;
	vi a(n);
	rep(i,n) cin>>a[i];
	rep(i,n) a[i]=min(a[i],n);
	vec(vi) rbts(n+1);
	rep(i,n){
		rbts[a[i]].pb(i);
	}
	roots[n+1]=build(0,n);
	drep(i,n+1){
		if(!i) break;
		roots[i]=roots[i+1];
		for(auto j : rbts[i]){
			roots[i]=update(roots[i],0,n,j,1);
		}
	}
	auto bs=[&](int l,int r)->int{
		int _l=1,_r=n,_c=0;
		while(_l<=_r){
			int _m=(_l+_r)>>1;
			if(prod(roots[_m],0,n,l,r)>=_m){
				_c=_m;
				_l=_m+1;
			}else{
				_r=_m-1;
			}
		}
		return _c;
	};
	rep(_,q){
		int l,r;
		cin>>l>>r;
		cout<<bs(l-1,r)<<"\n";
	}
//	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...