luogu P4137 Rmq Problem / mex
一道莫队
完全不会
yfl dalao表示我太菜了这道题贼简单
qaqqq
orz我对不起讲过莫队的yfl dalao
真的是纯纯纯莫队emmm
#include<cstdio>
#include<cmath>
#include<algorithm>
#define sev en
using namespace std;
#define N 200010
int n,m,a[N],s,num[N],ans[N],L = 1,R;
struct EDGE{
int l,r,blo,id;
bool operator < (const EDGE &x) const{
return blo < x.blo || (blo == x.blo && r < x.r);
}
}e[N];
void add(int x){
num[a[x]]++;
int i = s;
while(num[i])
i++;
s = i;
}
void del(int x){
num[a[x]]--;
if(num[a[x]] == 0)
s = min(s,a[x]);
}
void Mo(){
for(int i = 1;i <= m;i++){
int l = e[i].l,r = e[i].r;
while(L > l)
add(--L);
while(R < r)
add(++R);
while(L < l)
del(L++);
while(R > r)
del(R--);
ans[e[i].id] = s;
}
}
int main(){
scanf("%d%d",&n,&m);
int sq = sqrt(n);
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
for(int i = 1;i <= m;i++){
int x,y;
scanf("%d%d",&x,&y);
e[i].l = x;
e[i].r = y;
e[i].id = i;
e[i].blo = (x - 1) / sq + 1;
}
sort(e + 1,e + m + 1);
Mo();
for(int i = 1;i <= m;i++)
printf("%d\n",ans[i]);
return 0;
}
qaqqq
我现在博客怎么都这么水了