cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; cin >> m; blo = n / sqrt(m); for (int i = 1; i <= m; ++i) cin >> Q[i].l >> Q[i].r, Q[i].id = i; sort(Q + 1, Q + m + 1); int l = Q[1].l, r = l - 1; for (int i = 1; i <= m; ++i) { while (r < Q[i].r) add(a[++r]); while (l > Q[i].l) add(a[--l]); while (r > Q[i].r) del(a[r--]); while (l < Q[i].l) del(a[l++]); ans[Q[i].id] = Ans; } for (int i = 1; i <= m; ++i) cout << ans[i] << "\n"; return0; }
int ans, cnt[maxs]; inlinevoidadd(int v){ if (++cnt[v] == 1) ++ans; }
inlinevoiddel(int v){ if (--cnt[v] == 0) --ans; }
inlinevoidmodify(int i, int l, int r){ if (l <= C[i].k && C[i].k <= r) del(a[C[i].k]), add(C[i].v); swap(a[C[i].k], C[i].v); }
int Ans[maxn], cans; intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; blo = pow(n, 0.6666); for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) { char s[3]; int x, y; cin >> s + 1 >> x >> y; if (s[1] == 'Q') Q[++c1] = { x, y, ++cans, c2 }; else C[++c2] = { x, y }; } sort(Q + 1, Q + c1 + 1); int l = Q[1].l, r = l - 1, k = 0; for (int i = 1; i <= c1; ++i) { while (r < Q[i].r) add(a[++r]); while (l > Q[i].l) add(a[--l]); while (r > Q[i].r) del(a[r--]); while (l < Q[i].l) del(a[l++]); while (k < Q[i].k) modify(++k, l, r); while (k > Q[i].k) modify(k--, l, r); Ans[Q[i].id] = ans; } for (int i = 1; i <= cans; ++i) cout << Ans[i] << "\n"; return0; }
int b[maxn]; voidinit_hash(){ for (int i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = unique(b + 1, b + n + 1) - b - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b; }
structEdge { int to, next; } e[maxn * 2]; int c1, head[maxn]; inlinevoidadd_edge(int u, int v){ e[c1].to = v; e[c1].next = head[u]; head[u] = c1++; }
int f[maxn][21], dep[maxn], in[maxn], out[maxn], bl[maxn * 2], c2; voiddfs(int u, int fa){ in[u] = ++c2; bl[c2] = u; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa) continue; dep[v] = dep[u] + 1; f[v][0] = u; dfs(v, u); } out[u] = ++c2; bl[c2] = u; }
voidinit_lca(){ for (int j = 1; j <= 20; ++j) for (int i = 1; i <= n; ++i) f[i][j] = f[f[i][j - 1]][j - 1]; }
intget_lca(int x, int y){ if (dep[x] < dep[y]) swap(x, y); for (int i = 20; ~i; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i]; if (x == y) return x; for (int i = 20; ~i; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; return f[x][0]; }
int cnt[maxn], Ans[maxm], ans; bool use[maxn]; inlinevoidmodify(int k){ if (!use[k] && ++cnt[a[k]] == 1) ++ans; if (use[k] && --cnt[a[k]] == 0) --ans; use[k] ^= 1; }
intmain(){ fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; blo = sqrt(n); for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; add_edge(x, y); add_edge(y, x); } init_hash(); dep[1] = 1; dfs(1, 0); init_lca(); for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; if (in[x] > in[y]) swap(x, y); if (in[x] <= in[y] && out[y] <= out[x]) Q[i] = { in[x], in[y], 0, i }; else Q[i] = { out[x], in[y], in[get_lca(x, y)], i }; } sort(Q + 1, Q + m + 1); int l = Q[1].l, r = l - 1; for (int i = 1; i <= m; ++i) { while (r < Q[i].r) modify(bl[++r]); while (l > Q[i].l) modify(bl[--l]); while (r > Q[i].r) modify(bl[r--]); while (l < Q[i].l) modify(bl[l++]); if (Q[i].g) modify(bl[Q[i].g]); Ans[Q[i].id] = ans; if (Q[i].g) modify(bl[Q[i].g]); } for (int i = 1; i <= m; ++i) cout << Ans[i] << "\n"; return0; }
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #define maxn 100010 #define ll long long usingnamespacestd;
int n, m, k, a[maxn];
int b[maxn]; voidinit_hash(){ for (int i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = unique(b + 1, b + n + 1) - b - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b; }
int blo, bl[maxn], R[maxn]; structQuery { int l, r, k, id;
cin >> n >> m; blo = sqrt(n); for (int i = 1; i <= n; ++i) cin >> a[i]; init_hash(); for (int i = 1; i <= m; ++i) cin >> Q[i].l >> Q[i].r, Q[i].id = i; for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / blo + 1, R[bl[i]] = bl[i] * blo; sort(Q + 1, Q + m + 1); for (int o = 1, p = 1; o <= m; o = p) { while (bl[Q[o].l] == bl[Q[p].l]) ++p; ll ans = 0; int r = R[bl[Q[o].l]], _r = r; fill(s1, s1 + maxn, 0); for (int i = o; i < p; ++i) { while (r < Q[i].r) add(a[++r], ans); Ans[Q[i].id] = ans; for (int j = Q[i].l; j <= min(Q[i].r, _r); ++j) Add(a[j], Ans[Q[i].id]); for (int j = Q[i].l; j <= min(Q[i].r, _r); ++j) s2[a[j]] = 0; } } for (int i = 1; i <= m; ++i) cout << Ans[i] << "\n"; return0; }
#include<iostream> #include<algorithm> #include<vector> #include<cmath> #define maxn 100010 #define maxs 340 #define lowbit(i) ((i) & (-i)) #define ll long long usingnamespacestd;
int n, m, a[maxn];
ll pre[maxn], suf[maxn];
int b[maxn], cnt; voidinit_hash(){ for (int i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); cnt = unique(b + 1, b + n + 1) - b - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b; }
int Bit[maxn]; voidadd(int i, int v){ while (i <= cnt) Bit[i] += v, i += lowbit(i); }
intget_sum(int i){ int s = 0; while (i) s += Bit[i], i -= lowbit(i); return s; }