跳到正文

题解

题解:P16035 [CSPro 33] 文件夹合并

Adam1101约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年9月9日
收录
2026年10月24日
更新
2026年11月19日
篇幅
约 6 分钟
获赞
1
题目
P16035

版本

  1. v1当前

题意简述

有一棵树,每个节点有一个权值,每次操作可以把一个节点的子节点的权值加到自己上面,并将其子节点删除,孙子节点连到自己上面。每次询问一个结点的深度。

思路

每次合并操作相当于把该节点的子树除了自己以外的所有节点的深度减一,显然可以用树剖 + 树状数组维护。

对于合并 xx 的操作,我们直接暴力遍历 xx 的所有子结点 yy,将 yy 的所有子结点连接到 xx 上,再把 yy 删除。

为了高效实现这个操作,我们可以把 vector 邻接表改为 list,它支持 O(1)O(1) 的插入、删除和拼接。

复杂度分析

树剖 O(n)O(n),树状数组维护 O(nlog⁡n)O(n \log n)。

在合并操作中,每个节点最多作为直接儿子被删除 11 次,使用 list 自带的 ls.splice() O(1)O(1) 拼接邻接表,总复杂度 O(n)O(n)

实现细节

注意修改子树深度时要除去本身,数据量 dd 要开 long long。

AC 代码

C++
class DifferentialBinaryIndexedTree {  // 差分树状数组维护区间加、单点查
public:
    void Set(const vector<int>& a) {
        if (a.empty()) return n_ = 0, void();
        n_ = a.size() - 1;
        tr_.resize(n_ + 1);
        AddImpl(1, a[1]);
        for (int i = 2; i <= n_; i++) AddImpl(i, a[i] - a[i - 1]);
    }
    explicit DifferentialBinaryIndexedTree(const vector<int>& a) { Set(a); }
    explicit DifferentialBinaryIndexedTree() { n_ = 0; }
    void Add(int l, int r, int k) {
        if (l > r) return;
        AddImpl(l, k);
        AddImpl(r + 1, -k);
    }
    int Qry(int x) { return SumImpl(x); }

private:
    int n_;
    vector<int> tr_;
    void AddImpl(int x, int k) {
        for (int i = x; i <= n_; i += (i & -i)) tr_[i] += k;
    }
    int SumImpl(int x) {
        int ans = 0;
        for (int i = x; i; i -= (i & -i)) ans += tr_[i];
        return ans;
    }
} tr;
vector<list<int>> g;
vector<long long> data;
vector<int> fa, dep, siz, hson;
vector<int> dfs_order, dfs_rank, top;
int timer;
void DfsHson(int x, int now_dep) {  // 树剖 DFS1
    dep[x] = now_dep, siz[x] = 1;
    int maxn = 0;
    for (auto y : g[x]) {
        DfsHson(y, now_dep + 1);
        siz[x] += siz[y];
        if (siz[y] > maxn) maxn = siz[y], hson[x] = y;
    }
}
void DfsChain(int x, int now_top) {  // 树剖 DFS2
    dfs_order[++timer] = dep[x];
    dfs_rank[x] = timer;
    top[x] = now_top;
    if (hson[x]) DfsChain(hson[x], now_top);
    for (auto y : g[x]) {
        if (y == hson[x]) continue;
        DfsChain(y, y);
    }
}
void AddSubtree(int x, int k) {  // 子树加(不包括本身)
    tr.Add(dfs_rank[x] + 1, dfs_rank[x] + siz[x] - 1, k);
}
int main() {
    int n, m;
    cin >> n >> m;
    g.resize(n + 1);
    data.resize(n + 1);
    fa.resize(n + 1), dep.resize(n + 1);
    siz.resize(n + 1), hson.resize(n + 1);
    dfs_order.resize(n + 1), dfs_rank.resize(n + 1), top.resize(n + 1);
    for (int i = 2; i <= n; i++) {
        cin >> fa[i];
        g[fa[i]].push_back(i);
    }
    for (int i = 1; i <= n; i++) cin >> data[i];
    DfsHson(1, 1);
    DfsChain(1, 1);
    tr.Set(dfs_order);
    while (m--) {
        int op, x;
        cin >> op >> x;
        if (op == 1) {
            list<int> old;
            old.swap(g[x]);  // g[x] 变为空,old 是原来的儿子
            for (int y : old) {
                data[x] += data[y];
                g[x].splice(g[x].end(), g[y]);  // 把 y 的儿子接到 x 的儿子列表
            }
            AddSubtree(x, -1);
            cout << g[x].size() << " " << data[x] << "\n";
        } else {
            cout << tr.Qry(dfs_rank[x]) << "\n";
        }
    }
    return 0;
}

说句闲话,西西艾弗有限公司竟然是真实存在的!

原文
存档评论4 条抓取于