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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <algorithm> #include <string> #define INF 0x3f3f3f3f #define NO 600100 #define MO 1000005 typedef long long ll;
using namespace std; ll read() { char ch = ' ', last; ll ans = 0; while (ch < '0' || ch > '9') last = ch, ch = getchar(); while (ch >= '0' && ch <= '9') ans = ans * 10 + int(ch - '0'), ch = getchar(); if (last == '-') return -ans; return ans; }
int n, a[NO], m, d[NO], last[MO]; struct node { int L, R, pos, ans; }asks[NO];
int lowbit(int x) { return x & -x; } void add(int val, int pos) { for(int i = pos; i <= n; i += lowbit(i)) d[i] += val; } int ask(int pos) { int ans = 0; for(int i = pos; i; i -= lowbit(i)) ans += d[i]; return ans; } bool cmp1(node i, node j) { return i.R < j.R; } bool cmp2(node i, node j) { return i.pos < j.pos; } void init() { n = read(); for(int i = 1; i <= n; i++) a[i] = read(); m = read(); for(int i = 1; i <=m; i++) asks[i].L = read(), asks[i].R = read(), asks[i].pos = i; sort(asks + 1, asks + m + 1, cmp1); }
int main() { init(); memset(last, 0, sizeof(last)); int now = 1; for(int i = 1; i <= m; i++) { for(; now <= asks[i].R; now++) { if(last[a[now]]) add(-1, last[a[now]]); add(1, now), last[a[now]] = now; } asks[i].ans = ask(asks[i].R) - ask(asks[i].L - 1); } sort(asks + 1, asks + m + 1, cmp2); for(int i = 1; i <= m; i++) cout << asks[i].ans << endl; return 0; }
|