思路;
恶心;
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 200005 struct TreeNodeType { int l,r,dis,mid,flag; bool if_; }; struct TreeNodeType tree[maxn<<2]; struct QueryType { int l,r,id; }; struct QueryType qu[maxn]; int n,m,ai[maxn],sg[maxn],ans[maxn],Next[maxn],last[maxn]; int num[maxn]; inline void in(int &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0') Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } void tree_build(int now,int l,int r) { tree[now].l=l,tree[now].r=r; if(l==r) { tree[now].dis=sg[l];return; } tree[now].mid=l+r>>1; tree_build(now<<1,l,tree[now].mid); tree_build(now<<1|1,tree[now].mid+1,r); tree[now].dis=min(tree[now<<1].dis,tree[now<<1|1].dis); } void tree_down(int now) { tree[now<<1].dis=min(tree[now<<1].dis,tree[now].flag); tree[now<<1|1].dis=min(tree[now<<1|1].dis,tree[now].flag); if(tree[now<<1].if_) tree[now<<1].flag=min(tree[now<<1].flag,tree[now].flag); else tree[now<<1].flag=tree[now].flag,tree[now<<1].if_=true; if(tree[now<<1|1].if_) tree[now<<1|1].flag=min(tree[now<<1|1].flag,tree[now].flag); else tree[now<<1|1].flag=tree[now].flag,tree[now<<1|1].if_=true; tree[now].if_=false;return ; } void tree_change(int now,int l,int r,int x) { if(tree[now].l==l&&tree[now].r==r) { tree[now].dis=min(tree[now].dis,x); if(tree[now].if_) tree[now].flag=min(tree[now].flag,x); else tree[now].flag=x,tree[now].if_=true; return ; } if(tree[now].if_) tree_down(now); if(r<=tree[now].mid) tree_change(now<<1,l,r,x); else if(l>tree[now].mid) tree_change(now<<1|1,l,r,x); else tree_change(now<<1,l,tree[now].mid,x),tree_change(now<<1|1,tree[now].mid+1,r,x); tree[now].dis=min(tree[now<<1].dis,tree[now<<1|1].dis); } int tree_query(int now,int to) { if(tree[now].l==tree[now].r) return tree[now].dis; if(tree[now].if_) tree_down(now); if(to<=tree[now].mid) return tree_query(now<<1,to); else return tree_query(now<<1|1,to); } bool cmp(QueryType aa,QueryType bb) { return aa.l<bb.l; } int main() { freopen("mex.in","r",stdin); freopen("mex.out","w",stdout); in(n),in(m);int now=0; for(int i=1;i<=n;i++) in(ai[i]); for(int i=1;i<=n;i++) { num[ai[i]]++; while(num[now]) now++; sg[i]=now; if(last[ai[i]]) Next[last[ai[i]]]=i; last[ai[i]]=i; } sg[n+1]=now;tree_build(1,1,n); for(int i=1;i<=n;i++) if(!Next[i]) Next[i]=n+1; for(int i=1;i<=m;i++) in(qu[i].l),in(qu[i].r),qu[i].id=i; sort(qu+1,qu+m+1,cmp),now=1; for(int no=1;no<=m;no++) { while(now<qu[no].l) tree_change(1,now,Next[now]-1,ai[now]),now++; ans[qu[no].id]=tree_query(1,qu[no].r); } for(int i=1;i<=m;i++) printf("%d ",ans[i]); return 0; }