firiexp's Library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_vertex_add_subtree_sum_dsu_on_tree.test.cpp

Depends on

Code

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>

#include "../util/fastio.cpp"
#include "../datastructure/binaryindexedtree.cpp"
#include "../tree/dsu_on_tree.cpp"

struct Query {
    int time, type;
    ll x;
};

int main() {
    Scanner sc;
    Printer pr;

    int n, q;
    sc.read(n, q);
    vector<ll> a(n);
    for (auto &&x : a) sc.read(x);

    vector<vector<int>> g(n);
    for (int v = 1; v < n; ++v) {
        int p;
        sc.read(p);
        g[p].push_back(v);
        g[v].push_back(p);
    }

    vector<vector<Query>> qs(n);
    vector<ll> ans(q, -1);
    for (int i = 0; i < q; ++i) {
        int t, v;
        sc.read(t, v);
        if (t == 0) {
            ll x;
            sc.read(x);
            qs[v].push_back({i, 0, x});
        } else {
            qs[v].push_back({i, 1, 0});
        }
    }

    BIT<ll> bit(q + 1);
    vector<pair<int, ll>> history;
    ll base = 0;

    auto update = [&](int v) {
        base += a[v];
        for (auto &&qu : qs[v]) {
            if (qu.type == 0) {
                bit.add(qu.time, qu.x);
                history.push_back({qu.time, qu.x});
            }
        }
    };
    auto query = [&](int v) {
        for (auto &&qu : qs[v]) {
            if (qu.type == 1) ans[qu.time] = base + bit.sum(qu.time);
        }
    };
    auto clear = [&](int) {};
    auto reset = [&]() {
        base = 0;
        for (auto &&[t, x] : history) bit.add(t, -x);
        history.clear();
    };

    DSUonTree dsu(g);
    dsu.run(update, query, clear, reset);

    for (auto &&x : ans) {
        if (x != -1) pr.println(x);
    }
    return 0;
}
#line 1 "test/yosupo_vertex_add_subtree_sum_dsu_on_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#line 11 "test/yosupo_vertex_add_subtree_sum_dsu_on_tree.test.cpp"
#include <type_traits>

#line 1 "util/fastio.cpp"
using namespace std;

extern "C" int fileno(FILE *);
extern "C" int isatty(int);

template<class T, class = void>
struct is_fastio_range : false_type {};

template<class T>
struct is_fastio_range<T, void_t<decltype(declval<T &>().begin()), decltype(declval<T &>().end())>> : true_type {};

template<class T, class = void>
struct has_fastio_value : false_type {};

template<class T>
struct has_fastio_value<T, void_t<decltype(declval<const T &>().value())>> : true_type {};

struct FastIoDigitTable {
    char num[40000];

    constexpr FastIoDigitTable() : num() {
        for (int i = 0; i < 10000; ++i) {
            int x = i;
            for (int j = 3; j >= 0; --j) {
                num[i * 4 + j] = char('0' + x % 10);
                x /= 10;
            }
        }
    }
};

