树链剖分
思想
把一棵树转化为一个序列,树中任意一条路径可以被分为O(logn)个连续区间
然后选择维护区间信息的数据结构
定义
-
重(轻)儿子:当前节点的子节点中子树最大的子节点,其余是轻儿子
-
重(轻)边:重儿子向父节点的边是重边,其他是轻边
-
重链:连续重边的极大集合
dfs序:优先遍历重儿子,可以保证重链上的序号是连续的
定理:树中任意一条路径可以被分为O(logn)个连续区间
如何求这个呢?
每次让深度较深的点通过重链向上爬,直到爬到相同重链上
例题
[ACW 2568][https://www.acwing.com/problem/content/description/2570/]
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 100010, M = N * 2;
int n,m;
int w[N], h[N], e[M], ne[M], idx;
int id[N], nw[N], cnt;
int dep[N], sz[N], top[N], fa[N], son[N];
struct tree
{
int l,r;
ll add, sum;
}tr[N * 4];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a] , h[a] = idx ++;
}
//预处理深度,重儿子,父子关系,大小
void dfs1(int u, int father, int depth){
dep[u] = depth ,fa[u] = father ,sz[u] = 1;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if(j == father) continue;
dfs1(j,u, depth + 1);
sz[u] += sz[j];
if(sz[son[u]] < sz[j]) son[u] = j;
}
}
//求解dfs序
void dfs2(int u,int t)
{
id[u] = ++ cnt, nw[cnt] = w[u], top[u] = t;
if(!son[u]) return;
dfs2(son[u],t);
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if(j == fa[u] || j == son[u]) continue;
dfs2(j,j);
}
}
//线段树部分
void pushup(int u){
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}
void pushdown(int u){
auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if(root.add)
{
left.add += root.add, left.sum += (left.r - left.l + 1) * root.add;
right.add += root.add,right.sum += (right.r - right.l + 1) * root.add;
root.add = 0;
}
}
void build(int u, int l, int r)
{
if(l == r) tr[u] = {l,r,0,nw[r]};
else
{
tr[u] = {l,r};
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void update(int u, int l, int r ,int k)
{
if(l <= tr[u].l && tr[u].r <= r)
{
tr[u].add += k;
tr[u].sum += k*(tr[u].r -tr[u].l + 1);
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) update(u << 1, l, r, k);
if(r > mid) update(u << 1| 1, l,r,k);
pushup(u);
}
ll query(int u, int l, int r)
{
if(l <= tr[u].l && tr[u].r <= r) return tr[u].sum;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
ll res = 0;
if(l <= mid) res += query(u << 1, l ,r);
if(r > mid) res += query(u << 1 | 1 , l, r);
return res;
}
//修改路径上的点
void update_path(int u, int v ,int k)
{
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]]) swap(u,v);
update(1,id[top[u]], id[u], k);
u = fa[top[u]];
}
if(dep[u] < dep[v]) swap(u,v);
update(1,id[v], id[u],k);
}
//询问路径和
ll query_path(int u, int v)
{
ll res = 0;
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]]) swap(u,v);
res += query(1,id[top[u]], id[u]);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u,v);
res += query(1, id[v], id[u]);
return res;
}
//修改子树
void update_tree(int u, int k)
{
update(1,id[u], id[u] + sz[u] - 1, k);
}
//询问子树和
ll query_tree(int u)
{
return query(1,id[u], id[u] + sz[u] - 1);
}
int main()
{
scanf("%d",&n);
for (int i = 1; i <= n ; i ++) scanf("%d", &w[i]);
memset(h,-1,sizeof h);
for (int i = 0; i < n - 1; i ++)
{
int a,b;
scanf("%d%d", &a, &b);
add(a,b), add(b,a);
}
dfs1(1, -1, 1);
dfs2(1, 1);
build(1, 1, n);
scanf("%d", &m);
while(m --)
{
int t,u,v,k;
scanf("%d%d", &t, &u);
if(t == 1)
{
scanf("%d%d", &v, &k);
update_path(u,v,k);
}
else if (t == 2)
{
scanf("%d", &k);
update_tree(u,k);
}
else if (t == 3)
{
scanf("%d", &v);
printf("%lld\n", query_path(u,v));
}
else printf("%lld\n", query_tree(u));
}
return 0;
}