题解:P3384 【模板】重链剖分/长链剖分
qiuby123456 · · 题解
树链剖分,把树剖开分成链。
我们有一棵有根树,从根开始 dfs,记录以下信息:
- 每个节点被搜索的顺序,存在
dfn 数组中; - 每个节点的父亲(根节点父亲为
0 ),存在fa 数组中; - 每个节点的子树大小(包含自己),存在
size 数组中。
我们容易发现,对于树上以节点
那如果我们不先搜索上面提到的
如果选择一个子节点
其实这两种划分方法大同小异,这里主要以重链剖分为主,长链剖分只放完整代码不放片段代码。
我们先求:
片段代码:
void dfs1(int root){
int j = 0, i = depth[root] + 1;//临时变量
size[root] = 1;
for (int ch : to[root]){
if (!depth[ch]){
depth[ch] = i;
fa[ch] = root;
dfs1(ch);
size[root] += size[ch];
if (size[j] < size[ch]){
j = ch;
}
}
}
heavy[root] = j;
}
解释 std::vector<int> 类型的数组,
然后按照先搜重儿子的方法,求:
以及将每个点的初始值按照
片段代码:
//dfnnum 初始值为 0。
void dfs2(int root, int an/*ancestor*/){
dfn[root] = ++dfnnum;
b[dfnnum] = a[root];//排序,将 root 的值复制到 dfn[root] 上。
top[root] = an;
if (heavy[root]){
int i = fa[root], j = heavy[root];
dfs2(j, an);
for (int ch : to[root]){
if ((ch ^ i) && (ch ^ j)){//异或代替不等于号卡常
dfs2(ch, ch);
}
}
}
}
这样,重链剖分的初始化就做好了。
题目要求我们实现以下操作:
1 x y z,表示将树从x 到y 结点最短路径上所有节点的值都加上z 。2 x y,表示求树从x 到y 结点最短路径上所有节点的值之和。3 x z,表示将以x 为根节点的子树内所有节点值都加上z 。4 x表示求以x 为根节点的子树内所有节点值之和。
操作 3、4 比较好操作,我们先处理操作 3、4。
操作 3:
scanf("%d%d", &o, &x);//o 是操作编号
scanf("%lld", &z);
add(dfn[x], dfn[x] + size[x], z % p);
操作 4:
scanf("%d%d", &o, &x);
printf("%d\n", query(dfn[x], dfn[x] + size[x]));
我的增加和查询是左闭右开区间。
对于操作 1,我们可以这样操作(假设
- 从节点
x 到节点top_x 经过的所有节点(包含x 和top_x )的点权都加上z (因为重链的dfn 是连续的,所以修改和查询比较方便,本步修改的是排序后的节点值); -
片段代码:
while (top[x] ^ top[y]){
if (depth[top[x]] < depth[top[y]]){
x ^= y ^= x ^= y;
}
add(dfn[top[x]], dfn[x] + 1, z);
x = fa[top[x]];
}
if (depth[x] < depth[y]){
x ^= y ^= x ^= y;
}
add(dfn[y], dfn[x] + 1, z);
操作 2 同理。
片段代码:
long long s = 0ll;
while (top[x] ^ top[y]){
if (depth[top[x]] < depth[top[y]]){
x ^= y ^= x ^= y;
}
s = (s + c(dfn[top[x]], dfn[x] + 1)) % p;
x = fa[top[x]];
}
if (depth[x] < depth[y]){
x ^= y ^= x ^= y;
}
s = (s + c(dfn[y], dfn[x] + 1)) % p;
这里我使用了线段树来维护区间和。
综上所述,重链剖分完整代码:
#include <cstdio>
#include <vector>
long long p;
template<typename T, int len>
class segment{
public:
class iterator{
public:
T sum;
T addlazy;
T setlazy;
iterator(): sum(0), addlazy(0), setlazy(0){}
iterator &operator=(const iterator &other){
sum = other.sum;
addlazy = other.addlazy;
setlazy = other.setlazy;
}
};
protected:
iterator a[len << 2];
void _build(int l, int r, int i, const T *other){
if (l + 1 == r){
a[i].sum = other[l];
return;
}
int m = l + r >> 1, li = i << 1, ri = i << 1 | 1;
_build(l, m, li, other);
_build(m, r, ri, other);
a[i].sum = (a[li].sum + a[ri].sum) % p;
}
void _add(int l, int r, int i, int lx, int rx, T v){
if (r <= lx || l >= rx){
return;
}
if (l >= lx && r <= rx){
a[i].addlazy += v;
a[i].sum += (r - l) * v;
a[i].sum %= p;
return;
}
int m = l + r >> 1, li = i << 1, ri = i << 1 | 1;
if (a[i].addlazy){
a[li].addlazy += a[i].addlazy;
a[li].sum += (m - l) * a[i].addlazy;
a[ri].addlazy += a[i].addlazy;
a[ri].sum += (r - m) * a[i].addlazy;
a[li].sum %= p;
a[ri].sum %= p;
a[i].addlazy = 0;
}
_add(l, m, li, lx, rx, v);
_add(m, r, ri, lx, rx, v);
a[i].sum = a[li].sum + a[ri].sum;
}
T _query(int l, int r, int i, int lx, int rx){
if (r <= lx || l >= rx){
return 0;
}
if (l >= lx && r <= rx){
return a[i].sum;
}
int m = l + r >> 1, li = i << 1, ri = i << 1 | 1;
if (a[i].addlazy){
a[li].addlazy += a[i].addlazy;
a[li].sum += (m - l) * a[i].addlazy;
a[ri].addlazy += a[i].addlazy;
a[ri].sum += (r - m) * a[i].addlazy;
a[li].sum %= p;
a[ri].sum %= p;
a[i].addlazy = 0;
}
return (_query(l, m, li, lx, rx) + _query(m, r, ri, lx, rx)) % p;
}
public:
segment(){}
segment(const segment<T, len> &other){
for (int i = 0; i < len; ++i){
a[i] = other[i];
}
}
segment(const T *other){
_build(1, len, 1, other);
}
void build(const T *other){
_build(1, len, 1, other);
}
void add(int l, int r, T v){
_add(1, len, 1, l, r, v);
}
T operator()(int l, int r){
return _query(1, len, 1, l, r);
}
};
segment<long long, 500001> c;
std::vector<int> to[500001];
long long a[500001], b[500001], z;
int fa[500001], top[500001], heavy[500001], size[500001], dfn[500001], depth[500001], n, m, r, o, x, y, dfnnum;
void dfs1(int root){
int j = 0, i = depth[root] + 1;
size[root] = 1;
for (int ch : to[root]){
if (!depth[ch]){
depth[ch] = i;
fa[ch] = root;
dfs1(ch);
size[root] += size[ch];
if (size[j] < size[ch]){
j = ch;
}
}
}
heavy[root] = j;
}
void dfs2(int root, int an){
dfn[root] = ++dfnnum;
b[dfnnum] = a[root];
top[root] = an;
if (heavy[root]){
int i = fa[root], j = heavy[root];
dfs2(j, an);
for (int ch : to[root]){
if ((ch ^ i) && (ch ^ j)){
dfs2(ch, ch);
}
}
}
}
void add(int x, int y, long long z){
while (top[x] ^ top[y]){
if (depth[top[x]] < depth[top[y]]){
x ^= y ^= x ^= y;
}
c.add(dfn[top[x]], dfn[x] + 1, z);
x = fa[top[x]];
}
if (depth[x] < depth[y]){
x ^= y ^= x ^= y;
}
c.add(dfn[y], dfn[x] + 1, z);
}
long long query(int x, int y){
long long s = 0ll;
while (top[x] ^ top[y]){
if (depth[top[x]] < depth[top[y]]){
x ^= y ^= x ^= y;
}
s = (s + c(dfn[top[x]], dfn[x] + 1)) % p;
x = fa[top[x]];
}
if (depth[x] < depth[y]){
x ^= y ^= x ^= y;
}
return (s + c(dfn[y], dfn[x] + 1)) % p;
}
int main(){
scanf("%d%d%d", &n, &m, &r);
scanf("%lld", &p);
for (int i = 1; i <= n; ++i){
scanf("%lld", a + i);
}
for (int i = 1; i < n; ++i){
scanf("%d%d", &x, &y);
to[x].emplace_back(y);
to[y].emplace_back(x);
}
depth[r] = 1;
dfs1(r);
dfs2(r, r);
c.build(b);
while (m--){
scanf("%d%d", &o, &x);
switch(o){
case 1:
scanf("%d", &y);
scanf("%lld", &z);
add(x, y, z % p);
break;
case 2:
scanf("%d", &y);
printf("%d\n", query(x, y));
break;
case 3:
scanf("%lld", &z);
c.add(dfn[x], dfn[x] + size[x], z % p);
break;
case 4:
printf("%d\n", c(dfn[x], dfn[x] + size[x]));
break;
}
}
return 0;
}
长链剖分完整代码:
#include <cstdio>
#include <vector>
long long p;
template<typename T, int len>
class segment{
public:
class iterator{
public:
T sum;
T addlazy;
T setlazy;
iterator(): sum(0), addlazy(0), setlazy(0){}
iterator &operator=(const iterator &other){
sum = other.sum;
addlazy = other.addlazy;
setlazy = other.setlazy;
}
};
protected:
iterator a[len << 2];
void _build(int l, int r, int i, const T *other){
if (l + 1 == r){
a[i].sum = other[l];
return;
}
int m = l + r >> 1, li = i << 1, ri = i << 1 | 1;
_build(l, m, li, other);
_build(m, r, ri, other);
a[i].sum = (a[li].sum + a[ri].sum) % p;
}
void _add(int l, int r, int i, int lx, int rx, T v){
if (r <= lx || l >= rx){
return;
}
if (l >= lx && r <= rx){
a[i].addlazy += v;
a[i].sum += (r - l) * v;
a[i].sum %= p;
return;
}
int m = l + r >> 1, li = i << 1, ri = i << 1 | 1;
if (a[i].addlazy){
a[li].addlazy += a[i].addlazy;
a[li].sum += (m - l) * a[i].addlazy;
a[ri].addlazy += a[i].addlazy;
a[ri].sum += (r - m) * a[i].addlazy;
a[li].sum %= p;
a[ri].sum %= p;
a[i].addlazy = 0;
}
_add(l, m, li, lx, rx, v);
_add(m, r, ri, lx, rx, v);
a[i].sum = a[li].sum + a[ri].sum;
}
T _query(int l, int r, int i, int lx, int rx){
if (r <= lx || l >= rx){
return 0;
}
if (l >= lx && r <= rx){
return a[i].sum;
}
int m = l + r >> 1, li = i << 1, ri = i << 1 | 1;
if (a[i].addlazy){
a[li].addlazy += a[i].addlazy;
a[li].sum += (m - l) * a[i].addlazy;
a[ri].addlazy += a[i].addlazy;
a[ri].sum += (r - m) * a[i].addlazy;
a[li].sum %= p;
a[ri].sum %= p;
a[i].addlazy = 0;
}
return (_query(l, m, li, lx, rx) + _query(m, r, ri, lx, rx)) % p;
}
public:
segment(){}
segment(const segment<T, len> &other){
for (int i = 0; i < len; ++i){
a[i] = other[i];
}
}
segment(const T *other){
_build(1, len, 1, other);
}
void build(const T *other){
_build(1, len, 1, other);
}
void add(int l, int r, T v){
_add(1, len, 1, l, r, v);
}
T operator()(int l, int r){
return _query(1, len, 1, l, r);
}
};
segment<long long, 500001> c;
std::vector<int> to[500001];
long long a[500001], b[500001], z;
int fa[500001], top[500001], deep[500001], length[500001], size[500001], dfn[500001], depth[500001], n, m, r, o, x, y, dfnnum;
void dfs1(int root){
int k = 0, j = 0, i = depth[root] + 1;
size[root] = 1;
for (int ch : to[root]){
if (!depth[ch]){
depth[ch] = i;
fa[ch] = root;
dfs1(ch);
size[root] += size[ch];
if (k < length[ch]){
k = length[j = ch];
}
}
}
length[root] = k + 1;
deep[root] = j;
}
void dfs2(int root, int an){
dfn[root] = ++dfnnum;
b[dfnnum] = a[root];
top[root] = an;
if (deep[root]){
int i = fa[root], j = deep[root];
dfs2(j, an);
for (int ch : to[root]){
if ((ch ^ i) && (ch ^ j)){
dfs2(ch, ch);
}
}
}
}
void add(int x, int y, long long z){
while (top[x] ^ top[y]){
if (depth[top[x]] < depth[top[y]]){
x ^= y ^= x ^= y;
}
c.add(dfn[top[x]], dfn[x] + 1, z);
x = fa[top[x]];
}
if (depth[x] < depth[y]){
x ^= y ^= x ^= y;
}
c.add(dfn[y], dfn[x] + 1, z);
}
long long query(int x, int y){
long long s = 0ll;
while (top[x] ^ top[y]){
if (depth[top[x]] < depth[top[y]]){
x ^= y ^= x ^= y;
}
s = (s + c(dfn[top[x]], dfn[x] + 1)) % p;
x = fa[top[x]];
}
if (depth[x] < depth[y]){
x ^= y ^= x ^= y;
}
return (s + c(dfn[y], dfn[x] + 1)) % p;
}
int main(){
scanf("%d%d%d", &n, &m, &r);
scanf("%lld", &p);
for (int i = 1; i <= n; ++i){
scanf("%lld", a + i);
}
for (int i = 1; i < n; ++i){
scanf("%d%d", &x, &y);
to[x].emplace_back(y);
to[y].emplace_back(x);
}
depth[r] = 1;
dfs1(r);
dfs2(r, r);
c.build(b);
while (m--){
scanf("%d%d", &o, &x);
switch(o){
case 1:
scanf("%d", &y);
scanf("%lld", &z);
add(x, y, z % p);
break;
case 2:
scanf("%d", &y);
printf("%d\n", query(x, y));
break;
case 3:
scanf("%lld", &z);
c.add(dfn[x], dfn[x] + size[x], z % p);
break;
case 4:
printf("%d\n", c(dfn[x], dfn[x] + size[x]));
break;
}
}
return 0;
}