树链剖分
先不讲树剖,从 dfs 序开始
dfs 序的应用
很显然的就不说了,分析加法对求的东西的贡献,因为区间操作基本只能对子树,子树的 dfs 序连续
把路径加路径查用差分转换为到根的路径,默认维护路径的都是维护到根的
-
路径加,单点查询:考虑某个点只有在从根出发的路径的路径终点在子树内时才会被加,因此转化为 dfs 序序列上单点加,子树查询
-
单点加,路径查询:此时用欧拉序,每个节点有
in,out 两个位置,加时把in 加val ,把out 减val ,查询则查询1\sim in_x 的和 -
子树加,路径查询:对
x 加,会对x 子树内y 到根的路径产生(dep_y-dep_x+1)\times val 的贡献,拆开,维护\sum val,\sum dep_x\times val ,转化为子树加,单点查询 -
子树加,子树查询:区间加区间查询
-
路径加,子树查询:
y 的贡献来自y 子树内x 被加过,产生(dep_x-dep_y+1)\times val 的贡献,同理拆开维护,转化为单点加,子树查询
但是,如果是路径修改,路径查询呢?
树链剖分
前置知识:dfs 序,线段树
这里定义:
重儿子:一个节点儿子中子树大小最大的儿子(叶子没有儿子,即无重儿子)
轻儿子:剩下的
重边:连接任意两个重儿子的边叫做重边
轻边:剩下的
重链:相邻重边连起来的连接一条重儿子的链叫重链
流程:
首先预处理各种内容,包括重儿子
inline void dfs1(ll x, ll fath)
{
fa[x] = fath, siz[x] = 1;
for(reg ll i = 0; i < (ll)edge[x].size(); ++i)
{
if(edge[x][i] == fath) continue;
dis[edge[x][i]] = dis[x] + 1;
dfs1(edge[x][i], x);
siz[x] += siz[edge[x][i]];
if(siz[edge[x][i]] > siz[mson[x]]) mson[x] = edge[x][i];
}
}
然后关键是处理出每个点所在链的开头,这里轻儿子有一条以它自己开头的链
注意这里我们给每个点用 dfs 序添加新编号,作为它在线段树上的位置编号
要先遍历每个点的重儿子(后面说原因)
inline void dfs2(ll x, ll tp)
{
dfn[x] = ++cnt, top[x] = tp; // tp 即为链的开头编号
if(!mson[x]) return; // 叶子,返回
dfs2(mson[x], tp); // 先遍历重儿子
for(reg ll i = 0; i < (ll)edge[x].size(); ++i)
if(edge[x][i] != mson[x] && edge[x][i] != fa[x]) dfs2(edge[x][i], edge[x][i]);
}
以 P3384 【模板】轻重链剖分/树链剖分 为例
修改 x->y 的路径:
这里先遍历重儿子的好处凸显:每条重链的dfs序编号连续!
就很方便在线段树上执行区间操作了
我们直接暴力跳重链,直到
这里每次跳过的重链条数不超过
inline void treeadd(ll x, ll y, ll val)
{
while(top[x] != top[y]) // x和y不在一条重链上
{
if(dis[top[x]] < dis[top[y]]) swap(x, y);
// x更新为所在重链开头更深的节点,确保每次向上跳不会错过
update(dfn[top[x]], dfn[x], val, 1, n, 1); // 线段树的修改
x = fa[top[x]]; // x到所在重链的开头的父节点
}
if(dis[x] > dis[y]) swap(x, y); // x更新为深度小的
update(dfn[x], dfn[y], val, 1, n, 1); // 一条重链上,区间修改
}
那查询就完全同理了~
inline ll treeask(ll x, ll y)
{
ll res = 0;
while(top[x] != top[y])
{
if(dis[top[x]] < dis[top[y]]) swap(x, y);
res = res + query(dfn[top[x]], dfn[x], 1, n, 1);
x = fa[top[x]];
}
if(dis[x] > dis[y]) swap(x, y);
return res + query(dfn[x], dfn[y], 1, n, 1);
}
修改整个子树/查询子树和:
由于子树的dfs序也连续,直接用每个节点的
应用:
eg1 :P4211 [LNOI2014]LCA
- 拆开修改,
pushup 时注意两段颜色是否相同,若相同整体段数-1
查询:同理,在跳链时也要先单点查询端点颜色
注意修改的
struct bit // dfs序
{
ll sum[N << 2];
void add(ll x, ll y, ll val)
{
for(ll i = dfn[y]; i <= cnt; i += i & (-i)) sum[i] += val;
}
ll query(ll x)
{
ll res = 0;
for(; x > 0; x -= x & (-x)) res += sum[x];
return res;
}
ll ask(ll x)
{
return size[x] + query(mxdfn[x]) - query(dfn[x] - 1);
}
}tree;
void dfs(ll x)
{
size[x] = 1, dep[x] = dep[fa[x]] + 1, in[x] = ++idx;
if(son[x][0]) fa[son[x][0]] = x, dfs(son[x][0]), size[x] += size[son[x][0]];
if(son[x][1]) fa[son[x][1]] = x, dfs(son[x][1]), size[x] += size[son[x][1]];
mson[x] = !(!son[x][1] || size[son[x][0]] >= size[son[x][1]]);
ans += son[x][mson[x]];
out[x] = ++idx;
}
void dfs2(ll x, ll tp)
{
top[x] = tp, dfn[x] = mxdfn[x] = ++cnt, lin[cnt] = x;
if(son[x][mson[x]]) dfs2(son[x][mson[x]], tp);
if(son[x][!mson[x]]) dfs2(son[x][!mson[x]], son[x][!mson[x]]);
mxdfn[x] = max({mxdfn[x], mxdfn[son[x][0]], mxdfn[son[x][1]]});
}
ll jump(ll x, ll step) // 从 x向上跳 step步
{
while(step >= dep[x] - dep[top[x]] + 1) step -= dep[x] - dep[top[x]] + 1, x = fa[top[x]];
return lin[dfn[x] - step];
}
ll wh(ll x) {return x == son[fa[x]][1];}
int main()
{
n = read();
for(int i = 1; i <= n; ++i) son[i][0] = read(), son[i][1] = read();
dfs(1), dfs2(1, 1);
print(ans), putchar('\n');
q = read();
for(int i = 1; i <= q; ++i)
{
nw = read(), root = 1;
tree.add(1, nw, -1), book[nw] = 1;
while(1)
{
ll l = 0, r = dep[nw] - dep[root], siz = tree.ask(root);
while(l < r) // 二分
{
ll mid = (l + r + 1) >> 1;
if(tree.ask(jump(nw, mid)) <= (siz - 1) / 2) l = mid;
else r = mid - 1;
}
ll nx = jump(nw, l), ny = son[fa[nx]][!wh(nx)];
ll sizx = tree.ask(nx), sizy = tree.ask(ny);
if(!book[ny] && (sizx < sizy || book[nx]) && wh(nx) == mson[fa[nx]])
ans += ny - nx, mson[fa[nx]] = wh(ny); // 换了重儿子,更新答案
else if(book[nx] && book[ny])
{
ans -= son[fa[nx]][mson[fa[nx]]];
break;
}
if(nx == root || book[nx]) break; // 如果没有更新或为叶子节点,退出,把贡献减去
root = nx;
}
print(ans), putchar('\n');
}
return 0;
}
/*
当 siz_{nw} \le siz_{root}/2 时,nw才是 root轻儿子
二分找到路径上深度最浅的 nw
nw不一定是root的子节点,但没关系,nw到 root路径上的点 siz已经 >siz_{root}/2,不可能再成为它们父节点的轻儿子
把 root设为 nw,重复
*/
4. 边权转点权
比较平凡,把边权下放到深度更深的子节点正常维护
P4315 月下“毛景树”
比较模板
P1505 [国家集训队] 旅游
难点在于线段树的标记
int lson(int x) {return x << 1;}
int rson(int x) {return x << 1 | 1;}
void pushup(int p)
{
sum[p] = sum[lson(p)] + sum[rson(p)];
mn[p] = min(mn[lson(p)], mn[rson(p)]), mx[p] = max(mx[lson(p)], mx[rson(p)]);
}
void pushdown(int x)
{
if(tag[x] == 1) return;
tag[lson(x)] *= tag[x], tag[rson(x)] *= tag[x];
sum[lson(x)] *= tag[x], sum[rson(x)] *= tag[x];
swap(mn[lson(x)], mx[lson(x)]), mn[lson(x)] *= -1, mx[lson(x)] *= -1;
swap(mn[rson(x)], mx[rson(x)]), mn[rson(x)] *= -1, mx[rson(x)] *= -1;
tag[x] = 1;
}
void build(int l, int r, int p)
{
tag[p] = 1;
if(l == r) {sum[p] = mx[p] = mn[p] = a[lin[l]]; return;}
int mid = (l + r) >> 1;
build(l, mid, lson(p)), build(mid + 1, r, rson(p));
pushup(p);
}
void modify1(int id, int val, int l, int r, int p)
{
if(l == r) {sum[p] = mx[p] = mn[p] = val; return;}
int mid = (l + r) >> 1;
pushdown(p);
if(mid >= id) modify1(id, val, l, mid, lson(p));
else modify1(id, val, mid + 1, r, rson(p));
pushup(p);
}
void modify2(int l, int r, int nl, int nr, int p)
{
if(l <= nl && nr <= r)
{
tag[p] *= -1, sum[p] *= -1;
int lsh = mx[p]; mx[p] = -mn[p], mn[p] = -lsh;
return;
}
int mid = (nl + nr) >> 1;
pushdown(p);
if(mid >= l) modify2(l, r, nl, mid, lson(p));
if(mid < r) modify2(l, r, mid + 1, nr, rson(p));
pushup(p);
}
int querysum(int l, int r, int nl, int nr, int p)
{
if(l <= nl && nr <= r) return sum[p];
int mid = (nl + nr) >> 1, res = 0;
pushdown(p);
if(mid >= l) res += querysum(l, r, nl, mid, lson(p));
if(mid < r) res += querysum(l, r, mid + 1, nr, rson(p));
return res;
}
int querymax(int l, int r, int nl, int nr, int p)
{
if(l <= nl && nr <= r) return mx[p];
int mid = (nl + nr) >> 1, res = -inf;
pushdown(p);
if(mid >= l) res = max(res, querymax(l, r, nl, mid, lson(p)));
if(mid < r) res = max(res, querymax(l, r, mid + 1, nr, rson(p)));
return res;
}
int querymin(int l, int r, int nl, int nr, int p)
{
if(l <= nl && nr <= r) return mn[p];
int mid = (nl + nr) >> 1, res = inf;
pushdown(p);
if(mid >= l) res = min(res, querymin(l, r, nl, mid, lson(p)));
if(mid < r) res = min(res, querymin(l, r, mid + 1, nr, rson(p)));
return res;
}
void update(int x, int y)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
modify2(dfn[top[x]], dfn[x], 1, n, 1);
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(dfn[x] < dfn[y]) modify2(dfn[x] + 1, dfn[y], 1, n, 1);
}
int asksum(int x, int y)
{
int res = 0;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res += querysum(dfn[top[x]], dfn[x], 1, n, 1);
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(dfn[x] < dfn[y]) res += querysum(dfn[x] + 1, dfn[y], 1, n, 1);
return res;
}
int askmin(int x, int y)
{
int res = inf;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res = min(res, querymin(dfn[top[x]], dfn[x], 1, n, 1));
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(dfn[x] < dfn[y]) res = min(res, querymin(dfn[x] + 1, dfn[y], 1, n, 1));
return res;
}
int askmax(int x, int y)
{
int res = -inf;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res = max(res, querymax(dfn[top[x]], dfn[x], 1, n, 1));
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
if(dfn[x] < dfn[y]) res = max(res, querymax(dfn[x] + 1, dfn[y], 1, n, 1));
return res;
}
5. 配合换根
P3979 遥远的国度
换根可以不用 lct 换,对路径加没有影响
分析子树的情况
-
当
x 为root 时,为整棵树 -
当
root 不在x 子树内,就是原来子树 -
当
root 在x 子树内时,为整棵树去掉root 所在的x 的子节点对应的子树,可以将root 往上跳重链,直到遇到x 轻子节点,否则就对应重儿子
void update(int x, int y, int val)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
modify(dfn[top[x]], dfn[x], val, 1, n, 1);
x = fa[top[x]];
}
if(dfn[x] > dfn[y]) swap(x, y);
modify(dfn[x], dfn[y], val, 1, n, 1);
}
int merge(int x, int val)
{
int y = root;
while(top[x] != top[y])
{
if(fa[top[y]] == x) return top[y];
y = fa[top[y]];
}
return mson[x];
}
int ask(int x)
{
if(x == root) return query(1, n, 1, n, 1);
if(dfn[x] < dfn[root] && dfn[root] <= dfn[x] + siz[x] - 1)
{
int y = merge(x, dfn[root]), mn = inf;
if(dfn[y] > 1) mn = min(mn, query(1, dfn[y] - 1, 1, n, 1));
if(dfn[y] + siz[y] <= n) mn = min(mn, query(dfn[y] + siz[y], n, 1, n, 1));
return mn;
}
return query(dfn[x], dfn[x] + siz[x] - 1, 1, n, 1);
}