很简单的树状数组,又不是线段树,你们应该愿意看吧(
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 7;
int n, m;
struct Bit{
#define lb(i) (i & -i)
int t[MAXN];
void modify(int x, int y) {
for (; x <= n; x += lb(x)) t[x] += y;
}
int querydd(int x) {
int ans = 0;
for (; x; x -= lb(x)) ans += t[x];
return ans;
}
int query(int x, int y) {return querydd(y) - querydd(x - 1); }
}t;
struct Node {
int l, r;
bool operator < (const Node other) const {
return r < other.r;
}
}A[MAXN];
int B[MAXN], Last[MAXN];//前一个数的下标
int V[MAXN], Ans[MAXN];
int main () {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> B[i];
cin >> m;
for (int i = 1; i <= m; i ++) cin >> A[i].l >> A[i].r, V[A[i].r] = i;
sort(A + 1, A + 1 + m);
for (int i = 1; i <= n; i ++) {
if (Last[B[i]] != 0) t.modify(Last[B[i]], -1);
Last[B[i]] = i; t.modify(Last[B[i]], 1);
if (V[i]) Ans[V[i]] = t.query(A[V[i]].l, A[V[i]].r);
}
for (int i = 1; i <= m; i ++) cout << Ans[i] << '\n';
return 0;
}
实在不行提供hack也行,蟹蟹你们啦