# bzoj4817 & loj2001 [Sdoi2017]树点涂色 LCT + 线段树

## 题目传送门

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

https://loj.ac/problem/2001

## 题解

#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 = 100000 + 7;

int n, m, dfc;
int dep[N], f[N], siz[N], son[N], dfn[N], pre[N], top[N];

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; }

namespace SGT {
#define lc o << 1
#define rc o << 1 | 1
struct Node { int add, max; } t[N << 2];
inline void build(int o, int L, int R) {
if (L == R) return t[o].max = dep[pre[L]], (void)0;
int M = (L + R) >> 1;
build(lc, L, M), build(rc, M + 1, R);
t[o].max = std::max(t[lc].max, t[rc].max);
}
inline void qadd(int o, int L, int R, int l, int r, int k) {
if (l <= L && R <= r) return t[o].add += k, t[o].max += k, (void)0;
int M = (L + R) >> 1;
if (l <= M) qadd(lc, L, M, l, r, k);
if (r > M) qadd(rc, M + 1, R, l, r, k);
t[o].max = std::max(t[lc].max, t[rc].max) + t[o].add;
}
inline int qmax(int o, int L, int R, int l, int r) {
if (l <= L && R <= r) return t[o].max;
int M = (L + R) >> 1;
if (r <= M) return qmax(lc, L, M, l, r) + t[o].add;
if (l > M) return qmax(rc, M + 1, R, l, r) + t[o].add;
return std::max(qmax(lc, L, M, l, r), qmax(rc, M + 1, R, l, r)) + t[o].add;
}
#undef lc
#undef rc
}

namespace LCT {
#define lc c[0]
#define rc c[1]
struct Node { int c[2], fa; } t[N];
inline bool isroot(int o) { return t[t[o].fa].lc != o && t[t[o].fa].rc != o; }
inline bool idtfy(int o) { return t[t[o].fa].rc == o; }
inline void connect(int fa, int o, int d) { t[fa].c[d] = o, t[o].fa = fa; }
inline void rotate(int o) {
int fa = t[o].fa, pa = t[fa].fa, d1 = idtfy(o), d2 = idtfy(fa), b = t[o].c[d1 ^ 1];
if (!isroot(fa)) t[pa].c[d2] = o; t[o].fa = pa;
connect(o, fa, d1 ^ 1), connect(fa, b, d1);
}
inline void splay(int o) {
while (!isroot(o)) {
int fa = t[o].fa;
if (isroot(fa)) rotate(o);
else if (idtfy(o) == idtfy(fa)) rotate(fa), rotate(o);
else rotate(o), rotate(o);
}
}
inline void access(int o) {
for (int x = 0; o; o = t[x = o].fa) {
splay(o);
if (t[o].rc) {
int p = t[o].rc;
while (t[p].lc) p = t[p].lc;
SGT::qadd(1, 1, n, dfn[p], dfn[p] + siz[p] - 1, 1);
splay(p), splay(o);
}
t[o].rc = x;
if (t[o].rc) {
int p = t[o].rc;
while (t[p].lc) p = t[p].lc;
SGT::qadd(1, 1, n, dfn[p], dfn[p] + siz[p] - 1, -1);
splay(p), splay(o);
}
}
}
#undef lc
#undef rc
}

inline void dfs1(int x, int fa = 0) {
dep[x] = dep[fa] + 1, f[x] = fa, siz[x] = 1, LCT::t[x].fa = fa;
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) {
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;
}

inline void work() {
dfs1(1), dfs2(1, 1);
SGT::build(1, 1, n);
while (m--) {
int opt, x, y, p;
if (opt == 1) read(x), LCT::access(x);
else if (opt == 2) read(x), read(y), p = lca(x, y), printf("%d\n", SGT::qmax(1, 1, n, dfn[x], dfn[x]) + SGT::qmax(1, 1, n, dfn[y], dfn[y]) - (SGT::qmax(1, 1, n, dfn[p], dfn[p]) << 1) + 1);
else read(x), printf("%d\n", SGT::qmax(1, 1, n, dfn[x], dfn[x] + siz[x] - 1));
}
}

inline void init() {
int x, y;