啃了好久,终于把树链剖分给啃下来了
前置知识:dfs序,lca,线段树等
算法思想:把树拆成一条一条的链,然后用数据结构维护。
首先定义几个概念:
具体实现:以luogu3384 【模板】树链剖分 为例
这个dfs要处理这些事情:
void dfs1(int u, int f, int d){
dep[u] = d, fa[u] = f, size[u] = 1, son[u] = 0;
for(int i = fir[u]; i != -1; i = nxt[i]){
int v = to[i];
if(v == f) continue;
dfs1(v, u, d + 1);
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
}
这个dfs要做这些事情:
void dfs2(int u, int topf){
dfn[u] = ++num, id[num] = u, top[u] = topf;
if(!son[u]) return; dfs2(son[u], topf);
for(int i = fir[u]; i != -1; i = nxt[i]){
int v = to[i];
if(v != son[u] && v != fa[u]) dfs2(v, v);
}
}
轻重链划分后,就可以证明复杂度了(以下省略1w字)
也可以看这里
维护的数据结构我们用线段树来解决。
你可以想象,轻链和重链都按照dfs一条一条地躺在了一条线上,这时突然有个线段树跳出来说:我来维护他们! 太逗了
事实上dfs序有许多优秀的性质。比如,一颗子树里的dfs序总是连续的,这意味着维护子树就可以转化为维护区间;同理,维护重链也可以转换成维护区间了。that’s good
int val[MAXN << 2], tag[MAXN << 2];
#define mid ((l + r) >> 1)
inline void Pushup(int u){
val[u] = (val[u << 1] + val[u << 1 | 1]) % p;
}
inline void Pushdown(int u, int l, int r){
if(tag[u]){
val[u << 1] = (val[u << 1] + (l - mid + 1) * tag[u]) % p;
val[u << 1 | 1] = (val[u << 1 | 1] + (r - mid) * tag[u]) % p;
tag[u] = 0;
}
}
inline void Build(int u, int l, int r){
if(l == r){val[u] = w[id[l]] % p; return;}
Build(u << 1, l, mid); Build(u << 1 | 1, mid + 1, r);
Pushup(u);
}
inline void Modify(int u, int l, int r, int L, int R, int k){
if(l >= L && r <= R){val[u] = (val[u] + (r - l + 1) * k) % p, tag[u] = (tag[u] + k) % p; return;}
Pushdown(u, l, r);
if(L <= mid) Modify(u << 1, l, mid, L, R, k);
if(R > mid) Modify(u << 1 | 1, mid + 1, r, L, R, k);
Pushup(u);
}
inline int Query(int u, int l, int r, int L, int R){
if(l >= L && r <= R) return val[u];
Pushdown(u, l, r); int sum = 0;
if(L <= mid) sum = (sum + Query(u << 1, l, mid, L, R)) % p;
if(R > mid) sum = (sum + Query(u << 1 | 1, mid + 1, r, L, R)) % p;
return sum;
}
#undef mid
要注意的一点是建树时取的初值是 w [ i d [ u ] ] w[id[u]] w[id[u]]。
类似于求lca的思想,两个点向上跳,每跳一条链就对这条链进行修改,最后跳到同一条链的时候修改两点之间的部分。
inline void Path_Add(int x, int y, int k){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x, y); //x向上跳
Modify(1, 1, n, dfn[top[x]], dfn[x], k);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
Modify(1, 1, n, dfn[x], dfn[y], k);
}
与修改路径差不多,不同的是把修改换成了统计答案。
inline int Path_Query(int x, int y){
int sum = 0;
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x, y);
sum = (sum + Query(1, 1, n, dfn[top[x]], dfn[x])) % p;
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
sum = (sum + Query(1, 1, n, dfn[x], dfn[y])) % p;
return sum;
}
前面我们说过一颗子树里的dfs序总是连续的,具体就是从 d f n [ i ] dfn[i] dfn[i]到 d f n [ i ] + s i z e [ i ] − 1 dfn[i]+size[i]-1 dfn[i]+size[i]−1的区间。
inline void Subtree_Add(int x, int k){
Modify(1, 1, n, dfn[x], dfn[x] + size[x] - 1, k);
}
同修改子树。
inline int Subtree_Query(int x){
return Query(1, 1, n, dfn[x], dfn[x] + size[x] - 1);
}
全部代码:
#include
#include
#include
using namespace std;
const int MAXN = 100001;
const int MAXM = 100001;
int n, m, r, p;
int w[MAXN];
int fir[MAXN], nxt[MAXM << 1], to[MAXM << 1], len[MAXM << 1], cnt;
int size[MAXN], son[MAXN], fa[MAXN], dep[MAXN];
int dfn[MAXN], top[MAXN], id[MAXN], num;
int val[MAXN << 2], tag[MAXN << 2];
#define mid ((l + r) >> 1)
inline void Pushup(int u){
val[u] = (val[u << 1] + val[u << 1 | 1]) % p;
}
inline void Pushdown(int u, int l, int r){
if(tag[u]){
val[u << 1] = (val[u << 1] + (l - mid + 1) * tag[u]) % p;
val[u << 1 | 1] = (val[u << 1 | 1] + (r - mid) * tag[u]) % p;
tag[u] = 0;
}
}
inline void Build(int u, int l, int r){
if(l == r){val[u] = w[id[l]] % p; return;}
Build(u << 1, l, mid); Build(u << 1 | 1, mid + 1, r);
Pushup(u);
}
inline void Modify(int u, int l, int r, int L, int R, int k){
if(l >= L && r <= R){val[u] = (val[u] + (r - l + 1) * k) % p, tag[u] = (tag[u] + k) % p; return;}
Pushdown(u, l, r);
if(L <= mid) Modify(u << 1, l, mid, L, R, k);
if(R > mid) Modify(u << 1 | 1, mid + 1, r, L, R, k);
Pushup(u);
}
inline int Query(int u, int l, int r, int L, int R){
if(l >= L && r <= R) return val[u];
Pushdown(u, l, r); int sum = 0;
if(L <= mid) sum = (sum + Query(u << 1, l, mid, L, R)) % p;
if(R > mid) sum = (sum + Query(u << 1 | 1, mid + 1, r, L, R)) % p;
return sum;
}
#undef mid
void dfs1(int u, int f, int d){
dep[u] = d, fa[u] = f, size[u] = 1, son[u] = 0;
for(int i = fir[u]; i != -1; i = nxt[i]){
int v = to[i];
if(v == f) continue;
dfs1(v, u, d + 1);
size[u] += size[v];
if(size[v] > size[son[u]]) son[u] = v;
}
}
void dfs2(int u, int topf){
dfn[u] = ++num, id[num] = u, top[u] = topf;
if(!son[u]) return; dfs2(son[u], topf);
for(int i = fir[u]; i != -1; i = nxt[i]){
int v = to[i];
if(v != son[u] && v != fa[u]) dfs2(v, v);
}
}
inline void Path_Add(int x, int y, int k){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x, y); //x向上跳
Modify(1, 1, n, dfn[top[x]], dfn[x], k);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
Modify(1, 1, n, dfn[x], dfn[y], k);
}
inline int Path_Query(int x, int y){
int sum = 0;
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x, y);
sum = (sum + Query(1, 1, n, dfn[top[x]], dfn[x])) % p;
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
sum = (sum + Query(1, 1, n, dfn[x], dfn[y])) % p;
return sum;
}
inline void Subtree_Add(int x, int k){
Modify(1, 1, n, dfn[x], dfn[x] + size[x] - 1, k);
}
inline int Subtree_Query(int x){
return Query(1, 1, n, dfn[x], dfn[x] + size[x] - 1);
}
inline int read(){
int k = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){k = k*10 + ch - '0'; ch = getchar();}
return k * f;
}
inline void add_edge(int a, int b){
to[cnt] = b;
nxt[cnt] = fir[a];
fir[a] = cnt++;
}
int main(){
freopen("in.txt", "r", stdin);
memset(fir, -1, sizeof(fir));
n = read(), m = read(), r = read(), p = read();
for(int i = 1; i <= n; i++){
w[i] = read();
}
for(int i = 1; i < n; i++){
int a = read(), b = read();
add_edge(a, b);
add_edge(b, a);
}
dfs1(r, 0, 0), dfs2(r, r);
Build(1, 1, n);
for(int i = 1; i <= m; i++){
int opt = read(), x, y, z;
switch(opt){
case 1:
x = read(), y = read(), z = read();
Path_Add(x, y, z % p);
break;
case 2:
x = read(), y = read();
printf("%d\n", Path_Query(x, y));
break;
case 3:
x = read(), z = read();
Subtree_Add(x, z % p);
break;
case 4:
x = read();
printf("%d\n", Subtree_Query(x));
break;
}
}
return 0;
}
tips:写完之后发现还多简单的