2020牛客多校七 C. A National Pandemic (树链剖分)

题意:
题解:树链剖分
什么?你要看点分树做法?不会

操作2很简单,维护一个delta,取min相当于减去当前自身的值,用delta记录进行当前操作时自身的值即可,操作3查询的时候直接减去就ok了。

接下来考虑操作1。

w − d i s ( x , y ) = w − ( d e p [ x ] + d e p [ y ] − 2 ∗ d e p [ l c a ( x , y ) ] ) w-dis(x,y)=w-(dep[x]+dep[y]-2*dep[lca(x,y)]) wdis(x,y)=w(dep[x]+dep[y]2dep[lca(x,y)])
上面这个化简很容易得出,由于 w 和 x 是题目每次进行操作1给出的,我们用 S S S记录所有 w − d e p [ x ] w-dep[x] wdep[x]的和即可, d e p [ y ] dep[y] dep[y]的话查询的时候直接算就行,记得乘上操作1的次数 n u m num num

接下来我们再考虑如何求 d e p [ l c a ( x , y ) ] dep[lca(x,y)] dep[lca(x,y)]

对于操作1给出的 x ,我们将 x 及其所有祖先+1,那我们在求 y 到 rt 的点权和时,是不是就是把 l c a ( x , y ) lca(x,y) lca(x,y)以上的点求和,也就是 d e p [ l c a ( x , y ) ] dep[lca(x,y)] dep[lca(x,y)],这样做的正确性完全是可以保证的,因为 lca 永远只会在 y 到 rt 的路径上。至此我们的问题就结束了。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
//树链剖分
const int MAXN = 5e5 + 5;
int sum[MAXN << 2], add[MAXN << 2];
int head[MAXN << 1], pre[MAXN], id[MAXN];
int siz[MAXN], son[MAXN], fa[MAXN];
int dep[MAXN], top[MAXN];
ll delta[MAXN];
struct edge {
    int to;
    int next;
}e[MAXN << 1];
int tot, sign;
/*------------准备阶段--------------*/
void init() {
    memset(head, -1, sizeof(head));
    memset(id, 0, sizeof(id));
    memset(siz, 0, sizeof(siz));
    memset(son, 0, sizeof(son));
    memset(fa, 0, sizeof(fa));
    memset(dep, 0, sizeof(dep));
    memset(top, 0, sizeof(top));
    memset(delta, 0, sizeof(delta));
    tot = sign = 0;
}
void addedge(int u, int v) {
    e[tot].to = v, e[tot].next = head[u], head[u] = tot++;
}
/*--------------dfs-----------------*/
void dfs1(int u, int f) {    //1 0
    dep[u] = dep[f] + 1;
    fa[u] = f;
    siz[u] = 1;
    for (int i = head[u]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == f) continue;
        dfs1(to, u);
        siz[u] += siz[to];
        if (siz[to] > siz[son[u]]) son[u] = to;
    }
}
void dfs2(int u, int Top) {  //1 1
    id[u] = ++sign;
    pre[sign] = u;
    top[u] = Top;
    if (son[u]) dfs2(son[u], Top);
    for (int i = head[u]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (to == son[u] || to == fa[u]) continue;
        dfs2(to, to);
    }
}
/*--------------线段树--------------*/
struct Node {
    int l, r;
    int mid() { return (l + r) >> 1; }
} tree[MAXN << 2];
void PushDown(int rt, int m) {
    if (add[rt]) {
        add[rt << 1] += add[rt];
        add[rt << 1 | 1] += add[rt];
        sum[rt << 1] += add[rt] * (m - (m >> 1));
        sum[rt << 1 | 1] += add[rt] * (m >> 1);
        add[rt] = 0;
    }
}
void build(int l, int r, int rt) {
    tree[rt].l = l;
    tree[rt].r = r;
    add[rt] = 0;
    sum[rt] = 0;
    if (l == r) {
        sum[rt] = 0;
        return;
    }
    int m = tree[rt].mid();
    build(l, m, rt << 1);
    build(m + 1, r, rt << 1 | 1);
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update(int c, int l, int r, int rt) {
    if (tree[rt].l == l && r == tree[rt].r) {
        add[rt] += c;
        sum[rt] += c * (r - l + 1);
        return;
    }
    if (tree[rt].l == tree[rt].r) return;
    PushDown(rt, tree[rt].r - tree[rt].l + 1);
    int m = tree[rt].mid();
    if (r <= m) update(c, l, r, rt << 1);
    else if (l > m) update(c, l, r, rt << 1 | 1);
    else {
        update(c, l, m, rt << 1);
        update(c, m + 1, r, rt << 1 | 1);
    }
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
int query(int pos, int rt) {
    if (tree[rt].l == tree[rt].r) {
        return sum[rt];
    }
    PushDown(rt, tree[rt].r - tree[rt].l + 1);
    int m = tree[rt].mid();
    int res = 0;
    if (pos <= m) res = query(pos, rt << 1);
    else res = query(pos, rt << 1 | 1);
    return res;
}
ll query2(int l, int r, int rt) {
    if (l == tree[rt].l && r == tree[rt].r) {
        return sum[rt];
    }
    PushDown(rt, tree[rt].r - tree[rt].l + 1);
    int m = tree[rt].mid();
    ll res = 0;
    if (r <= m) res += query2(l, r, rt << 1);
    else if (l > m) res += query2(l, r, rt << 1 | 1);
    else {
        res += query2(l, m, rt << 1);
        res += query2(m + 1, r, rt << 1 | 1);
    }
    return res;
}
/*------------树链剖分-------------*/
ll getSum(int x, int y) {  //查询x到y路径上所有结点和
    ll res = 0;
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        res += query2(id[top[x]], id[x], 1);
        x = fa[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    res += query2(id[x], id[y], 1);
    return res;
}
void solve(int x, int y, int c) {  //更新x到y路径上结点值
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        update(c, id[top[x]], id[x], 1);
        x = fa[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    update(c, id[x], id[y], 1);
}
ll S, num;
ll queryX(int x) {
    return S - num * dep[x] + 2 * getSum(1, x);
}
int n, m, u, v, ind, x, t, w;
int main() {
    scanf("%d", &t);
    while (t--) {
        init();
        S = num = 0;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n - 1; i++) {
            scanf("%d%d", &u, &v);
            addedge(u, v);
            addedge(v, u);
        }
        dfs1(1, 0);
        dfs2(1, 1);
        build(1, n, 1);
        while (m--) {
            scanf("%d%d", &ind, &x);
            if (ind == 1) {
                scanf("%d", &w);
                S += w - dep[x];
                num++;
                solve(1, x, 1);
            }
            else if (ind == 2) {  //与0取min,就是减去自身值
                ll ans = queryX(x) - delta[x];
                if(ans > 0) delta[x] += ans;  //delta保存当前值
            }
            else {
                ll ans = queryX(x) - delta[x];
                printf("%lld\n", ans);
            }
        }
    }
    return 0;
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页