提交纪录
#include <bits/stdc++.h>
using namespace std;
#define hh putchar('\n')
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
const int N=1e6+10;
int n,m,len,pos[N];
struct node{
int l,r,id;
} q[N];
bool cmp(node A,node B){
if(pos[A.l]!=pos[B.l]) return pos[A.l]<pos[B.l];
if(pos[A.l]&1) return A.r>B.r;
return A.r<B.r;
}
int a[N],ans[N],cnt[N],L=1,R=0,now=0;
void update_add(int x){
if(!cnt[a[x]]) now++;
cnt[a[x]]++;
}
void update_del(int x){
cnt[a[x]]--;
if(!cnt[a[x]]) now--;
}
signed main(){
n=read();
for(int i=1;i<=n;i++) a[i]=read();
len=sqrt(n);
for(int i=1;i<=n;i++) pos[i]=(i-1)/len+1;
m=read();
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);
for(int i=1;i<=m;i++){
while(L<q[i].l) update_del(L++);
while(R>q[i].r) update_del(R--);
while(L>q[i].l) update_add(--L);
while(R<q[i].r) update_add(++R);
ans[q[i].id]=now;
}
for(int i=1;i<=m;i++) write(ans[i]),hh;
return 0;
}