Title Link hh Necklace
This problem can be directly on the chairman of the tree template
#include <bits/stdc++.h>using namespace std; #define REP (I, A, b) for (int i (a); I <= (b); ++i) #define DEC (i, a , b) for (int i (a); I >= (b); i.) typedef long LONG ll;const int N = 5e4 + 10;const int M = 3e6 + 10;int N, tot, Q, A[n];int T[m], lson[m], rson[m], Val[m];int nxt[n], b[n];int m;int build (int l, int r) {int rt = tot++; VAL[RT] = 0; int m = (L + r) >> 1; if (L = r) {Lson[rt] = build (L, m); RSON[RT] = build (M + 1, R); } return RT; } int update (int rt, int pos, int v) {int newrt = tot++, tmp = newrt;int L = 1, r = n;val[newrt] = Val[rt] + v;while (L & Lt R) {int m = (L + R) >> 1;if (pos <= m) {Lson[newrt] = tot++;rson[newrt] = Rson[rt];newrt = Lson[newrt];rt = Lson[rt] ; r = m;} ELSE{RSON[NEWRT] = Tot++;lson[newrt] = Lson[rt];newrt = Rson[newrt];rt = Rson[rt];l = m + 1;} VAL[NEWRT] = Val[rt] + V;} return TMP;} int query (int rt, int pos) {int ret = 0;int L = 1, R = N;while (pos > L) {int m = (L + R) >> 1;if (pos <= m) {RET + = Val[rson[rt]];rt = Lson[rt];r = m;} else{L = m + 1;rt = Rson[rt];}} return ret + VAL[RT];} int ask (int l, int r) {return query (T[r], l);} void init () {tot = 0;memset (NXT,-1, sizeof (NXT)); Rep (i, 1, n) b[i-1] = A[i];sort (b, B + n); int cnt = unique (b, B + N)-B; T[0] = build (1, N); Rep (i, 1, n) {int id = Lower_bound (b, B + CNT, a[i])-b;if (Nxt[id] = 1) t[i] = Update (t[i-1], I, 1); Else{int t = Update (T[I-1], Nxt[id],-1); T[i] = update (t, I, 1);} Nxt[id] = i;}} int main () {scanf ("%d", &n), Rep (i, 1, N) scanf ("%d", A + i), init (), scanf ("%d", &m), while (m--) {int x, y;scanf ("%d%d ", &x, &y);p rintf ("%d\n ", ask (x, y));} return 0;}
Of course, you can do it with the MO team algorithm.
#include <bits/stdc++.h>using namespace std; #define REP (I, A, b) for (int i (a); I <= (b); ++i) #define DEC (i, A, b) for (int i (a); I >= (b); i.) typedef long LONG ll;const int N = 2e5 + 10;int belong[n];int a[n], b[n], c[n], Ans[n];int N, M, L, R, cnt, Bs;int now = 0;struct Node{int L, R, id;friend bool operator < (const node &a, const node &B) {return BELONG[A.L] = = BELONG[B.L]? BELONG[A.R] < BELONG[B.R]: Belong[a.l] < BELONG[B.L];}} Q[n];int Main () {scanf ("%d", &n), BS = (int) sqrt (N + 0.5), Rep (i, 1, N) scanf ("%d", A + i), b[i] = A[i];sort (b + 1, B + N + 1); cnt = unique (b + 1, B + n + 1)-B-1;rep (i, 1, n) a[i] = Lower_bound (b + 1, B + CNT + 1, a[i])-b;scanf ("%d", &am P;M); Rep (I, 1, m) {scanf ("%d%d", &Q[I].L, &Q[I].R); q[i].id = i;} Rep (i, 1, n) belong[i] = (i-1)/BS + 1;sort (q + 1, q + M + 1); l = 0, r = 0;rep (i, 1, m) {while (L > Q[i].l) {--l; if (C[a[l]] = = 0) ++now; ++c[a[l]]; }while (R < Q[I].R) {++r; if (c[a[r]] = = 0) ++now; ++c[a[r]]; }while (L < Q[I].L) {--c[a[l]]; if (!c[a[l]])--now; ++l;} while (R > Q[I].R) {--c[a[r]] [if (!c[a[r]])--now;--r;} Ans[q[i].id] = Now;} Rep (i, 1, m) printf ("%d\n", Ans[i]); return 0;}
Bzoj 1878 [Sdoi2009]hh's Necklace (Chairman tree or MO team algorithm)