用的是把第一次出现的数字存到栈里的方法,可是一直卡#9。 QwQ
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
int n,m,B;
int a[N];
int ans[N];
struct node
{
int l,r,id;
}q[N];
int t[N],st[N],top,f;
int cnt[N];
int blk(int x)
{
return (x-1)/B+1;
}
bool cmp(node a,node b)
{
return (blk(a.l)==blk(b.l))?(blk(a.l&1)?a.r<b.r:a.r>b.r):a.l<b.l;
}
void add(int xx)
{
int x=a[xx];
cnt[x]++;
if(cnt[x]==1)
{
f++;
st[++top]=x;
t[x]=top;
}else if(cnt[x]==2)
{
st[t[x]]=st[top];
t[st[top]]=t[x];
t[x]=st[top]=0;
top--;
}
}
void del(int xx)
{
int x=a[xx];
cnt[x]--;
if(cnt[x]==1)
{
f++;
st[++top]=x;
t[x]=top;
}else if(cnt[x]==0)
{
f--;
st[t[x]]=st[top];
t[st[top]]=t[x];
t[x]=st[top]=0;
top--;
}
}
int read()
{
char c;
int x=0,ff=1;
c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') ff=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}
return x*ff;
}
void write(int x)
{
if(x<0) putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
a[i]=read();
}
scanf("%d",&m);
B=sqrt(n);
for(int i=1;i<=m;++i)
{
q[i].l=read();
q[i].r=read();
q[i].id=i;
}
sort(q+1,q+1+m,cmp);
int l=1,r=0;
for(int i=1;i<=m;++i)
{
while(l>q[i].l) add(--l);
while(r<q[i].r) add(++r);
while(r>q[i].r) del(r--);
while(l<q[i].l) del(l++);
if(f==0) ans[q[i].id]=0;
else ans[q[i].id]=st[top];
}
for(int i=1;i<=m;++i)
{
write(ans[i]);
printf("\n");
}
return 0;
}