先考虑如何求出一个集合的答案。
将所有数从小到大加入。
假设当前已经可以构造出
[1,mx]
的所有数,然后加入一个数
x
。
当
当
x≤mx+1
时,
mx
会加上
x
。
于是建一棵主席树,对于一个询问,每次求出
#include<bits/stdc++.h>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void Read(int& x){
char c=nc();
for(;c<'0'||c>'9';c=nc());
for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());
}
const int N=100010;
const int M=10000010;
const int INF=1e9;
int k,n,m,x,y;
int Rt[N],ls[M],rs[M],c[M],num;
void Update(int& x,int y,int l,int r,int z){
x=++num;ls[x]=ls[y];rs[x]=rs[y];c[x]=c[y]+z;
if(l==r)return;
int Mid=l+r>>1;
if(z<=Mid)Update(ls[x],ls[y],l,Mid,z);else Update(rs[x],rs[y],Mid+1,r,z);
}
int Query(int x,int y,int l,int r,int R){
if(l>R)return 0;
if(r<=R)return c[y]-c[x];
int Mid=l+r>>1;
return Query(ls[x],ls[y],l,Mid,R)+Query(rs[x],rs[y],Mid+1,r,R);
}
int main(){
Read(n);
for(int i=1;i<=n;i++)Read(x),Update(Rt[i],Rt[i-1],1,INF,x);
Read(m);
while(m--){
Read(x);Read(y);
k=1;
while(1){
int t=Query(Rt[x-1],Rt[y],1,INF,k);
if(t<k)break;
k=t+1;
}
printf("%d\n",k);
}
return 0;
}