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/aizu_online_judge/grl/range_query_on_a_tree_2.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_E"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/heavy_light_decomposition.hpp"
#include "../../../src/data_structure/lazy_segment_tree.hpp"
struct S {
    long long value;
    long long size;
};
using F = long long;
S op(const S& a, const S& b) {
    return {a.value + b.value, a.size + b.size};
}
S e() {
    return {0, 0};
}
S mapping(const F& f, const S& x) {
    return {x.value + f * x.size, x.size};
}
F composition(const F& f, const F& g) {
    return f + g;
}
F id() {
    return 0;
}
int main(void) {
    int n;
    cin >> n;
    Graph<int> g(n);
    rep(i, 0, n) {
        int k;
        cin >> k;
        rep(j, 0, k) {
            int c;
            cin >> c;
            g.add_edge(i, c);
        }
    }
    HeavyLightDecomposition<int> hld(g);
    LazySegmentTree<S, op, e, F, mapping, composition, id> seg(n);
    rep(i, 0, n) seg.set(i, {0, 1});
    int q;
    cin >> q;
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            ll v, w;
            cin >> v >> w;
            auto add = [&](int u, int v) -> void {
                seg.apply(u, v, w);
            };
            hld.path_query(0, v, false, add);
        } else {
            int v;
            cin >> v;
            ll ans = 0;
            auto query = [&](int u, int v) -> void {
                ans += seg.prod(u, v).value;
            };
            hld.path_query(0, v, false, query);
            cout << ans << '\n';
        }
    }
}
#line 1 "verify/aizu_online_judge/grl/range_query_on_a_tree_2.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_E"
#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/lazy_segment_tree.hpp"
template <typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id>
struct LazySegmentTree {
    LazySegmentTree(const int N)
        : LazySegmentTree(vector<S>(N, e())) {}
    LazySegmentTree(const vector<S>& v)
        : n((int)v.size()) {
        size = bit_ceil((unsigned int)n);
        log = countr_zero((unsigned int)size);
        data = vector<S>(2 * size, e());
        lazy = vector<F>(size, id());
        for(int i = 0; i < n; ++i) {
            data[size + i] = v[i];
        }
        for(int i = size - 1; i >= 1; --i) {
            update(i);
        }
    }
    void set(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        for(int i = log; i >= 1; --i) {
            push(p >> i);
        }
        data[p] = x;
        for(int i = 1; i <= log; ++i) {
            update(p >> i);
        }
    }
    S get(int p) {
        assert(0 <= p and p < n);
        p += size;
        for(int i = log; i >= 1; --i) {
            push(p >> i);
        }
        return data[p];
    }
    S prod(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        if(l == r) return e();
        l += size;
        r += size;
        for(int i = log; i >= 1; --i) {
            if(((l >> i) << i) != l) push(l >> i);
            if(((r >> i) << i) != r) push((r - 1) >> i);
        }
        S sml = e(), smr = e();
        while(l < r) {
            if(l & 1) sml = op(sml, data[l++]);
            if(r & 1) smr = op(data[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }
    S all_prod() const {
        return data[1];
    }
    void apply(int l, int r, const F& f) {
        assert(0 <= l and l <= r and r <= n);
        if(l == r) return;
        l += size;
        r += size;
        for(int i = log; i >= 1; --i) {
            if(((l >> i) << i) != l) push(l >> i);
            if(((r >> i) << i) != r) push((r - 1) >> i);
        }
        {
            int l2 = l, r2 = r;
            while(l < r) {
                if(l & 1) all_apply(l++, f);
                if(r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }
        for(int i = 1; i <= log; ++i) {
            if(((l >> i) << i) != l) update(l >> i);
            if(((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)>
    int max_right(const int l) {
        return max_right(l, [](const S& x) { return g(x); });
    }
    template <class G>
    int max_right(int l, const G& g) {
        assert(0 <= l and l <= n);
        assert(g(e()));
        if(l == n) return n;
        l += size;
        for(int i = log; i >= 1; --i) push(l >> i);
        S sm = e();
        do {
            while(l % 2 == 0) l >>= 1;
            if(!g(op(sm, data[l]))) {
                while(l < size) {
                    push(l);
                    l = 2 * l;
                    if(g(op(sm, data[l]))) {
                        sm = op(sm, data[l]);
                        ++l;
                    }
                }
                return l - size;
            }
            sm = op(sm, data[l]);
            ++l;
        } while((l & -l) != l);
        return n;
    }

    template <bool (*g)(S)>
    int min_left(const int r) {
        return min_left(r, [](const S& x) { return g(x); });
    }
    template <class G>
    int min_left(int r, const G& g) {
        assert(0 <= r and r <= n);
        assert(g(e()));
        if(r == 0) return 0;
        r += size;
        for(int i = log; i >= 1; --i) push((r - 1) >> i);
        S sm = e();
        do {
            --r;
            while(r > 1 and (r % 2)) r >>= 1;
            if(!g(op(data[r], sm))) {
                while(r < size) {
                    push(r);
                    r = 2 * r + 1;
                    if(g(op(data[r], sm))) {
                        sm = op(data[r], sm);
                        --r;
                    }
                }
                return r + 1 - size;
            }
            sm = op(data[r], sm);
        } while((r & -r) != r);
        return 0;
    }

   private:
    int n, size, log;
    vector<S> data;
    vector<F> lazy;
    inline void update(const int k) {
        data[k] = op(data[2 * k], data[2 * k + 1]);
    }
    inline void all_apply(const int k, const F& f) {
        data[k] = mapping(f, data[k]);
        if(k < size) {
            lazy[k] = composition(f, lazy[k]);
        }
    }
    inline void push(const int k) {
        all_apply(2 * k, lazy[k]);
        all_apply(2 * k + 1, lazy[k]);
        lazy[k] = id();
    }
    inline unsigned int bit_ceil(const unsigned int n) const {
        unsigned int res = 1;
        while(res < n) res *= 2;
        return res;
    }
    inline int countr_zero(const unsigned int n) const {
        return __builtin_ctz(n);
    }
};
#line 6 "verify/aizu_online_judge/grl/range_query_on_a_tree_2.test.cpp"
struct S {
    long long value;
    long long size;
};
using F = long long;
S op(const S& a, const S& b) {
    return {a.value + b.value, a.size + b.size};
}
S e() {
    return {0, 0};
}
S mapping(const F& f, const S& x) {
    return {x.value + f * x.size, x.size};
}
F composition(const F& f, const F& g) {
    return f + g;
}
F id() {
    return 0;
}
int main(void) {
    int n;
    cin >> n;
    Graph<int> g(n);
    rep(i, 0, n) {
        int k;
        cin >> k;
        rep(j, 0, k) {
            int c;
            cin >> c;
            g.add_edge(i, c);
        }
    }
    HeavyLightDecomposition<int> hld(g);
    LazySegmentTree<S, op, e, F, mapping, composition, id> seg(n);
    rep(i, 0, n) seg.set(i, {0, 1});
    int q;
    cin >> q;
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            ll v, w;
            cin >> v >> w;
            auto add = [&](int u, int v) -> void {
                seg.apply(u, v, w);
            };
            hld.path_query(0, v, false, add);
        } else {
            int v;
            cin >> v;
            ll ans = 0;
            auto query = [&](int u, int v) -> void {
                ans += seg.prod(u, v).value;
            };
            hld.path_query(0, v, false, query);
            cout << ans << '\n';
        }
    }
}
Back to top page