rcpl

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

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: verify/graph/heavy_light_decomposition_1.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_E"

#include <iostream>

#include "graph/read_graph.hpp"
#include "graph/heavy_light_decomposition.hpp"
#include "data_structure/lazy_segment_tree.hpp"
#include "algebra/monoid_s_f/monoid_sum_size_add.hpp"

int main() {
    int N;
    std::cin >> N;
    Graph<int> g(N, false);

    for (int i = 0; i < N; i++) {
        int K;
        std::cin >> K;
        for (int j = 0; j < K; j++) {
            int c;
            std::cin >> c;
            g.add_edge(i, c, 1);
        }
    }
    const int root = N / 2;  // verify のために適当に決める
    HeavyLightDecomposition hld(g, root);
    std::vector<std::pair<long long, int>> segi(N - 1, {0, 1});
    LazySegmentTree<MonoidSumSizeAdd<long long>> seg(segi);

    int Q;
    std::cin >> Q;
    for (int i = 0; i < Q; i++) {
        int type;
        std::cin >> type;
        if (type == 0) {
            int v, w;
            std::cin >> v >> w;
            auto intervals = hld.path_query(0, v, true);
            for (auto&& [l, r] : intervals) {
                seg.apply(l, r, w);
            }
        } else {
            int v;
            std::cin >> v;
            auto intervals = hld.path_query(0, v, true);
            auto res = MonoidSumSizeAdd<long long>::MS::e();
            for (auto&& [l, r] : intervals) {
                res = MonoidSumSizeAdd<long long>::MS::op(res, seg.prod(l, r));
            }
            std::cout << res.first << '\n';
        }
    }

    return 0;
}
#line 1 "verify/graph/heavy_light_decomposition_1.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_E"

#include <iostream>

#line 2 "graph/read_graph.hpp"

#line 2 "graph/graph_template.hpp"

#include <vector>
#include <cassert>

template <class T> struct Edge {
    int from, to;
    T cost;
    int id;

    Edge() = default;
    Edge(const int from, const int to, const T cost = T(1), const int id = -1) : from(from), to(to), cost(cost), id(id) {}

    friend bool operator<(const Edge<T>& a, const Edge<T>& b) { return a.cost < b.cost; }

    friend std::ostream& operator<<(std::ostream& os, const Edge<T>& e) {
        // output format: {id: cost(from, to) = cost}
        return os << "{" << e.id << ": cost(" << e.from << ", " << e.to << ") = " << e.cost << "}";
    }
};
template <class T> using Edges = std::vector<Edge<T>>;

template <class T> struct Graph {
    struct EdgeIterators {
       public:
        using Iterator = typename std::vector<Edge<T>>::iterator;
        EdgeIterators() = default;
        EdgeIterators(const Iterator& begit, const Iterator& endit) : begit(begit), endit(endit) {}
        Iterator begin() const { return begit; }
        Iterator end() const { return endit; }
        size_t size() const { return std::distance(begit, endit); }
        Edge<T>& operator[](int i) const { return begit[i]; }

       private:
        Iterator begit, endit;
    };

    int n, m;
    bool is_build, is_directed;
    std::vector<Edge<T>> edges;

    // CSR (Compressed Row Storage) 形式用
    std::vector<int> start;
    std::vector<Edge<T>> csr_edges;

    Graph() = default;
    Graph(const int n, const bool directed = false) : n(n), m(0), is_build(false), is_directed(directed), start(n + 1, 0) {}

    // 辺を追加し, その辺が何番目に追加されたかを返す
    int add_edge(const int from, const int to, const T cost = T(1), int id = -1) {
        assert(!is_build);
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        if (id == -1) id = m;
        edges.emplace_back(from, to, cost, id);
        return m++;
    }

    // CSR 形式でグラフを構築
    void build() {
        assert(!is_build);
        for (auto&& e : edges) {
            start[e.from + 1]++;
            if (!is_directed) start[e.to + 1]++;
        }
        for (int v = 0; v < n; v++) start[v + 1] += start[v];
        auto counter = start;
        csr_edges.resize(start.back() + 1);
        for (auto&& e : edges) {
            csr_edges[counter[e.from]++] = e;
            if (!is_directed) csr_edges[counter[e.to]++] = Edge(e.to, e.from, e.cost, e.id);
        }
        is_build = true;
    }

    EdgeIterators operator[](int i) {
        if (!is_build) build();
        return EdgeIterators(csr_edges.begin() + start[i], csr_edges.begin() + start[i + 1]);
    }

    size_t size() const { return (size_t)(n); }

    friend std::ostream& operator<<(std::ostream& os, Graph<T>& g) {
        os << "[";
        for (int i = 0; i < (int)(g.size()); i++) {
            os << "[";
            for (int j = 0; j < (int)(g[i].size()); j++) {
                os << g[i][j];
                if (j + 1 != (int)(g[i].size())) os << ", ";
            }
            os << "]";
            if (i + 1 != (int)(g.size())) os << ", ";
        }
        return os << "]";
    }
};
#line 4 "graph/read_graph.hpp"

