题解:P3384 【模板】重链剖分/长链剖分

· · 题解

树链剖分,把树剖开分成链。

我们有一棵有根树,从根开始 dfs,记录以下信息:

  1. 每个节点被搜索的顺序,存在 dfn 数组中;
  2. 每个节点的父亲(根节点父亲为 0),存在 fa 数组中;
  3. 每个节点的子树大小(包含自己),存在 size 数组中。

我们容易发现,对于树上以节点 x 为根的子树,其 dfn 取值区间是 \left[dfn_x,dfn_x+size_x\right),是连续的一段区间。并且,如果 x 为非叶子节点,记 y 为 x 按搜索顺序排序的第一个儿子,那么 dfn_y=dfn_x+1。一般情况下,我们会把输入中第一个和 x 相连的非父节点(记为 S_0)作为先搜索的子节点。

那如果我们不先搜索上面提到的 S_0 呢?我们可以挑选子节点中 size 最大的那一个(也叫重儿子)作为先搜索的子节点。这样,我们便得到了重链剖分算法。“重”是指节点 x 的一棵子树大小在所有子树中是最大的(size 最大)。节点与重儿子相连的边叫做重边。相邻的所有重边连接形成的链叫重链。被孤立的节点也是重链。由“记 y 为 x 按搜索顺序排序的第一个儿子,那么 dfn_y=dfn_x+1”得,重链上所有节点的 dfn 是连续的。

如果选择一个子节点 y,以它为根节点的子树深度最深,作为先搜索的子节点,那么就得到了长链剖分算法,这是另一种把树划分成链的算法。

其实这两种划分方法大同小异,这里主要以重链剖分为主,长链剖分只放完整代码不放片段代码。

我们先求:

片段代码:

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;
}

解释 to:to 是 std::vector<int> 类型的数组,to_i 用来存与某节点 i 直接连边的所有节点。

然后按照先搜重儿子的方法,求:

以及将每个点的初始值按照 dfn 排序。

片段代码:

//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);
            }
        }
    }
}

这样,重链剖分的初始化就做好了。

题目要求我们实现以下操作:

操作 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,我们可以这样操作(假设 depth_{top_x}>depth_{top_y}):

  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;
}