1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <cstdio> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <queue> #include <cstring> #include <string> #include <cstdlib> #include <cmath> #include <set> using namespace std; const int inf = (1 << 30); typedef long long ll; const int maxn = 1e5 + 5; vector<int> vec; struct node { int ls, rs, sum; } tr[maxn * 50]; int rt[maxn], cnt; void inser(int ver, int &now, int L, int R, int pos, int w) { now = ++cnt; tr[now] = tr[ver]; tr[now].sum = tr[ver].sum + w; if (L == R) return; int mid = L + R >> 1; if (mid >= pos) inser(tr[ver].ls, tr[now].ls, L, mid, pos, w); else inser(tr[ver].rs, tr[now].rs, mid + 1, R, pos, w); } int ask(int now, int L, int R, int ql, int qr) { if (L >= ql && R <= qr) return tr[now].sum; int mid = L + R >> 1; int ans = 0; if (ql <= mid) ans += ask(tr[now].ls, L, mid, ql, qr); if (qr > mid) ans += ask(tr[now].rs, mid + 1, R, ql, qr); return ans; } map<int, int> mp; int main() { ios::sync_with_stdio(false); cin.tie(0); mp.clear(); int n, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; if (!mp[x]) inser(rt[i - 1], rt[i], 1, n, i, 1); else inser(rt[i - 1], rt[i], 1, n, mp[x], -1), inser(rt[i], rt[i], 1, n, i, 1); mp[x] = i; } int m; cin >> m; while (m--) { int ql, qr; cin >> ql >> qr; cout << ask(rt[qr], 1, n, ql, qr) << endl; } }
|