重链剖分的总结与模板
概述:
我们通常说的树链剖分指的是重链剖分。此外还有长链剖分,实链剖分。在学LCT时感觉需要对重剖来个总结。于是有了这一篇。一句话的概括。重链剖分是一种对树上结点进行编号。然后把链哈希成区间的一种方式。然后就可以把树上信息变成若干区间信息,通过线段树等数据结构进行高效的维护。如果你想学习树剖,网络上有很多资源。然后完成下面例题的点权和边权的维护,重链剖分就算入门了。
点权:
推荐例题HDU 3966 & FJUT OJ 2710 Aragorn's Story
///树剖 点权
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50010;
typedef long long LL;
struct Edge {
int to, w, next;
} edge[MAXN * 2];
int first[MAXN], sign, tot;
int dep[MAXN], siz[MAXN], faz[MAXN], id[MAXN], son[MAXN], top[MAXN], tid[MAXN];
int n, m, q;
long long a[MAXN];
struct SegmentTree {
struct Node {
int l, r;
LL sum, Lazy;
} tree[MAXN * 4];
inline void push_up(int rt) {
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
}
inline void push_down(int rt) {
if(tree[rt].Lazy) {
tree[rt << 1].Lazy += tree[rt].Lazy;
tree[rt << 1 | 1].Lazy += tree[rt].Lazy;
tree[rt << 1].sum += (tree[rt << 1].r - tree[rt << 1].l + 1) * tree[rt].Lazy;
tree[rt << 1 | 1].sum += (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1) * tree[rt].Lazy;
tree[rt].Lazy = 0;
}
}
void build(int rt, int l, int r) {
tree[rt].l = l;
tree[rt].r = r;
tree[rt].sum = 0;
tree[rt].Lazy = 0;
if(l == r) {
tree[rt].sum = a[ tid[l] ];
return ;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
push_up(rt);
}
void update(int rt, int l, int r, LL val) {
if(l <= tree[rt].l && tree[rt].r <= r) {
tree[rt].sum += (tree[rt].r - tree[rt].l + 1) * val;
tree[rt].Lazy += val;
return ;
}
push_down(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if(r <= mid) {
update(rt << 1, l, r, val);
} else if(l > mid) {
update(rt << 1 | 1, l, r, val);
} else {
update(rt << 1, l, mid, val);
update(rt << 1 | 1, mid + 1, r, val);
}
push_up(rt);
}
LL query(int rt, int l, int r) {
if(l <= tree[rt].l && tree[rt].r <= r) {
return tree[rt].sum;
}
push_down(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if(r <= mid) {
return query(rt << 1, l, r);
} else if(l > mid) {
return query(rt << 1 | 1, l, r);
} else {
return query(rt << 1, l, mid) + query(rt << 1 | 1, mid + 1, r);
}
}
} Seg;
void init() {
sign = tot = 0;
memset(first, -1, sizeof(first));
memset(dep, 0, sizeof(dep));
memset(siz, 0, sizeof(siz));
memset(faz, 0, sizeof(faz));
memset(id, 0, sizeof(id));
memset(son, 0, sizeof(son));
memset(top, 0, sizeof(top));
}
void add_edge(int u, int v, int w) {
edge[sign].to = v;
edge[sign].w = w;
edge[sign].next = first[u];
first[u] = sign++;
}
void dfs1(int now, int father, int depth) {
siz[now] = 1;
faz[now] = father;
dep[now] = depth;
son[now] = 0;
for(int i = first[now]; ~i; i = edge[i].next) {
int to = edge[i].to;
if(to != father) {
dfs1(to, now, depth + 1);
siz[now] += siz[to];
if(son[now] == 0 || siz[ son[now] ] < siz[to]) {
son[now] = to;
}
}
}
}
void dfs2(int now, int topf) {
top[now] = topf;
id[now] = ++tot;
tid[id[now]] = now;
if(son[now]) {
dfs2(son[now], topf);
}
for(int i = first[now]; ~i; i = edge[i].next) {
int to = edge[i].to;
if(to == faz[now] || to == son[now]) {
continue;
}
dfs2(to, to);
}
}
void cutting(int u, int v, int val) {
int fu = top[u], fv = top[v];
while(fu != fv) {
if(dep[fu] < dep[fv]) {
swap(fu, fv);
swap(u, v);
}
Seg.update(1, id[fu], id[u], val);
u = faz[fu];
fu = top[u];
}
if(dep[u] > dep[v]) {
swap(u,v);
}
Seg.update(1, id[u], id[v], val);
}
long long query(int u, int v) {
long long sum = 0;
int fu = top[u], fv = top[v];
while(fu != fv) {
if(dep[fu] < dep[fv]) {
swap(fu, fv);
swap(u, v);
}
sum = sum + Seg.query(1, id[fu], id[u]);
u = faz[fu];
fu = top[u];
}
if(dep[u] > dep[v]) {
swap(u, v);
}
return sum = sum + Seg.query(1, id[u], id[v]);
}
int main() {
while(~scanf("%d %d %d", &n, &m, &q)) {
init();
for(int i = 1; i <= n; i++ ) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= m; i++ ) {
int u, v;
scanf("%d %d", &u, &v);
add_edge(u, v, 1);
add_edge(v, u, 1);
}
tot = 0;
dfs1(1, 0, 0);
dfs2(1, 1);
Seg.build(1, 1, n);
char opt[5];
int x, y,z;
for(int i = 1; i <= q; i++ ) {
scanf("%s", opt);
if(opt[0] == 'I') {
scanf("%d %d %d", &x, &y, &z);
cutting(x, y, z);
continue;
}
if(opt[0] == 'D') {
scanf("%d %d %d", &x, &y, &z);
cutting(x, y, -z);
continue;
}
if(opt[0] == 'Q') {
scanf("%d", &x);
printf("%I64d
", query(x, x));
continue;
}
}
}
return 0;
}
边权
推荐例题POJ2763 & FJUT OJ 2796 Housewife Wind
维护边权,只要把边权存到深度更深的结点即可。
这么长代码还好没写出bug。不然真不好办。。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 7;
const int INF = 0x7FFFFFF;
int dep[maxn], siz[maxn], faz[maxn], id[maxn], son[maxn], val[maxn], top[maxn];
struct Edge {
int to, w, next;
} edge[maxn * 2];
int first[maxn], sign, n, m, s, tot;
struct Node {
int from, to, cost;
} input[maxn];
struct TreeNode {
int l, r, mx, mi, lazy;
} tree[maxn << 2];
inline void init() {
memset(first, -1, sizeof(first));
sign = 0;
}
inline void add_edge(int u, int v, int w) {
edge[sign].to = v;
edge[sign].w = w;
edge[sign].next = first[u];
first[u] = sign ++;
}
void dfs1(int now, int father, int depth) {
siz[now] = 1;
faz[now] = father;
dep[now] = depth;
son[now] = 0;
for(int i = first[now]; ~i; i = edge[i].next) {
int to = edge[i].to;
if(to != father) {
dfs1(to, now, depth + 1);
siz[now] += siz[to];
if(siz[ son[now] ] < siz[to]) {
son[now] = to;
}
}
}
}
void dfs2(int now, int topf) {
top[now] = topf;
id[now] = ++tot;
if(son[now]) {
dfs2(son[now], topf);
}
for(int i = first[now]; ~i; i = edge[i].next) {
int to = edge[i].to;
if(to == faz[now] || to == son[now]) {
continue;
}
dfs2(to, to);
}
}
void push_up(int rt) {
tree[rt].mx = max(tree[rt << 1].mx, tree[rt << 1 | 1].mx);
tree[rt].mi = min(tree[rt << 1].mi, tree[rt << 1 | 1].mi);
}
void push_down(int rt) {
if(tree[rt].lazy) {
tree[rt].lazy ^= 1;
tree[rt << 1].lazy ^= 1;
tree[rt << 1 | 1].lazy ^= 1;
swap(tree[rt << 1].mx, tree[rt << 1].mi);
tree[rt << 1].mx *= -1;
tree[rt << 1].mi *= -1;
swap(tree[rt << 1 | 1].mx, tree[rt << 1 | 1].mi);
tree[rt << 1 | 1].mx *= -1;
tree[rt << 1 | 1].mi *= -1;
}
}
void build(int rt, int l, int r) {
tree[rt].l = l, tree[rt].r = r;
tree[rt].lazy = 0;
if(l == r) {
tree[rt].mx = tree[rt].mi = val[l];
return ;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
push_up(rt);
}
void update(int rt, int l, int r) { ///区间取反
if(l <= tree[rt].l && tree[rt].r <= r) {
tree[rt].lazy ^= 1;
swap(tree[rt].mx, tree[rt].mi);
tree[rt].mx *= -1;
tree[rt].mi *= -1;
return ;
}
push_down(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if(r <= mid) {
update(rt << 1, l, r);
} else if(l > mid) {
update(rt << 1 | 1, l, r);
} else {
update(rt << 1, l, mid);
update(rt << 1 | 1, mid + 1, r);
}
push_up(rt);
}
void updatePos(int rt, int pos, int val) { ///单点修改
if(tree[rt].l == tree[rt].r) {
tree[rt].mx = tree[rt].mi = val;
return ;
}
push_down(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if(pos <= mid) {
updatePos(rt << 1, pos, val);
} else {
updatePos(rt << 1 | 1, pos, val);
}
push_up(rt);
}
int query(int rt, int l, int r) {
if(l <= tree[rt].l && tree[rt].r <= r) {
return tree[rt].mx;
}
push_down(rt);
int mid = (tree[rt].l + tree[rt].r) >> 1;
if(r <= mid) {
return query(rt << 1, l, r);
} else if(l > mid) {
return query(rt << 1 | 1, l, r);
} else {
return max(query(rt << 1, l, mid), query(rt << 1 | 1, mid + 1, r));
}
}
void treeUpdate(int x, int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) {
swap(x,y);
}
update(1, id[top[x]], id[x]);
x = faz[top[x]];
}
if(dep[x] > dep[y]) {
swap(x,y);
}
if(x != y) {
update(1, id[son[x]], id[y]);
}
}
int treeQuery(int x, int y) {
int ans = -INF;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) {
swap(x,y);
}
ans = max(ans, query(1, id[top[x]], id[x]));
x = faz[top[x]];
}
if(dep[x] > dep[y]) {
swap(x,y);
}
if(x != y) {
ans = max(ans, query(1, id[son[x]], id[y]));
}
return ans;
}
int cutting(int x, int y) {
int sum = 0;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) {
swap(x,y);
}
sum += query(1, id[top[x]], id[x]);
x = faz[top[x]];
}
if(dep[x] > dep[y]) {
swap(x,y);
}
if(x != y) {
sum += query(1, id[son[x]], id[y]);
}
return sum;
}
int main() {
int T, x, y;
char opt[10];
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
init();
for(int i = 1; i <= n - 1; i++ ) {
scanf("%d %d %d", &input[i].from, &input[i].to, &input[i].cost);
add_edge(input[i].from, input[i].to, input[i].cost);
add_edge(input[i].to, input[i].from, input[i].cost);
}
tot = 0;
dfs1(1, 0, 1);
dfs2(1, 1);
for(int i = 1; i <= n - 1; i++ ) {
if(dep[ input[i].from ] < dep[ input[i].to ]) {
swap(input[i].from, input[i].to);
}
val[ id[ input[i].from ] ] = input[i].cost;
}
build(1, 1, n);
while(~scanf("%s", opt) && strcmp(opt, "DONE")) {
if(opt[0] == 'C') {
scanf("%d %d", &x, &y);
if(dep[ input[x].from ] < dep[ input[x].to ]) {
swap(input[x].from, input[x].to);
}
updatePos(1, id[ input[x].from ], y);
}
if(opt[0] == 'N') {
scanf("%d %d", &x, &y);
treeUpdate(x, y);
}
if(opt[0] == 'Q') {
scanf("%d %d", &x, &y);
printf("%d
", treeQuery(x, y));
}
}
}
return 0;
}