Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/library_checker/tree/vertex_add_subtree_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/heavy_light_decomposition.hpp"
#include "../../../src/data_structure/fenwick_tree.hpp"
int main(void) {
    int n, q;
    cin >> n >> q;
    vector<ll> a(n);
    rep(i, 0, n) {
        cin >> a[i];
    }
    Graph<int> g(n);
    rep(i, 1, n) {
        int p;
        cin >> p;
        g.add_edge(i, p);
    }
    HeavyLightDecomposition<int> hld(g);
    FenwickTree<ll> fw(n);
    rep(i, 0, n) {
        fw.add(hld.idx(i).first, a[i]);
    }
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            ll p, x;
            cin >> p >> x;
            fw.add(hld.idx(p).first, x);
        } else {
            int u;
            cin >> u;
            ll ans = 0;
            auto query = [&](int u, int v) -> void {
                ans += fw.sum(u, v);
            };
            hld.subtree_query(u, true, query);
            cout << ans << '\n';
        }
    }
}
#line 1 "verify/library_checker/tree/vertex_add_subtree_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 4 "src/tree/heavy_light_decomposition.hpp"
template <typename T>
struct HeavyLightDecomposition {
    HeavyLightDecomposition(Graph<T>& _g, const int root = 0)
        : g(_g), n(g.size()), id(0), sz(n, 0), dep(n, 0), down(n, -1), up(n, -1), nex(n, root), par(n, -1), rev(n, 0), co(n, 0) {
        assert(0 <= root and root < n);
        dfs_sz(root);
        dfs_hld(root);
    }
    pair<int, int> idx(const int i) const {
        assert(0 <= i and i < n);
        return make_pair(down[i], up[i]);
    }
    int depth(const int v) const {
        assert(0 <= v and v < n);
        return dep[v];
    }
    T cost(const int v) const {
        assert(0 <= v and v < n);
        return co[v];
    }
    int parent(const int v) const {
        assert(0 <= v and v < n);
        return par[v];
    }
    int la(int v, int x) const {
        assert(0 <= v and v < n);
        assert(x >= 0);
        if(x > dep[v]) return -1;
        while(true) {
            const int u = nex[v];
            if(down[v] - x >= down[u]) return rev[down[v] - x];
            x -= down[v] - down[u] + 1;
            v = par[u];
        }
    }
    int lca(int u, int v) const {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        while(nex[u] != nex[v]) {
            if(down[u] < down[v]) swap(u, v);
            u = par[nex[u]];
        }
        return dep[u] < dep[v] ? u : v;
    }
    int dist(const int u, const int v) const {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        return dep[u] + dep[v] - dep[lca(u, v)] * 2;
    }
    T length(const int u, const int v) const {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        return co[u] + co[v] - co[lca(u, v)] * 2;
    }
    template <typename F>
    void path_query(const int u, const int v, const bool vertex, const F& f) {
        assert(0 <= u and u < n);
        assert(0 <= v and v < n);
        const int l = lca(u, v);
        for(auto&& [a, b] : ascend(u, l)) f(a + 1, b);
        if(vertex) f(down[l], down[l] + 1);
        for(auto&& [a, b] : descend(l, v)) f(a, b + 1);
    }
    template <typename F>
    void subtree_query(const int v, const bool vertex, const F& f) {
        assert(0 <= v and v < n);
        f(down[v] + int(!vertex), up[v]);
    }

   private:
    Graph<T>& g;
    int n, id;
    vector<int> sz, dep, down, up, nex, par, rev;
    vector<T> co;
    void dfs_sz(const int cur) {
        sz[cur] = 1;
        for(Edge<T>& edge : g[cur]) {
            if(edge.to == par[cur]) {
                if(g[cur].size() >= 2 and edge.to == g[cur][0].to) {
                    swap(g[cur][0], g[cur][1]);
                } else {
                    continue;
                }
            }
            dep[edge.to] = dep[cur] + 1;
            co[edge.to] = co[cur] + edge.cost;
            par[edge.to] = cur;
            dfs_sz(edge.to);
            sz[cur] += sz[edge.to];
            if(sz[edge.to] > sz[g[cur][0].to]) {
                swap(edge, g[cur][0]);
            }
        }
    }
    void dfs_hld(const int cur) {
        down[cur] = id++;
        rev[down[cur]] = cur;
        for(const Edge<T>& edge : g[cur]) {
            if(edge.to == par[cur]) continue;
            nex[edge.to] = (edge.to == g[cur][0].to ? nex[cur] : edge.to);
            dfs_hld(edge.to);
        }
        up[cur] = id;
    }
    vector<pair<int, int>> ascend(int u, int v) const {
        vector<pair<int, int>> res;
        while(nex[u] != nex[v]) {
            res.emplace_back(down[u], down[nex[u]]);
            u = par[nex[u]];
        }
        if(u != v) res.emplace_back(down[u], down[v] + 1);
        return res;
    }
    vector<pair<int, int>> descend(const int u, const int v) const {
        if(u == v) return {};
        if(nex[u] == nex[v]) return {{down[u] + 1, down[v]}};
        auto res = descend(u, par[nex[v]]);
        res.emplace_back(down[nex[v]], down[v]);
        return res;
    }
};
#line 3 "src/data_structure/fenwick_tree.hpp"
template <typename T>
struct FenwickTree {
    FenwickTree(const int N)
        : n(N), data(N) {}
    void add(int p, const T& x) {
        assert(0 <= p and p < n);
        ++p;
        while(p <= n) {
            data[p - 1] += x;
            p += p & -p;
        }
    }
    T sum(const int l, const int r) const {
        assert(0 <= l and l <= r and r <= n);
        return sum(r) - sum(l);
    }
    T get(const int x) const {
        assert(0 <= x and x < n);
        return sum(x + 1) - sum(x);
    }

   private:
    int n;
    vector<T> data;
    inline T sum(int r) const {
        T s = 0;
        while(r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};
#line 6 "verify/library_checker/tree/vertex_add_subtree_sum.test.cpp"
int main(void) {
    int n, q;
    cin >> n >> q;
    vector<ll> a(n);
    rep(i, 0, n) {
        cin >> a[i];
    }
    Graph<int> g(n);
    rep(i, 1, n) {
        int p;
        cin >> p;
        g.add_edge(i, p);
    }
    HeavyLightDecomposition<int> hld(g);
    FenwickTree<ll> fw(n);
    rep(i, 0, n) {
        fw.add(hld.idx(i).first, a[i]);
    }
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            ll p, x;
            cin >> p >> x;
            fw.add(hld.idx(p).first, x);
        } else {
            int u;
            cin >> u;
            ll ans = 0;
            auto query = [&](int u, int v) -> void {
                ans += fw.sum(u, v);
            };
            hld.subtree_query(u, true, query);
            cout << ans << '\n';
        }
    }
}
Back to top page