萌新树剖过样例全 WA 求助
查看原帖
萌新树剖过样例全 WA 求助
610557
shinzanmonoszm 妹妹楼主2022/12/24 10:06
#include<iostream>
#include<algorithm>
using ll = long long;
const int sz = 1e5 + 10;
int arr[sz], n, q, rnk[sz];
struct ST {
    ll tree[sz << 2], lazy[sz << 2];
    void pushdown(int p, int ln, int rn) {
        if (lazy[p]) {
            int mid = ln + rn >> 1;
            tree[p << 1] += lazy[p] * (mid - ln + 1);
            lazy[p << 1] += lazy[p];
            tree[p << 1 | 1] += lazy[p] * (rn - mid);
            lazy[p << 1 | 1] += lazy[p];
            lazy[p] = 0;
        }
    }
    void build(int p, int ln, int rn) {
        if (ln == rn) return tree[p] = arr[rnk[ln]], void();
        int mid = ln + rn >> 1;
        build(p << 1, ln, mid);
        build(p << 1 | 1, mid + 1, rn);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    void addp(int p, int ln, int rn, int pos, ll val) {
        if (ln == rn) return tree[p] += val, void();
        int mid = ln + rn >> 1;
        if (pos <= mid) addp(p << 1, ln, mid, pos, val);
        else addp(p << 1 | 1, mid + 1, rn, pos, val);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    void addi(int p, int ln, int rn, int l, int r, ll val) {
        if (ln >= l && rn <= r)
            return lazy[p] += val, tree[p] += val * (rn - ln + 1), void();
        if (rn < l || ln > r) return;
        int mid = ln + rn >> 1;
        pushdown(p, ln, rn);
        addi(p << 1, ln, mid, l, r, val);
        addi(p << 1 | 1, mid + 1, rn, l, r, val);
        tree[p] = tree[p << 1] + tree[p << 1 | 1];
    }
    ll query(int p, int ln, int rn, int l, int r) {
        if (ln >= l && rn <= r) return tree[p];
        if (rn < l || ln > r) return 0;
        int mid = ln + rn >> 1;
        ll res = 0;
        pushdown(p, ln, rn);
        res += query(p << 1, ln, mid, l, r);
        res += query(p << 1 | 1, mid + 1, rn, l, r);
        return res;
    }
} st;
struct edge {
    int nxt, to;
} graph[sz << 1];
int head[sz], hpp;
void addEdge(int from, int to) {
    graph[++hpp] = edge{head[from], to};
    head[from] = hpp;
}
int dfn[sz], dpp, dep[sz], top[sz], fa[sz], hson[sz], ssz[sz], last[sz];
void buildDFS(int u, int fau) {
    dep[u] = dep[fau] + 1, ssz[u] = 1, fa[u] = fau;
    for (int p = head[u]; p; p = graph[p].nxt) {
        int v = graph[p].to;
        if (v == fau) continue;
        buildDFS(v, u);
        ssz[u] += ssz[v];
        if (ssz[v] > ssz[hson[u]]) hson[u] = v;
    }
}
void chainDFS(int u, int t) {
    dfn[u] = ++dpp, top[u] = t, rnk[dpp] = u;
    if (!hson[u]) return;
    chainDFS(hson[u], t);
    for (int p = head[u]; p; p = graph[p].nxt) {
        int v = graph[p].to;
        if (v == hson[u] || v == fa[u]) continue;
        chainDFS(v, v);
    }
    last[u] = dpp;
}
ll query(int u, int v) {
    ll res = 0;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
        res += st.query(1, 1, n, dfn[top[u]], dfn[u]);
        u = fa[top[u]];
    }
    if (dfn[u] > dfn[v]) std::swap(u, v);
    res += st.query(1, 1, n, dfn[u], dfn[v]);
    return res;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin >> n >> q;
    for (int i = 1; i <= n; i++) std::cin >> arr[i];
    for (int i = 1, u, v; i < n; i++)
        std::cin >> u >> v, addEdge(u, v), addEdge(v, u);
    buildDFS(1, 0);
    chainDFS(1, 1);
    st.build(1, 1, n);
    while (q--) {
        int op, x;
        ll a;
        std::cin >> op >> x;
        if (op == 1) std::cin >> a, st.addp(1, 1, n, dfn[x], a);
        if (op == 2) std::cin >> a, st.addi(1, 1, n, dfn[x], last[x], a);
        if (op == 3) std::cout << query(1, x) << "\n";
    }
    return 0;
}

2022/12/24 10:06
加载中...