# [BZOJ 1878] [SDOI2009] HH的项链

### 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int MaxN = 50000 + 5, MaxQ = 200000 + 5, MaxType = 1000000 + 5;

int n, m, a, b;
int A[MaxN], Next[MaxN], T[MaxN], Last[MaxType], Ans[MaxQ];

struct Query
{
int l, r, num;
} Q[MaxQ];

bool cmp(Query q1, Query q2) {
return q1.l < q2.l;
}

void Add(int x, int num) {
for (int i = x; i <= n; i += i & (-i))
T[i] += num;
}

int Get(int x) {
int ret = 0;
for (int i = x; i >= 1; i -= i & (-i))
ret += T[i];
return ret;
}

int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
Q[i].l = a; Q[i].r = b;
Q[i].num = i;
}
sort(Q + 1, Q + m + 1, cmp);
memset(T, 0, sizeof(T));
memset(Last, 0, sizeof(Last));
for (int i = 1; i <= n; i++) {
if (Last[A[i]] == 0) Add(i, 1);
else Next[Last[A[i]]] = i;
Last[A[i]] = i;
}
int x = 1;
for (int i = 1; i <= m; i++) {
while (x < Q[i].l) {