fork download
#include <bits/stdc++.h>

using namespace std;

int const N = 30 * 1000 + 16;

int n, m;
int a[N], b[N], last[N];

struct tree {
  int v;
  tree *lf, *rg;
  
  tree(tree* tmp) {
    *this = *tmp;
  }
  
  tree(int l, int r) {
    v = 0;
    if(l == r)
      return;
    int m = l + r >> 1;
    lf = new tree(l, m);
    rg = new tree(m+1, r);
  }
  
  int query(int l, int r, int L, int R) {
    if(L <= l && r <= R)
      return v;
    int m = l + r >> 1;
    if(R <= m)
      return lf->query(l, m, L, R);
    else if(m < L)
      return rg->query(m+1, r, L, R);
    else
      return lf->query(l, m, L, m) + rg->query(m+1, r, m+1, R);
  }
  
  tree* update(int l, int r, int i, int v) {
    auto tmp = new tree(this);
    if(l == i && i == r) {
      tmp->v = v;
    } else {
      int m = l+r>>1;
      if(i <= m)
        tmp->lf = lf->update(l, m, i, v);
      else
        tmp->rg = rg->update(m+1, r, i, v);
      tmp->v = tmp->lf->v + tmp->rg->v;
    }    
    return tmp;
  }
};

tree* ver[N];

int main()
{
  scanf("%d", &n);
  for(int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    a[i] = b[i-1] = x;
  }
  sort(b, b+n);
  m = unique(b, b+n) - b;
  
  ver[0] = new tree(1, n);
  
  for(int i = 1; i <= n; ++i) {
    int x = lower_bound(b, b+m, a[i]) - b;
    auto old = ver[i-1];
    if(last[x])
      old = old->update(1, n, last[x], 0);
    ver[i] = old->update(1, n, i, 1);
    last[x] = i;
  }
  
  int q;
  scanf("%d", &q);
  while(q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", ver[r]->query(1, n, l, r));
  }
}
Success #stdin #stdout 0s 3956KB
stdin
5
1 1 2 1 3
3
1 5
2 4
3 5
stdout
3
2
3