LOJ-2001-Luogu-3703-BZOJ-4817-「SDOI2017」树点涂色

题目

https://loj.ac/problem/2001

https://www.luogu.org/problemnew/show/P3703

题意

略。

题解

  • 如果把操作 1 看作是 LCT 的 access 操作,那么路径的权值就是路径上的虚边条数加一。
  • 用线段树维护每个结点到根的虚边条数,access 中轻重边的切换就对应于子树的虚边条数区间加减。
  • 操作 3 就是区间最大值,操作 2 就是单点查询 + LCA。
  • 复杂度 \(O(n\log ^2 n)\)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0)
#else
#define dbg(...)
#endif
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); }
template<typename T, typename... Args>
void err(T a, Args... args) { cout << a << ' '; err(args...); }
// -----------------------------------------------------------------------------
const int maxn = 1E5 + 100;
int n, Q;
vector<int> G[maxn];

namespace sg {
struct Q {
int add;
Q(int add = 0): add(add) {}
void operator += (Q& q) { add += q.add; }
};
struct P {
int max;
explicit P(int max = 0): max(max) {}
void up(Q& q) { max += q.add; }
};
template<typename T>
P operator & (T&& a, T&& b) {
return P(max(a.max, b.max));
}
P p[maxn << 2];
Q q[maxn << 2];
#define lson o * 2, l, (l + r) / 2
#define rson o * 2 + 1, (l + r) / 2 + 1, r
void maintain(int o, int l, int r) {
if (l == r) p[o] = P();
else p[o] = p[o * 2] & p[o * 2 + 1];
p[o].up(q[o]);
}
void pushdown(int o, int l, int r) {
q[o * 2] += q[o]; q[o * 2 + 1] += q[o];
q[o] = Q();
maintain(lson); maintain(rson);
}
template<typename T>
void build(T f, int o = 1, int l = 1, int r = n) {
if (l == r) q[o] = f(l);
else { build(f, lson); build(f, rson); }
maintain(o, l, r);
}
P query(int ql, int qr, int o = 1, int l = 1, int r = n) {
if (ql > r || l > qr) return P();
if (ql <= l && r <= qr) return p[o];
pushdown(o, l, r);
return query(ql, qr, lson) & query(ql, qr, rson);
}
void update(int ql, int qr, Q v, int o = 1, int l = 1, int r = n) {
if (ql > r || l > qr) return;
if (ql <= l && r <= qr) q[o] += v;
else {
pushdown(o, l, r);
update(ql, qr, v, lson); update(ql, qr, v, rson);
}
maintain(o, l, r);
}
}

int fa[maxn], dep[maxn], idx[maxn], out[maxn], ridx[maxn];
namespace hlc {
int sz[maxn], son[maxn], top[maxn], clk;
void predfs(int u, int d) {
dep[u] = d; sz[u] = 1;
int& maxs = son[u] = -1;
for (int& v: G[u]) {
if (v == fa[u]) continue;
fa[v] = u;
predfs(v, d + 1);
sz[u] += sz[v];
if (maxs == -1 || sz[v] > sz[maxs]) maxs = v;
}
}
void dfs(int u, int tp) {
top[u] = tp; idx[u] = ++clk; ridx[clk] = u;
if (son[u] != -1) dfs(son[u], tp);
for (int& v: G[u])
if (v != fa[u] && v != son[u]) dfs(v, v);
out[u] = clk;
}
int go(int u, int v) {
int uu = top[u], vv = top[v];
while (uu != vv) {
if (dep[uu] < dep[vv]) { swap(uu, vv); swap(u, v); }
u = fa[uu]; uu = top[u];
}
if (dep[u] < dep[v]) swap(u, v);
return v;
}
}

namespace lct {
struct P {
P *fa, *ls, *rs;
int idx;
bool has_fa() { return fa->ls == this || fa->rs == this; }
bool d() { return fa->ls == this; }
P*& c(bool x) { return x ? ls : rs; }
} *null = new P{nullptr, nullptr, nullptr}, pool[maxn], *pit = pool;
P* rt[maxn];
void rot(P* o) {
bool dd = o->d();
P *f = o->fa, *t = o->c(!dd);
if (f->has_fa()) f->fa->c(f->d()) = o; o->fa = f->fa;
if (t != null) t->fa = f; f->c(dd) = t;
o->c(!dd) = f; f->fa = o;
}
void splay(P* o) {
while (o->has_fa()) {
if (o->fa->has_fa()) rot(o->d() xor o->fa->d() ? o : o->fa);
rot(o);
}
}
void calc(P* o, int v) {
if (o == null) return;
while (o->ls != null) o = o->ls;
int x = o->idx;
sg::update(idx[x], out[x], v);
}
void access(P* u, P* v = null) {
if (u == null) return;
splay(u);
P* t = u->rs;
calc(v, -1);
u->rs = v;
calc(t, 1);
access(u->fa, u);
}
}


int main() {
cin >> n >> Q;
FOR (_, 1, n) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v); G[v].push_back(u);
}
hlc::predfs(1, 0); hlc::dfs(1, 1);
FOR (i, 1, n + 1) {
using namespace lct;
rt[i] = new (pit++) P{i == 1 ? null: rt[fa[i]], null, null, i};
}
sg::build([](int x)->sg::Q { return sg::Q(dep[ridx[x]]); });
while (Q--) {
int op; scanf("%d", &op);
if (op == 1) {
int u; scanf("%d", &u);
lct::access(lct::rt[u]);
} else if (op == 2) {
int u, v; scanf("%d%d", &u, &v);
int lca = hlc::go(u, v);
u = idx[u]; v = idx[v]; lca = idx[lca];
int ans = sg::query(u, u).max + sg::query(v, v).max - 2 * sg::query(lca, lca).max;
printf("%d\n", ans + 1);
} else {
int u; scanf("%d", &u);
printf("%d\n", sg::query(idx[u], out[u]).max + 1);
}
}
}