struct Scanner {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE + 1];
    int idx, size;
    bool interactive;

    Scanner() : idx(0), size(0), interactive(isatty(fileno(stdin))) {}

    inline void load() {
        int len = size - idx;
        memmove(buf, buf + idx, len);
        if (interactive) {
            if (fgets(buf + len, BUFSIZE + 1 - len, stdin)) size = len + (int)strlen(buf + len);
            else size = len;
        } else {
            size = len + (int)fread(buf + len, 1, BUFSIZE - len, stdin);
        }
        idx = 0;
        buf[size] = 0;
    }

    inline void ensure() {
        if (idx + OFFSET > size) load();
    }

    inline void ensure_interactive() {
        if (idx == size) load();
    }

    inline char skip() {
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            return buf[idx++];
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        return buf[idx++];
    }

    template<class T, typename enable_if<is_integral<T>::value, int>::type = 0>
    void read(T &x) {
        if (interactive) {
            char c = skip();
            bool neg = false;
            if constexpr (is_signed<T>::value) {
                if (c == '-') {
                    neg = true;
                    ensure_interactive();
                    c = buf[idx++];
                }
            }
            x = 0;
            while (c >= '0') {
                x = x * 10 + (c & 15);
                ensure_interactive();
                c = buf[idx++];
            }
            if constexpr (is_signed<T>::value) {
                if (neg) x = -x;
            }
            return;
        }
        char c = skip();
        bool neg = false;
        if constexpr (is_signed<T>::value) {
            if (c == '-') {
                neg = true;
                c = buf[idx++];
            }
        }
        x = 0;
        while (c >= '0') {
            x = x * 10 + (c & 15);
            c = buf[idx++];
        }
        if constexpr (is_signed<T>::value) {
            if (neg) x = -x;
        }
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void read(T &x) {
        long long v;
        read(v);
        x = T(v);
    }

    template<class Head, class Next, class... Tail>
    void read(Head &head, Next &next, Tail &...tail) {
        read(head);
        read(next, tail...);
    }

    template<class T, class U>
    void read(pair<T, U> &p) {
        read(p.first, p.second);
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void read(T &a) {
        for (auto &x : a) read(x);
    }

    void read(char &c) {
        c = skip();
    }

    void read(string &s) {
        s.clear();
        if (interactive) {
            ensure_interactive();
            while (buf[idx] && buf[idx] <= ' ') {
                ++idx;
                ensure_interactive();
            }
            while (true) {
                int start = idx;
                while (idx < size && buf[idx] > ' ') ++idx;
                s.append(buf + start, idx - start);
                if (idx < size) break;
                load();
                if (size == 0) break;
            }
            if (idx < size) ++idx;
            return;
        }
        ensure();
        while (buf[idx] && buf[idx] <= ' ') {
            ++idx;
            ensure();
        }
        while (true) {
            int start = idx;
            while (idx < size && buf[idx] > ' ') ++idx;
            s.append(buf + start, idx - start);
            if (idx < size) break;
            load();
        }
        if (idx < size) ++idx;
    }
};

struct Printer {
    static constexpr int BUFSIZE = 1 << 17;
    static constexpr int OFFSET = 64;
    char buf[BUFSIZE];
    int idx;
    bool interactive;
    inline static constexpr FastIoDigitTable table{};

    Printer() : idx(0), interactive(isatty(fileno(stdout))) {}
    ~Printer() { flush(); }

    inline void flush() {
        if (idx) {
            fwrite(buf, 1, idx, stdout);
            idx = 0;
        }
    }

    inline void pc(char c) {
        if (idx > BUFSIZE - OFFSET) flush();
        buf[idx++] = c;
        if (interactive && c == '\n') flush();
    }

    inline void print_range(const char *s, size_t n) {
        size_t pos = 0;
        while (pos < n) {
            if (idx == BUFSIZE) flush();
            size_t chunk = min(n - pos, (size_t)(BUFSIZE - idx));
            memcpy(buf + idx, s + pos, chunk);
            idx += (int)chunk;
            pos += chunk;
        }
    }

    void print(const char *s) {
        print_range(s, strlen(s));
    }

    void print(const string &s) {
        print_range(s.data(), s.size());
    }

    void print(char c) {
        pc(c);
    }

    void print(bool b) {
        pc(char('0' + (b ? 1 : 0)));
    }

    template<class T, typename enable_if<is_integral<T>::value && !is_same<T, bool>::value, int>::type = 0>
    void print(T x) {
        if (idx > BUFSIZE - 100) flush();
        using U = typename make_unsigned<T>::type;
        U y;
        if constexpr (is_signed<T>::value) {
            if (x < 0) {
                buf[idx++] = '-';
                y = U(0) - static_cast<U>(x);
            } else {
                y = static_cast<U>(x);
            }
        } else {
            y = x;
        }
        if (y == 0) {
            buf[idx++] = '0';
            return;
        }
        static constexpr int TMP_SIZE = sizeof(U) * 10 / 4;
        char tmp[TMP_SIZE];
        int pos = TMP_SIZE;
        while (y >= 10000) {
            pos -= 4;
            memcpy(tmp + pos, table.num + (y % 10000) * 4, 4);
            y /= 10000;
        }
        if (y >= 1000) {
            memcpy(buf + idx, table.num + (y << 2), 4);
            idx += 4;
        } else if (y >= 100) {
            memcpy(buf + idx, table.num + (y << 2) + 1, 3);
            idx += 3;
        } else if (y >= 10) {
            unsigned q = (unsigned(y) * 205) >> 11;
            buf[idx] = char('0' + q);
            buf[idx + 1] = char('0' + (unsigned(y) - q * 10));
            idx += 2;
        } else {
            buf[idx++] = char('0' + y);
        }
        memcpy(buf + idx, tmp + pos, TMP_SIZE - pos);
        idx += TMP_SIZE - pos;
    }

    template<class T, typename enable_if<!is_integral<T>::value && !is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value && has_fastio_value<T>::value, int>::type = 0>
    void print(const T &x) {
        print(x.value());
    }

    template<class T, typename enable_if<is_fastio_range<T>::value && !is_same<typename decay<T>::type, string>::value, int>::type = 0>
    void print(const T &a) {
        bool first = true;
        for (auto &&x : a) {
            if (!first) pc(' ');
            first = false;
            print(x);
        }
    }

    template<class T>
    void println(const T &x) {
        print(x);
        pc('\n');
    }

    template<class Head, class... Tail>
    void println(const Head &head, const Tail &...tail) {
        print(head);
        ((pc(' '), print(tail)), ...);
        pc('\n');
    }

    void println() {
        pc('\n');
    }
};

template<class T>
Scanner &operator>>(Scanner &in, T &x) {
    in.read(x);
    return in;
}

template<class T>
Printer &operator<<(Printer &out, const T &x) {
    out.print(x);
    return out;
}

/**
 * @brief 高速入出力(Fast IO)
 */
#line 1 "datastructure/binaryindexedtree.cpp"
template<class T>
class BIT {
    vector<T> bit;
    int m, n;
public:
    BIT(int n): bit(n), m(1), n(n) {
        while (m < n) m <<= 1;
    }

    T sum(int k){
        T ret = 0;
        for (; k > 0; k -= (k & -k)) ret += bit[k - 1];
        return ret;
    }

    void add(int k, T x){
        for (k++; k <= n; k += (k & -k)) bit[k - 1] += x;
    }

    int lower_bound(T x) {
        if (x <= 0) return 0;
        int i = 0;
        for (int j = m; j; j >>= 1) {
            if (i + j <= n && bit[i + j - 1] < x) x -= bit[i + j - 1], i += j;
        }
        return min(i + 1, n);
    }
};

/**
 * @brief Binary Indexed Tree(BIT)
 */
#line 1 "tree/dsu_on_tree.cpp"
template<class G>
struct DSUonTree {
    G &g;
    int n, root, ord;
    vector<int> sub_size, euler, down, up;

    explicit DSUonTree(G &g, int root = 0)
        : g(g), n(g.size()), root(root), ord(0),
          sub_size(n), euler(n), down(n), up(n) {
        dfs_sz(root, -1);
        dfs_euler(root, -1);
    }

    int idx(int v) const {
        return down[v];
    }

    int begin(int v) const {
        return down[v];
    }

    int end(int v) const {
        return up[v];
    }

    template<class UPDATE, class QUERY, class CLEAR, class RESET>
    void run(UPDATE update, QUERY query, CLEAR clear, RESET reset) {
        auto dfs = [&](auto &&self, int v, int p, bool keep) -> void {
            int heavy = (g[v].empty() || g[v][0] == p ? -1 : g[v][0]);
            for (auto &&u : g[v]) {
                if (u == p || u == heavy) continue;
                self(self, u, v, false);
            }
            if (heavy != -1) self(self, heavy, v, true);
            for (auto &&u : g[v]) {
                if (u == p || u == heavy) continue;
                for (int i = down[u]; i < up[u]; ++i) update(euler[i]);
            }
            update(v);
            query(v);
            if (!keep) {
                for (int i = down[v]; i < up[v]; ++i) clear(euler[i]);
                reset();
            }
        };
        dfs(dfs, root, -1, false);
    }

private:
    void dfs_sz(int v, int p) {
        sub_size[v] = 1;
        int heavy_idx = -1;
        for (int i = 0; i < (int)g[v].size(); ++i) {
            int u = g[v][i];
            if (u == p) continue;
            dfs_sz(u, v);
            sub_size[v] += sub_size[u];
            if (heavy_idx == -1 || sub_size[u] > sub_size[g[v][heavy_idx]]) {
                heavy_idx = i;
            }
        }
        if (heavy_idx > 0) swap(g[v][0], g[v][heavy_idx]);
    }

    void dfs_euler(int v, int p) {
        down[v] = ord;
        euler[ord++] = v;
        for (auto &&u : g[v]) {
            if (u == p) continue;
            dfs_euler(u, v);
        }
        up[v] = ord;
    }
};

/**
 * @brief DSU on Tree
 */
#line 16 "test/yosupo_vertex_add_subtree_sum_dsu_on_tree.test.cpp"

struct Query {
    int time, type;
    ll x;
};

int main() {
    Scanner sc;
    Printer pr;

    int n, q;
    sc.read(n, q);
    vector<ll> a(n);
    for (auto &&x : a) sc.read(x);

    vector<vector<int>> g(n);
    for (int v = 1; v < n; ++v) {
        int p;
        sc.read(p);
        g[p].push_back(v);
        g[v].push_back(p);
    }

    vector<vector<Query>> qs(n);
    vector<ll> ans(q, -1);
    for (int i = 0; i < q; ++i) {
        int t, v;
        sc.read(t, v);
        if (t == 0) {
            ll x;
            sc.read(x);
            qs[v].push_back({i, 0, x});
        } else {
            qs[v].push_back({i, 1, 0});
        }
    }

    BIT<ll> bit(q + 1);
    vector<pair<int, ll>> history;
    ll base = 0;

    auto update = [&](int v) {
        base += a[v];
        for (auto &&qu : qs[v]) {
            if (qu.type == 0) {
                bit.add(qu.time, qu.x);
                history.push_back({qu.time, qu.x});
            }
        }
    };
    auto query = [&](int v) {
        for (auto &&qu : qs[v]) {
            if (qu.type == 1) ans[qu.time] = base + bit.sum(qu.time);
        }
    };
    auto clear = [&](int) {};
    auto reset = [&]() {
        base = 0;
        for (auto &&[t, x] : history) bit.add(t, -x);
        history.clear();
    };

    DSUonTree dsu(g);
    dsu.run(update, query, clear, reset);

    for (auto &&x : ans) {
        if (x != -1) pr.println(x);
    }
    return 0;
}
Back to top page