??????
???????????? n ???????????????,????????? m ?????????,?????????????????? [ l , r ] ??????????????????????????????
?????????
??????\(O(n\log(n))\)???
??????????????????
???????????????????????????????????????????????????????????????????????????sort?????????two-points???????????????????????????
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 50010
#define M 200010
using namespace std;
int n,m,ans[M],tot,a[N],cnt[1000010],s,l,r;
int bel(int x) { return ((x-1)/s+1); }
struct hhh
{
int x,y,id;
bool operator < (const hhh &b) const
{
if (bel(x)==bel(b.x)) return y<b.y;
return bel(x)<bel(b.x);
}
}q[M];
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
int main()
{
n=read();s=sqrt(n);
for (int i=1;i<=n;i++)
a[i]=read();
m=read();
for (int i=1;i<=m;i++) q[i].x=read(),q[i].y=read(),q[i].id=i;
sort(q+1,q+m+1);
for (int i=1;i<=m;i++)
{
while (l<q[i].x) if (!--cnt[a[l++]]) tot--;
while (l>q[i].x) if (!cnt[a[--l]]++) ++tot;
while (r<q[i].y) if (!cnt[a[++r]]++) ++tot;
while (r>q[i].y) if (!--cnt[a[r--]]) tot--;
ans[q[i].id]=tot;
}
for (int i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}