template <class T> Graph<T> read_graph(const int n, const int m, const bool weight = false, const bool directed = false, const int offset = 1) {
    Graph<T> g(n, directed);
    for (int i = 0; i < m; i++) {
        int a, b;
        std::cin >> a >> b;
        a -= offset, b -= offset;
        T c = 1;
        if (weight) std::cin >> c;
        g.add_edge(a, b, c);
    }
    g.build();
    return g;
}

template <class T> Graph<T> read_parent(const int n, const bool weight = false, const bool directed = false, const int offset = 1) {
    Graph<T> g(n, directed);
    for (int i = 1; i < n; i++) {
        int p;
        std::cin >> p;
        p -= offset;
        T c = 1;
        if (weight) std::cin >> c;
        g.add_edge(p, i, c);
    }
    g.build();
    return g;
}
#line 2 "graph/heavy_light_decomposition.hpp"

#line 4 "graph/heavy_light_decomposition.hpp"

// Heavy-Light Decomposition
template <class T> struct HeavyLightDecomposition {
    int n;
    // dfs_size
    std::vector<int> subsize;  // subsize[v] = v を根とする部分木のサイズ
    std::vector<int> depth;    // depth[v] = v の深さ
    std::vector<int> parent;   // parent[v] = v の親の頂点番号

    // dfs_hld
    std::vector<int> vertices;  // Heavy-Edge から優先的に DFS したときの頂点の番号を並べたもの, n 要素
    std::vector<int> edges;     // Heavy-Edge から優先的に DFS したときの辺の番号を並べたもの, n - 1 要素
    std::vector<int> pathtop;   // pathtop[v] = v を含むパス上の祖先
    std::vector<int> subbegin;  // subbegin[v] = v を根とする部分木の頂点列の開始位置, vertices における v の登場位置
    std::vector<int> subend;    // subend[v] = v を根とする部分木の頂点列の終わり
    std::vector<int> eindex;    // eindex[e] = edges における e の登場位置

    // Graph<T> の辺の並べ替えを行うことに注意
    HeavyLightDecomposition(Graph<T>& g, const int root = 0) : n((int)(g.size())), subsize(n, 1), depth(n, 0), parent(n, -1), pathtop(n, -1), subbegin(n, -1), subend(n, -1), eindex(n - 1, -1) {
        // 部分木のサイズを計算
        auto dfs_size = [&](auto f, int cur, int par) -> void {
            parent[cur] = par;
            // 親方向への辺を末尾に移動
            for (int i = 0; i < (int)(g[cur].size()); i++) {
                if (g[cur][i].to == par) {
                    std::swap(g[cur][i], g[cur][(int)(g[cur].size()) - 1]);
                    break;
                }
            }
            // 部分木のサイズが最大のものを先頭に移動
            for (auto&& e : g[cur]) {
                if (e.to == par) continue;
                depth[e.to] = depth[cur] + 1;
                f(f, e.to, cur);
                subsize[cur] += subsize[e.to];
                if (subsize[e.to] > subsize[g[cur][0].to]) {
                    std::swap(e, g[cur][0]);
                }
            }
        };
        dfs_size(dfs_size, root, -1);

        // 頂点を並べる
        vertices.reserve(n);
        edges.reserve(n - 1);
        auto dfs_hld = [&](auto f, int cur, int par, int top) -> void {
            pathtop[cur] = top;
            subbegin[cur] = (int)(vertices.size());
            vertices.push_back(cur);

            for (auto&& e : g[cur]) {
                if (e.to == par) continue;
                eindex[e.id] = (int)(edges.size());
                edges.push_back(e.id);
                // top は heavy-edge に対してのみ引き継がれる
                f(f, e.to, cur, (e.to == g[cur][0].to ? top : e.to));
            }
            subend[cur] = (int)(vertices.size());
        };
        dfs_hld(dfs_hld, root, -1, root);
    }

    int lca(int u, int v) {
        // 同じパスまで上がる
        while (pathtop[u] != pathtop[v]) {
            if (subbegin[u] > subbegin[v]) {
                u = parent[pathtop[u]];
            } else {
                v = parent[pathtop[v]];
            }
        }
        if (subbegin[u] > subbegin[v]) std::swap(u, v);
        return u;
    }

    // u - v パスに対応する区間
    // is_edges = true なら edges に対応する区間, false なら vertices に対応する区間
    std::vector<std::pair<int, int>> path_query(int u, int v, const bool is_edges) {
        std::vector<std::pair<int, int>> res;
        while (pathtop[u] != pathtop[v]) {
            if (subbegin[u] > subbegin[v]) std::swap(u, v);
            // subbegin[u] <= subbegin[v]
            if (is_edges) {
                // edges に対応する区間なので pathtop[u] から parent[pathtop[u]] に行く辺も区間に加える
                res.emplace_back(subbegin[pathtop[v]] - 1, subbegin[v]);
            } else {
                res.emplace_back(subbegin[pathtop[v]], subbegin[v] + 1);
            }
            v = parent[pathtop[v]];
        }
        if (subbegin[u] > subbegin[v]) std::swap(u, v);
        if (is_edges) {
            res.emplace_back(subbegin[u], subbegin[v]);
        } else {
            res.emplace_back(subbegin[u], subbegin[v] + 1);
        }
        return res;
    }

    // u を根とする部分木に対応する区間
    // is_edges = true なら edges に対応する区間, false なら vertices に対応する区間
    std::pair<int, int> subtree_query(int u, const bool is_edges) {
        if (is_edges) {
            return {subbegin[u], subend[u] - 1};
        } else {
            return {subbegin[u], subend[u]};
        }
    }
};
#line 4 "data_structure/lazy_segment_tree.hpp"
template <class MSF> struct LazySegmentTree {
   public:
    using S = typename MSF::S;
    using F = typename MSF::F;
    using MS = typename MSF::MS;
    using MF = typename MSF::MF;
    LazySegmentTree() : LazySegmentTree(0) {}
    LazySegmentTree(int n) : LazySegmentTree(std::vector<S>(n, MS::e())) {}
    LazySegmentTree(const std::vector<S>& v) : n((int)(v.size())) {
        log = 0;
        while ((1U << log) < (unsigned int)(n)) log++;
        size = 1 << log;
        d = std::vector<S>(size << 1, MS::e());
        lz = std::vector<F>(size, MF::id());
        for (int i = 0; i < n; i++) d[i + size] = 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);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void chset(int p, const S& x) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = MS::op(d[p], x);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S operator[](int p) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S get(int p) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        if (l == r) return MS::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 = MS::e(), smr = MS::e();
        while (l < r) {
            if (l & 1) sml = MS::op(sml, d[l++]);
            if (r & 1) smr = MS::op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return MS::op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, const F& f) {
        assert(0 <= p and p < n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = MSF::mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    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 <class G> int max_right(int l, G& g) {
        assert(0 <= l and l <= n);
        assert(g(MS::e()));
        if (l == n) return n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = MS::e();
        do {
            while ((l & 1) == 0) l >>= 1;
            if (!g(MS::op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l <<= 1;
                    if (g(MS::op(sm, d[l]))) {
                        sm = MS::op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = MS::op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return n;
    }

    template <class G> int min_left(int r, G& g) {
        assert(0 <= r and r <= n);
        assert(g(MS::e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = MS::e();
        do {
            r--;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!g(MS::op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (r << 1) | 1;
                    if (g(MS::op(d[r], sm))) {
                        sm = MS::op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = MS::op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

    std::vector<S> make_vector() {
        std::vector<S> vec(n);
        for (int i = 0; i < n; i++) vec[i] = get(i);
        return vec;
    }

   private:
    int n, log, size;
    std::vector<S> d;
    std::vector<F> lz;
    inline void update(int k) { d[k] = MS::op(d[k << 1], d[(k << 1) | 1]); }
    void all_apply(int k, const F& f) {
        d[k] = MSF::mapping(f, d[k]);
        if (k < size) lz[k] = MF::composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(k << 1, lz[k]);
        all_apply((k << 1) | 1, lz[k]);
        lz[k] = MF::id();
    }
};
#line 2 "algebra/monoid_s/monoid_sum_size.hpp"
// MS
template <class T> struct MonoidSumSize {
    using S = std::pair<T, int>;
    static constexpr S op(S a, S b) { return {a.first + b.first, a.second + b.second}; }
    static constexpr S e() { return {T(0), 0}; }
};
#line 2 "algebra/monoid_f/monoid_add.hpp"
// MF
template <class T> struct MonoidAdd {
    using F = T;
    static constexpr F composition(F f, F g) { return f + g; }
    static constexpr F id() { return T(0); }
};
#line 4 "algebra/monoid_s_f/monoid_sum_size_add.hpp"
// MSF
template <class T> struct MonoidSumSizeAdd {
    using MS = MonoidSumSize<T>;
    using MF = MonoidAdd<T>;
    using S = typename MS::S;
    using F = typename MF::F;
    static constexpr S mapping(F f, S x) { return {x.first + f * x.second, x.second}; }
};
#line 9 "verify/graph/heavy_light_decomposition_1.test.cpp"

int main() {
    int N;
    std::cin >> N;
    Graph<int> g(N, false);

    for (int i = 0; i < N; i++) {
        int K;
        std::cin >> K;
        for (int j = 0; j < K; j++) {
            int c;
            std::cin >> c;
            g.add_edge(i, c, 1);
        }
    }
    const int root = N / 2;  // verify のために適当に決める
    HeavyLightDecomposition hld(g, root);
    std::vector<std::pair<long long, int>> segi(N - 1, {0, 1});
    LazySegmentTree<MonoidSumSizeAdd<long long>> seg(segi);

    int Q;
    std::cin >> Q;
    for (int i = 0; i < Q; i++) {
        int type;
        std::cin >> type;
        if (type == 0) {
            int v, w;
            std::cin >> v >> w;
            auto intervals = hld.path_query(0, v, true);
            for (auto&& [l, r] : intervals) {
                seg.apply(l, r, w);
            }
        } else {
            int v;
            std::cin >> v;
            auto intervals = hld.path_query(0, v, true);
            auto res = MonoidSumSizeAdd<long long>::MS::e();
            for (auto&& [l, r] : intervals) {
                res = MonoidSumSizeAdd<long long>::MS::op(res, seg.prod(l, r));
            }
            std::cout << res.first << '\n';
        }
    }

    return 0;
}
Back to top page