Description
Input
Output
Sample Input
7 5
0 2 1 0 1 3 2
1 3
2 3
1 4
3 6
2 7
Sample Output
3
0
3
2
4
HINT
Source
By Xhr
SB题_ (:зゝ∠) _
考虑离线做法,把询问按左右端点排序,然后利用每个数对后面询问的贡献来做.
#include
#include
#include
#include
#include
#define MAXN 200010
#define MAXINT 0x7fffffff
#define lchild rt<<1,l,mid
#define rchild rt<<1|1,mid+1,r
#define ln rt<<1
#define rn rt<<1|1
#define GET (ch>='0'&&ch<='9')
using namespace std;
int n,m,now=1;
int a[MAXN],sg[MAXN];
bool hash[MAXN];
int next[MAXN],last[MAXN];
struct Query
{
int l,r,id,ans;
bool operator <(const Query &a)const {return l==a.l?rq[MAXN];
struct seg
{
int l,r,minn;
}tree[MAXN<<2];
bool cmp(Query a,Query b) {return a.idint rt=1,int l=1,int r=n)
{
tree[rt].l=l;tree[rt].r=r;tree[rt].minn=MAXINT;
if (l==r) {tree[rt].minn=sg[l];return;}
int mid=(l+r)>>1;build(lchild);build(rchild);
}
void push_down(int rt)
{
if (tree[rt].l==tree[rt].r) return;
tree[ln].minn=min(tree[rt].minn,tree[ln].minn);tree[rn].minn=min(tree[rn].minn,tree[rt].minn);
}
void modify(int rt,int l,int r,int delta)
{
push_down(rt);
int L=tree[rt].l,R=tree[rt].r,mid=(L+R)>>1;
if (l<=L&&r>=R) {tree[rt].minn=min(tree[rt].minn,delta);return;}
if (r<=mid) modify(ln,l,r,delta);
else if (l>mid) modify(rn,l,r,delta);
else modify(ln,l,mid,delta),modify(rn,mid+1,r,delta);
}
int query(int rt,int x)
{
push_down(rt);
int L=tree[rt].l,R=tree[rt].r,mid=(L+R)>>1;
if (L==R) return tree[rt].minn;
if (x<=mid) return query(ln,x);
else return query(rn,x);
}
void in(int &x)
{
char ch=getchar();x=0;
while (!GET) ch=getchar();
while (GET) x=x*10+ch-'0',ch=getchar();
}
int main()
{
in(n);in(m);int mex=0;
for (int i=1;i<=n;i++) in(a[i]);
for (int i=1;i<=n;i++)
{
hash[a[i]]=1;
if (a[i]==mex) for (;hash[mex];mex++);
sg[i]=mex;
}
build();
for (int i=n;i;i--) next[i]=last[a[i]],last[a[i]]=i;
for (int i=1;i<=m;i++) in(q[i].l),in(q[i].r),q[i].id=i;
sort(q+1,q+m+1);
for (int i=1;i<=m;i++)
{
while (now<q[i].l)
{
if (!next[now]) next[now]=n+1;
modify(1,now,next[now]-1,a[now]);now++;
}
q[i].ans=query(1,q[i].r);
}
sort(q+1,q+m+1,cmp);
for (int i=1;i<=m;i++) printf("%d\n",q[i].ans);
}