对于每一个数字 , 再对数字进行分块 , 如果块的值 = 块的长度 , 那么这些数字就是选了的
这样加上莫队就是O(sqrt(n)) 修改 O(sqrt(n)) 查询
#include<bits/stdc++.h>
#define N 200050
using namespace std;
int n,m,a[N],ans[N],pos[N],cnt[N];
int L[N],R[N],val[N],tot;
struct Node{int l,r,id;}q[N];
int read(){
int cnt=0;char ch=0;
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
return cnt;
}
bool cmp(Node a,Node b){
if(pos[a.l] == pos[b.l]) return a.r < b.r;
return pos[a.l] < pos[b.l];
}
inline int Q(){
int now = 1;
while(val[now] == R[now]-L[now]+1) now++;
for(int i=L[now];i<=R[now];i++) if(!cnt[i]) return i;
}
inline void add(int x){if(cnt[x]==0) val[pos[x]]++; cnt[x]++;}
inline void del(int x){if(cnt[x]==1) val[pos[x]]--; cnt[x]--;}
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++){
a[i] = read();
if(a[i] > n) a[i] = n+1;
}
int siz = sqrt(n);
for(int i=0;i<=n;i++) pos[i] = i/siz + 1;
for(int i=0;i<=n;i+=siz){
L[++tot] = i , R[tot] = i + siz - 1;
}
for(int i=1;i<=m;i++){
q[i].l = read() , q[i].r = read() , q[i].id = i;
} sort(q+1,q+m+1,cmp);
int l=1,r=0;
for(int i=1;i<=m;i++){
while(l<q[i].l) del(a[l++]); while(l>q[i].l) add(a[--l]);
while(r<q[i].r) add(a[++r]); while(r>q[i].r) del(a[r--]);
ans[q[i].id] = Q();
}
for(int i=1;i<=m;i++) printf("%d\n",ans[i]); return 0;
}