Kuro-orzz's Library

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

View on GitHub

:heavy_check_mark: Tree/Vertex_add_subtree_sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"

#include "../template.h"
#include "EulerTour/EulerTour.h"
#include "../DataStructure/Fenwick/Fenwick.h"

void solve() {
    int n, q; cin >> n >> q;
    int a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int x; cin >> x;
        adj[i].push_back(x);
        adj[x].push_back(i);
    }
    vector<int> st, en;
    vector<int> tour = EulerTour(st, en, adj);
    Fenwick<ll> BIT((int)tour.size() + 1);
    for (int i = 0; i < n; i++) BIT.update(st[i], a[i]);
    while (q--) {
        int type; cin >> type;
        if (type == 0) {
            int u, x; cin >> u >> x;
            BIT.update(st[u], x);
        } else if (type == 1) {
            int u; cin >> u;
            cout << BIT.get(st[u], en[u]) << '\n';
        }
    }
}
#line 1 "Tree/Vertex_add_subtree_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"

#line 2 "template.h"

#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18

#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

const int mod = 998244353;

void solve();

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // cin.exceptions(cin.failbit);
    // int t; cin >> t;
    // while(t--)
        solve();
    cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
    return 0;
}
#line 2 "Tree/EulerTour/EulerTour.h"

vector<int> EulerTour(vector<int> &st, vector<int> &en, 
                vector<vector<int>> &adj, int root = 0, int type = 0) {
    int n = adj.size();
    st.assign(n, 0);
    en.assign(n, 0);
    vector<int> tour;
    if (type == 0) tour.reserve(n);
    else if (type == 1) tour.reserve(2 * n);
    int pos = 0;
    function<void(int, int)> dfs = [&] (int u, int par) {
        tour.push_back(u);
        st[u] = ++pos;
        for (int v : adj[u]) {
            if (v == par) continue;
            dfs(v, u);
        }
        if (type == 1) {
            tour.push_back(u);
            pos++;
        }
        en[u] = pos;
    };
    dfs(root, -1);
    return tour;
}
#line 2 "DataStructure/Fenwick/Fenwick.h"

template <typename T>
struct Fenwick{
    int n;
    vector<T> fen;

    Fenwick() {}
    Fenwick(int _n): n(_n), fen(_n+1) {}

    void update(int pos, T val) {
        for (; pos <= n; pos += pos & -pos) {
            fen[pos] += val;
        }
    }

    T get(int pos) {
        T ans = 0;
        for (; pos > 0; pos -= pos & -pos) {
            ans += fen[pos];
        }
        return ans;
    }

    T get(int l, int r) {
        return get(r) - get(l - 1);
    }

    void update_range(int l, int r, T val){
        update(l, val);
        update(r+1, -val);
    }

    void reset() {
        fill(all(fen), T(0));
    }
};
#line 6 "Tree/Vertex_add_subtree_sum.test.cpp"

void solve() {
    int n, q; cin >> n >> q;
    int a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<vector<int>> adj(n);
    for (int i = 1; i < n; i++) {
        int x; cin >> x;
        adj[i].push_back(x);
        adj[x].push_back(i);
    }
    vector<int> st, en;
    vector<int> tour = EulerTour(st, en, adj);
    Fenwick<ll> BIT((int)tour.size() + 1);
    for (int i = 0; i < n; i++) BIT.update(st[i], a[i]);
    while (q--) {
        int type; cin >> type;
        if (type == 0) {
            int u, x; cin >> u >> x;
            BIT.update(st[u], x);
        } else if (type == 1) {
            int u; cin >> u;
            cout << BIT.get(st[u], en[u]) << '\n';
        }
    }
}
Back to top page