#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; const int maxx = 1e6+6; int a[maxx]; int vis[maxx]; int ans[maxx]; int block; int res; struct node { int l,r; int id; friend bool operator < (node &a,node &b) { if (a.l/block==b.l/block) { return a.r<b.r; } return a.l/block<b.l/block; } } que[maxx]; void add(int x) { vis[a[x]]++; if (vis[a[x]]==1) res++; } void del(int x) { vis[a[x]]--; if (vis[a[x]]==0) { res--; } } int main() { int n,q; while(~scanf("%d",&n)) { res=0; block=sqrt(n); memset(vis,0,sizeof(vis)); for (int i=1; i<=n; i++) { scanf("%d",&a[i]); } scanf("%d",&q); int l=1,r=0; for (int i=1; i<=q; i++) { scanf("%d%d",&que[i].l,&que[i].r); que[i].id=i; } sort(que+1,que+1+q); for (int i=1; i<=q; i++) { while(l<que[i].l)del(l),l++; while(l>que[i].l)l--,add(l); while(r<que[i].r)r++,add(r); while(r>que[i].r)del(r),r--; ans[que[i].id]=res; } for (int i=1;i<=q;i++){ if (i-1)printf(" %d",ans[i]); else printf("%d",ans[i]); } } return 0; }