> File Name: 5787.cpp > Author: jiangyuzhu > Mail: 834138558@qq.com > Created Time: 2016/10/4 21:09:31 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; const int maxn = 5e4 + 5, maxm = 20 * maxn, maxa = 1e6 + 5; int lson[maxm], rson[maxm], t[maxm], tree[maxm]; int tot; int build(int l, int r) { int root = tot++; tree[root] = 0; int mid = l + r >> 1; if(l == r) return root; lson[root] = build(l, mid); rson[root] = build(mid + 1, r); return root; } int update(int root, int l, int r, int num, int val) { int newroot = tot++; tree[newroot] = tree[root] + val; if(l == r) return newroot; int mid = l + r >> 1; if(num <= mid){ lson[newroot] = update(lson[root], l, mid, num, val); rson[newroot] = rson[root]; }else{ rson[newroot] = update(rson[root], mid + 1, r, num, val); lson[newroot] = lson[root]; } return newroot; } int query(int L, int R, int l, int r, int i) { if(l == L && r == R) return tree[i]; int mid = l + r >> 1; if(R <= mid) return query(L, R, l, mid, lson[i]); else if(L > mid) return query(L, R, mid + 1, r, rson[i]); else return query(L, mid, l, mid, lson[i]) + query(mid + 1, R, mid + 1, r, rson[i]); } int a[maxn]; int last[maxa]; int main (void) { int n; while(~scanf("%d", &n)){ tot = 0; t[0] = build(1, n); memset(last, -1, sizeof(last)); for(int i = 1; i <= n; ++i){ scanf("%d", &a[i]); if(last[a[i]] == -1){ t[i] = update(t[i - 1], 1, n, i, 1); }else{ t[i] = update(t[i - 1], 1, n, last[a[i]], -1); t[i] = update(t[i], 1, n, i, 1); } last[a[i]] = i; } int q;scanf("%d", &q); int l, r; while(q--){ scanf("%d%d", &l, &r); printf("%d\n", query(l, r, 1, n, t[r])); } } return 0; }
|