# bzoj4771 七彩树 dfs序+主席树+树链的并

## 题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4771

## 题解

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}

const int N = 1e5 + 7;

int n, m, nod, dfc, cnt;
int c[N], rt[N], q[N];
int dep[N], f[N], siz[N], son[N], dfn[N], pre[N], top[N];

struct Node { int lc, rc, val; } t[N * 70];
inline void ins(int &o, int L, int R, int x, int k) {
++cnt;
t[++nod] = t[o], t[o = nod].val += k;
if (L == R) return;
int M = (L + R) >> 1;
if (x <= M) ins(t[o].lc, L, M, x, k);
else ins(t[o].rc, M + 1, R, x, k);
}
inline int qsum(int o, int L, int R, int l, int r) {
++cnt;
if (!o) return 0;
if (l <= L && R <= r) return t[o].val;
int M = (L + R) >> 1;
if (r <= M) return qsum(t[o].lc, L, M, l, r);
if (l > M) return qsum(t[o].rc, M + 1, R, l, r);
return qsum(t[o].lc, L, M, l, r) + qsum(t[o].rc, M + 1, R, l, r);
}

inline void qadd(int &o, int x, int k) { /*dbg("x = %d, k = %d\n", x, k);*/ ins(o, 1, n, dfn[x], k); }
inline int qval(int o, int x) { return qsum(o, 1, n, dfn[x], dfn[x] + siz[x] - 1); }

struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }

inline void dfs1(int x, int fa = 0) {
dep[x] = dep[fa] + 1, f[x] = fa, siz[x] = 1;
for fec(i, x, y) if (y != fa) dfs1(y, x), siz[x] += siz[y], siz[y] > siz[son[x]] && (son[x] = y);
}
inline void dfs2(int x, int pa) {
top[x] = pa, dfn[x] = ++dfc, pre[dfc] = x;
if (!son[x]) return; dfs2(son[x], pa);
for fec(i, x, y) if (y != f[x] && y != son[x]) dfs2(y, y);
}
inline int lca(int x, int y) {
++cnt;
while (top[x] != top[y]) dep[top[x]] > dep[top[y]] ? x = f[top[x]] : y = f[top[y]];
return dep[x] < dep[y] ? x : y;
}

struct cmp {
inline bool operator () (const int &x, const int &y) {++cnt; return dfn[x] < dfn[y]; }
};
std::set<int, cmp> s[N];

inline void ins(std::set<int, cmp> &s, int dep, int x) {
std::set<int, cmp>::iterator p = s.lower_bound(x);
int y = 0, z = 0;
if (p != s.end()) y = *p, qadd(rt[dep], lca(x, y), -1);
if (p != s.begin()) z = *--p, qadd(rt[dep], lca(x, z), -1);
if (y && z) qadd(rt[dep], lca(y, z), 1);
// dbg("ins: dep = %d, x = %d, y = %d, z = %d, qry = %d, rt[dep] = %d\n", dep, x, y, z, qval(rt[dep], x), rt[dep]);
s.insert(x);
}

inline void bfs() {
int hd = 0, tl = 1;
q[tl] = 1;
while (hd < tl) {
int x = q[++hd];
for fec(i, x, y) if (y != f[x]) q[++tl] = y;
}
}
inline void build() {
for (int i = 1; i <= n; ++i) {
int x = q[i];
if (dep[x] != dep[q[i - 1]]) rt[dep[x]] = rt[dep[x] - 1];
ins(s[c[x]], dep[x], x);
}
}

inline void work() {
dfs1(1), dfs2(1, 1);
bfs();
build();
int la = 0;
while (m--) {
int x, d;
x ^= la, d ^= la;
// dbg("x = %d, d = %d\n", x, d);
d += dep[x], smin(d, dep[q[n]]);
printf("%d\n", la = qval(rt[d], x));
// la = qval(rt[d], x);
}
memset(t, 0, sizeof(Node) * (nod + 1));
}

inline void cls() {
tot = nod = dfc = 0;
memset(son, 0, sizeof(son));
for (int i = 1; i <= n; ++i) s[i].clear();
}

inline void init() {
cls();
for (int i = 1; i <= n; ++i) read(c[i]);
for (int i = 2, x; i <= n; ++i) read(x), addedge(x, i);
}

int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
int T;