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
| #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5;
int n, q, a[N], b[N], c[N], ans[N];
struct Node { int l, r, id; } node[N];
bool cmp(Node x, Node y) { return x.r < y.r; }
void update(int x, int y) { for (; x <= n; x += x & (-x)) c[x] += y; }
int query(int x) { int sum = 0; for (; x >= 1; x -= x & (-x)) sum += c[x]; return sum; }
int main() { while (~scanf("%d%d", &n, &q)) { for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i + n] = a[i]; } memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); for (int i = 1; i <= q; i++) { scanf("%d%d", &node[i].r, &node[i].l); node[i].r += n; node[i].id = i; } sort(node + 1, node + q + 1, cmp); n <<= 1; int pre = 1; for (int i = 1; i <= q; i++) { for (int j = pre; j <= node[i].r; j++) { if (b[a[j]]) update(b[a[j]], -1); b[a[j]] = j; update(b[a[j]], 1); } pre = node[i].r + 1; ans[node[i].id] = query(node[i].r) - query(node[i].l - 1); } for (int i = 1; i <= q; i++) printf("%d\n", ans[i]); } return 0; }
|