题干:
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.
Input
- Line 1: n (1 ≤ n ≤ 30000).
- Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
- Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
- In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).
Output
- For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.
Example
Input
5
1 1 2 1 3
3
1 5
2 4
3 5
Output
3
2
3解题报告:
AC代码:(树状数组,mp存位置,其实也可以用一个pre数组去存位置,(需要先都初始化成-1),用时分别是170ms和150ms)
using namespace std;
map<int ,int> mp;
struct Node {
    int l,r;
    int id;
} node[200000 + 5];
int n,m,c[30000 + 5],ans[200000 + 5];
int a[30000 + 5];
int lowbit(int x) {
    return x&-x;
}
void update(int x,int val) {
    while(x<=n) {
        c[x] += val;
        x+=lowbit(x);
    }
}
int query(int x) {
    int res = 0;
    while(x>0) {
        res += c[x];
        x-=lowbit(x);
    }
    return res;
}
bool cmp(const Node & a,const Node & b) {
    return a.r < b.r;
}
int main()
{
    while(~scanf("%d",&n) ) {
        memset(c,0,sizeof c);
        mp.clear();
        for(int i = 1; i<=n; i++) {
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        for(int i = 1; i<=m; i++) {
            scanf("%d%d",&node[i].l,&node[i].r);
            node[i].id = i;
        }
        int cur = 1;
        sort(node+1,node+m+1,cmp);
        for(int i = 1; i<=m; i++) {
            while(cur <= node[i].r) {
                if(mp[a[cur] ] == 0) update(cur,1);
                else {
                    update(mp[a[cur] ] ,-1);
                    update(cur,1);
                }
                mp[a[cur] ] = cur;
                cur++;
            }
            ans[node[i].id] = query(node[i].r) - query(node[i].l - 1);
        }
        for(int i = 1; i<=m; i++) {
            printf("%d\n",ans[i]);
        }
        
    }
    return 0 ;
}AC代码2:
using namespace std;
const int MAX = 30000 + 5;
struct TREE {
  int l,r;
  int val;
} tree[MAX * 40];
int n,tot;
int pre[1000000 + 5],root[MAX],a[MAX];
int build(int l,int r) {
  int ne = ++tot;
  tree[ne].val = 0;
  tree[ne].l=tree[ne].r = 0;
  if(l == r) return ne;
  int m = (l+r)/2;
  tree[ne].l = build(l,m);
  tree[ne].r = build(m+1,r);
  return ne;
}
int update(int pos,int c,int val,int l,int r) {
  int ne = ++tot;
  tree[ne] = tree[c];
  tree[ne].val += val;
  if(l == r) return ne;
  int m = (l+r)/2;
  if(m >= pos) tree[ne].l = update(pos,tree[c].l,val,l,m);
  else tree[ne].r = update(pos,tree[c].r,val,m+1,r);
  return ne; 
  
}
int query(int pos,int c,int l,int r) {
  if(l == r) return tree[c].val;
  int m = (l+r)/2;
  if(m>=pos) return tree[tree[c].r].val + query(pos,tree[c].l,l,m);
  else return query(pos,tree[c].r,m+1,r);
}
int main()
{
  cin>>n;
  memset(pre,-1,sizeof pre);
  for(int i = 1; i<=n; i++) scanf("%d",a+i);
  root[0] = build(1,n);
  //构造主席树 
  for(int i = 1; i<=n; i++) {
    if(pre[a[i]] == -1) {
      root[i] = update(i,root[i-1],1,1,n);
    }
    else {
      int tmp = update(pre[a[i]],root[i-1],-1,1,n);
      root[i] = update(i,tmp,1,1,n);  
    }
    pre[a[i]]=i;
  }
  int q,x,y;
  cin>>q;
  while(q--) {
    scanf("%d%d",&x,&y);
    printf("%d\n",query(x,root[y],1,n));
  }
  return 0 ;
}莫队算法:(还未做、。、)
                










