using s64 = longlong; using u64 = unsignedlonglong;
/* 取 min */ template <classT> inlinevoidchmin(T &x, const T &y){ if (x > y) { x = y; } } /* 取 max */ template <classT> inlinevoidchmax(T &x, const T &y){ if (x < y) { x = y; } }
/* ----- ----- ----- 正文 ----- ----- ----- */
constint N = 100100; constint SIZE = N * 2;
int n, m; int col[N];
structEdge { int x, y, z; } e[N];
int fa[SIZE]; intget(int x){ return fa[x] == x ? x : fa[x] = get(fa[x]); }
structTree { int t; int val[SIZE]; int son[SIZE][2], anc[18][SIZE]; int dep[SIZE], sze[SIZE]; int dfsClock, dfn[SIZE], idx[SIZE];
voidinit(){ t = n; for (int i = 1; i <= n * 2; i ++) { fa[i] = i; } }
voiddfs_init(int u, int fu){ dep[u] = dep[fu] + 1; sze[u] = 1; dfsClock ++, dfn[u] = dfsClock, idx[dfsClock] = u; if (u <= n) return; dfs_init(son[u][0], u), sze[u] += sze[son[u][0]]; dfs_init(son[u][1], u), sze[u] += sze[son[u][1]]; }
intlca(int x, int y){ if (!x || !y) return0; if (dep[x] > dep[y]) std::swap(x, y); for (int i = 17; i >= 0; i --) if (dep[x] <= dep[y] - (1 << i)) y = anc[i][y]; if (x == y) return x; for (int i = 17; i >= 0; i --) if (anc[i][x] ^ anc[i][y]) x = anc[i][x], y = anc[i][y]; return anc[0][x]; }
voiddeal(){ dfs_init(t, 0); for (int j = 1; j <= 17; j ++) { for (int i = 1; i <= t; i ++) { anc[j][i] = anc[j - 1][anc[j - 1][i]]; } } } } T1, T2;
voidbuild1(){ for (int i = 1; i < n; i ++) { e[i].z = std::min(e[i].x, e[i].y); } std::sort(e + 1, e + n, [&] (Edge lhs, Edge rhs) -> bool { return lhs.z > rhs.z; });
T1.init();
for (int i = 1; i < n; i ++) { auto [x, y, z] = e[i];
voidadd(int x, int y){ if (!x) return; for (; x <= T2.dfsClock; x += x & -x) { c[x] += y; } }
intask(int x){ int ans = 0; for (; x; x -= x & -x) { ans += c[x]; } return ans; }
intget(int x){ int l = T2.dfn[x], r = T2.dfn[x] + T2.sze[x] - 1; returnask(r) - ask(l - 1); } }
voidadd(int x){ int c = col[x]; auto it = g[c].lower_bound(T2.dfn[x]); int net = T2.idx[*it], pre = T2.idx[*std::prev(it)]; BIT::add(T2.dfn[x], +1); BIT::add(T2.dfn[T2.lca(pre, x)], -1); BIT::add(T2.dfn[T2.lca(x, net)], -1); BIT::add(T2.dfn[T2.lca(pre, net)], +1); g[c].insert(T2.dfn[x]); }
voiddec(int x){ int c = col[x]; auto it = g[c].lower_bound(T2.dfn[x]); int pre = T2.idx[*std::prev(it)], net = T2.idx[*std::next(it)]; BIT::add(T2.dfn[x], -1); BIT::add(T2.dfn[T2.lca(pre, x)], +1); BIT::add(T2.dfn[T2.lca(x, net)], +1); BIT::add(T2.dfn[T2.lca(pre, net)], -1); g[c].erase(it); }
std::pair<int, int> find(int l, int r, int x){ int p = x; for (int i = 17; i >= 0; i --) { if (T1.anc[i][p] && T1.val[T1.anc[i][p]] >= l) { p = T1.anc[i][p]; } } int q = x; for (int i = 17; i >= 0; i --) { if (T2.anc[i][q] && T2.val[T2.anc[i][q]] <= r) { q = T2.anc[i][q]; } } return {p, q}; }
std::vector< std::pair<int, int> > qry[SIZE]; int ans[N];
voidchange(int u, int opt){ if (u <= n) opt ? add(u) : dec(u); if (T1.son[u][0]) change(T1.son[u][0], opt); if (T1.son[u][1]) change(T1.son[u][1], opt); }
voidsolve(int u){ if (u > n) { if (T1.sze[T1.son[u][0]] < T1.sze[T1.son[u][1]]) { std::swap(T1.son[u][0], T1.son[u][1]); } solve(T1.son[u][1]), change(T1.son[u][1], 0); solve(T1.son[u][0]), change(T1.son[u][1], 1); } else { add(u); }