1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int maxn=(int)1e6+10;
struct node{ int l,r,id; }q[maxn]; int n,m,pos[maxn],a[maxn],vis[maxn],ans[maxn],Ans; bool cmp(node a,node b){ if(pos[a.l]==pos[b.l]) return a.r<b.r; else return pos[a.l]<pos[b.l]; } int L=1,R=0;
void add(int x){ if(!vis[a[x]]) Ans++; vis[a[x]]++; }
void del(int x){ vis[a[x]]--; if(!vis[a[x]]) Ans--; }
int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); Ans=0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int block = (int) sqrt(n); scanf("%d",&m); for (int i = 1; i <= m; i++) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i, pos[i] = i / block; sort(q + 1, q + 1 + m, cmp); for (int i = 1; i <= m; i++) { while(L<q[i].l){ del(L); L++; } while(L>q[i].l){ L--; add(L); } while(R<q[i].r){ R++; add(R); } while(R>q[i].r){ del(R); R--; } ans[q[i].id]=Ans; } for(int i=1;i<=m;i++) printf("%d\n",ans[i]); } return 0; }
|