ACM_Notebook_new

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

View the Project on GitHub ngthanhtrung23/ACM_Notebook_new

:heavy_check_mark: DataStructure/test/hld_vertexsetpathcomposite.test.cpp

Depends on

Code

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

#include <bits/stdc++.h>
using namespace std;

#include "../SegTree.h"
#include "../../Math/modint.h"
#include "../HeavyLight_adamant.h"

using modular = ModInt<998244353>;

// SegTree ops
struct F {
    modular a, b;
};
F op(const F& l, const F& r) {
    return F{
        l.a*r.a,
        r.a*l.b + r.b
    };
}

struct Node {
    F forward, backward;
};

Node op(Node l, Node r) {
    return Node {
        op(l.forward, r.forward),
        op(r.backward, l.backward)
    };
}

Node e() {
    return Node {
        F{1, 0},
        F{1, 0}
    };
}

#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, q; cin >> n >> q;
    vector<F> fs(n);
    REP(i,n) {
        int a, b; cin >> a >> b;
        fs[i] = {a, b};
    }

    vector<vector<int>> g(n);
    REP(i,n-1) {
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    HLD hld(g, 0);

    vector<Node> nodes;
    REP(i,n) {
        auto f = fs[hld.order[i]];
        nodes.push_back({f, f});
    }
    SegTree<Node, op, e> tree(nodes);

    while (q--) {
        int typ; cin >> typ;
        if (typ == 0) {
            int p, a, b; cin >> p >> a >> b;
            tree.set(hld.in[p], {{a, b}, {a, b}});
        } else {
            int start, end, x; cin >> start >> end >> x;

            auto segments = hld.getSegments(start, end);
            F res {1, 0};
            for (auto [u, v] : segments) {
                if (hld.in[u] <= hld.in[v]) {
                    res = op(res, tree.prod(hld.in[u], hld.in[v] + 1).forward);
                } else {
                    res = op(res, tree.prod(hld.in[v], hld.in[u] + 1).backward);
                }
            }
            cout << (res.a * modular(x) + res.b) << '\n';
        }
    }
    return 0;
}
#line 1 "DataStructure/test/hld_vertexsetpathcomposite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"

#include <bits/stdc++.h>
using namespace std;

#line 1 "DataStructure/SegTree.h"
// SegTree, copied from AtCoder library {{{
// AtCoder doc: https://atcoder.github.io/ac-library/master/document_en/segtree.html
//
// Notes:
// - Index of elements from 0 -> n-1
// - Range queries are [l, r-1]
//
// Tested:
// - (binary search) https://atcoder.jp/contests/practice2/tasks/practice2_j
// - https://oj.vnoi.info/problem/gss
// - https://oj.vnoi.info/problem/nklineup
// - (max_right & min_left for delete position queries) https://oj.vnoi.info/problem/segtree_itstr
// - https://judge.yosupo.jp/problem/point_add_range_sum
// - https://judge.yosupo.jp/problem/point_set_range_composite
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

template<
    class T,  // data type for nodes
    T (*op) (T, T),  // operator to combine 2 nodes
    T (*e)() // identity element
>
struct SegTree {
    SegTree() : SegTree(0) {}
    explicit SegTree(int n) : SegTree(vector<T> (n, e())) {}
    explicit SegTree(const vector<T>& v) : _n((int) v.size()) {
        log = ceil_pow2(_n);
        size = 1<<log;
        d = vector<T> (2*size, e());

        for (int i = 0; i < _n; i++) d[size+i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    // 0 <= p < n
    void set(int p, T x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    // 0 <= p < n
    T get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    // Get product in range [l, r-1]
    // 0 <= l <= r <= n
    // For empty segment (l == r) -> return e()
    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        T sml = e(), smr = e();
        l += size;
        r += size;
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    T all_prod() const {
        return d[1];
    }

    // Binary search on SegTree to find largest r:
    //    f(op(a[l] .. a[r-1])) = true   (assuming empty array is always true)
    //    f(op(a[l] .. a[r])) = false    (assuming op(..., a[n]), which is out of bound, is always false)
    template <bool (*f)(T)> int max_right(int l) const {
        return max_right(l, [](T x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        T sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    // Binary search on SegTree to find smallest l:
    //    f(op(a[l] .. a[r-1])) = true      (assuming empty array is always true)
    //    f(op(a[l-1] .. a[r-1])) = false   (assuming op(a[-1], ..), which is out of bound, is always false)
    template <bool (*f)(T)> int min_left(int r) const {
        return min_left(r, [](T x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        T sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

private:
    int _n, size, log;
    vector<T> d;

    void update(int k) {
        d[k] = op(d[2*k], d[2*k+1]);
    }
};
// }}}

// SegTree examples {{{
// Examples: Commonly used SegTree ops: max / min / sum
struct MaxSegTreeOp {
    static int op(int x, int y) {
        return max(x, y);
    }
    static int e() {
        return INT_MIN;
    }
};

struct MinSegTreeOp {
    static int op(int x, int y) {
        return min(x, y);
    }
    static int e() {
        return INT_MAX;
    }
};

struct SumSegTreeOp {
    static long long op(long long x, long long y) {
        return x + y;
    }
    static long long e() {
        return 0;
    }
};

// using STMax = SegTree<int, MaxSegTreeOp::op, MaxSegTreeOp::e>;
// using STMin = SegTree<int, MinSegTreeOp::op, MinSegTreeOp::e>;
// using STSum = SegTree<int, SumSegTreeOp::op, SumSegTreeOp::e>;
// }}}
#line 1 "Math/modint.h"
// ModInt {{{
template<int MD> struct ModInt {
    using ll = long long;
    int x;

    constexpr ModInt() : x(0) {}
    constexpr ModInt(ll v) { _set(v % MD + MD); }
    constexpr static int mod() { return MD; }
    constexpr explicit operator bool() const { return x != 0; }

    constexpr ModInt operator + (const ModInt& a) const {
        return ModInt()._set((ll) x + a.x);
    }
    constexpr ModInt operator - (const ModInt& a) const {
        return ModInt()._set((ll) x - a.x + MD);
    }
    constexpr ModInt operator * (const ModInt& a) const {
        return ModInt()._set((ll) x * a.x % MD);
    }
    constexpr ModInt operator / (const ModInt& a) const {
        return ModInt()._set((ll) x * a.inv().x % MD);
    }
    constexpr ModInt operator - () const {
        return ModInt()._set(MD - x);
    }

    constexpr ModInt& operator += (const ModInt& a) { return *this = *this + a; }
    constexpr ModInt& operator -= (const ModInt& a) { return *this = *this - a; }
    constexpr ModInt& operator *= (const ModInt& a) { return *this = *this * a; }
    constexpr ModInt& operator /= (const ModInt& a) { return *this = *this / a; }

    friend constexpr ModInt operator + (ll a, const ModInt& b) {
        return ModInt()._set(a % MD + b.x);
    }
    friend constexpr ModInt operator - (ll a, const ModInt& b) {
        return ModInt()._set(a % MD - b.x + MD);
    }
    friend constexpr ModInt operator * (ll a, const ModInt& b) {
        return ModInt()._set(a % MD * b.x % MD);
    }
    friend constexpr ModInt operator / (ll a, const ModInt& b) {
        return ModInt()._set(a % MD * b.inv().x % MD);
    }

    constexpr bool operator == (const ModInt& a) const { return x == a.x; }
    constexpr bool operator != (const ModInt& a) const { return x != a.x; }

    friend std::istream& operator >> (std::istream& is, ModInt& other) {
        ll val; is >> val;
        other = ModInt(val);
        return is;
    }
    constexpr friend std::ostream& operator << (std::ostream& os, const ModInt& other) {
        return os << other.x;
    }

    constexpr ModInt pow(ll k) const {
        ModInt ans = 1, tmp = x;
        while (k) {
            if (k & 1) ans *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return ans;
    }

    constexpr ModInt inv() const {
        if (x < 1000111) {
            _precalc(1000111);
            return invs[x];
        }
        int a = x, b = MD, ax = 1, bx = 0;
        while (b) {
            int q = a/b, t = a%b;
            a = b; b = t;
            t = ax - bx*q;
            ax = bx; bx = t;
        }
        assert(a == 1);
        if (ax < 0) ax += MD;
        return ax;
    }

    static std::vector<ModInt> factorials, inv_factorials, invs;
    constexpr static void _precalc(int n) {
        if (factorials.empty()) {
            factorials = {1};
            inv_factorials = {1};
            invs = {0};
        }
        if (n > MD) n = MD;
        int old_sz = factorials.size();
        if (n <= old_sz) return;

        factorials.resize(n);
        inv_factorials.resize(n);
        invs.resize(n);

        for (int i = old_sz; i < n; ++i) factorials[i] = factorials[i-1] * i;
        inv_factorials[n-1] = factorials.back().pow(MD - 2);
        for (int i = n - 2; i >= old_sz; --i) inv_factorials[i] = inv_factorials[i+1] * (i+1);
        for (int i = n-1; i >= old_sz; --i) invs[i] = inv_factorials[i] * factorials[i-1];
    }

    static int get_primitive_root() {
        static int primitive_root = 0;
        if (!primitive_root) {
            primitive_root = [&]() {
                std::set<int> fac;
                int v = MD - 1;
                for (ll i = 2; i * i <= v; i++)
                    while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < MD; g++) {
                    bool ok = true;
                    for (auto i : fac)
                        if (ModInt(g).pow((MD - 1) / i) == 1) {
                            ok = false;
                            break;
                        }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root;
    }

    static ModInt C(int n, int k) {
        _precalc(n + 1);
        return factorials[n] * inv_factorials[k] * inv_factorials[n-k];
    }
    
private:
    // Internal, DO NOT USE.
    // val must be in [0, 2*MD)
    constexpr inline __attribute__((always_inline)) ModInt& _set(ll v) {
        x = v >= MD ? v - MD : v;
        return *this;
    }
};
template <int MD> std::vector<ModInt<MD>> ModInt<MD>::factorials = {1};
template <int MD> std::vector<ModInt<MD>> ModInt<MD>::inv_factorials = {1};
template <int MD> std::vector<ModInt<MD>> ModInt<MD>::invs = {0};
// }}}
#line 1 "DataStructure/HeavyLight_adamant.h"
// Index from 0
// Best used with SegTree.h
//
// Usage:
// HLD hld(g, root);
// // build segment tree. Note that we must use hld.order[i]
// vector<T> nodes;
// for (int i = 0; i < n; i++)
//   nodes.push_back(initial_value[hld.order[i]])
// SegTree<S, op, e> st(nodes);
//
// // Update single vertex
// st.set(hld.in[u], new_value)
//
// // Update path
// hld.apply_path(from, to, is_edge, [&] (int l, int r) {
//   st.apply(l, r+1, F);
// });
//
// // Query path
// hld.prod_path_commutative<S, op, e> (from, to, is_edge, [&] (int l, int r) {
//   return st.prod(l, r+1);
// });
//
// Tested:
// - (vertex, path) https://judge.yosupo.jp/problem/vertex_add_path_sum
// - (vertex, path, non-commutative) https://judge.yosupo.jp/problem/vertex_set_path_composite
// - (vertex, subtree) https://judge.yosupo.jp/problem/vertex_add_subtree_sum
// - (vertex, path, non-commutative, 1-index) https://oj.vnoi.info/problem/icpc21_mt_l
// - (vertex, path) https://oj.vnoi.info/problem/qtree3
//
// - (edge, path) https://oj.vnoi.info/problem/qtreex
// - (edge, path) https://oj.vnoi.info/problem/lubenica
// - (edge, path) https://oj.vnoi.info/problem/pwalk
// - (edge, path, lazy) https://oj.vnoi.info/problem/kbuild
// - (edge, path, lazy) https://oj.vnoi.info/problem/onbridge
//
// - (lca) https://oj.vnoi.info/problem/fselect
// - (kth_parent) https://cses.fi/problemset/task/1687
// HeavyLight {{{
struct HLD {
    HLD(const vector<vector<int>>& _g, int root)
            : n(_g.size()), g(_g),
            parent(n), depth(n), sz(n),
            dfs_number(0), nxt(n), in(n), out(n), order(n)
    {
        assert(0 <= root && root < n);

        // init parent, depth, sz
        // also move most heavy child of u to g[u][0]
        depth[root] = 0;
        dfs_sz(root, -1);

        // init nxt, in, out
        nxt[root] = root;
        dfs_hld(root);
    }

    int lca(int u, int v) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        while (true) {
            if (in[u] > in[v]) swap(u, v); // in[u] <= in[v]
            if (nxt[u] == nxt[v]) return u;
            v = parent[nxt[v]];
        }
    }

    // return k-th parent
    // if no such parent -> return -1
    int kth_parent(int u, int k) const {
        assert(0 <= u && u < n);
        if (depth[u] < k) return -1;

        while (true) {
            int v = nxt[u];
            if (in[u] - k >= in[v]) return order[in[u] - k];
            k -= in[u] - in[v] + 1;
            u = parent[v];
        }
    }

    // return k-th vertex on path from u -> v (0 <= k)
    // if k > distance -> return -1
    int kth_vertex_on_path(int u, int v, int k) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);

        int l = lca(u, v);
        int ul = depth[u] - depth[l];
        if (k <= ul) return kth_parent(u, k);
        k -= ul;
        int vl = depth[v] - depth[l];
        if (k <= vl) return kth_parent(v, vl - k);
        return -1;
    }

    int dist(int u, int v) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        int l = lca(u, v);
        return depth[u] + depth[v] - 2*depth[l];
    }

    // apply f on vertices on path [u, v]
    // edge = true -> apply on edge
    //
    // f(l, r) should update segment tree [l, r] INCLUSIVE
    void apply_path(int u, int v, bool edge, const function<void(int, int)> &f) {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        if (u == v && edge) return;

        while (true) {
            if (in[u] > in[v]) swap(u, v); // in[u] <= in[v]
            if (nxt[u] == nxt[v]) break;
            f(in[nxt[v]], in[v]);
            v = parent[nxt[v]];
        }
        if (u == v && edge) return;
        f(in[u] + edge, in[v]);
    }

    // get prod of path u -> v
    // edge = true -> get on edges
    //
    // f(l, r) should query segment tree [l, r] INCLUSIVE
    // f must be commutative. For non-commutative, use getSegments below
    template<class S, S (*op) (S, S), S (*e)()>
    S prod_path_commutative(
            int u, int v, bool edge,
            const function<S(int, int)>& f) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        if (u == v && edge) {
            return e();
        }
        S su = e(), sv = e();
        while (true) {
            if (in[u] > in[v]) { swap(u, v); swap(su, sv); }
            if (nxt[u] == nxt[v]) break;
            sv = op(sv, f(in[nxt[v]], in[v]));
            v = parent[nxt[v]];
        }
        if (u == v && edge) {
            return op(su, sv);
        } else {
            return op(su, op(sv, f(in[u] + edge, in[v])));
        }
    }

    // f(l, r) modify seg_tree [l, r] INCLUSIVE
    void apply_subtree(int u, bool edge, const function<void(int, int)>& f) {
        assert(0 <= u && u < n);
        f(in[u] + edge, out[u] - 1);
    }

    // f(l, r) queries seg_tree [l, r] INCLUSIVE
    template<class S>
    S prod_subtree_commutative(int u, bool edge, const function<S(S, S)>& f) {
        assert(0 <= u && u < n);
        return f(in[u] + edge, out[u] - 1);
    }

    // Useful when functions are non-commutative
    // Return all segments on path from u -> v
    // For this problem, the order (u -> v is different from v -> u)
    vector< pair<int,int> > getSegments(int u, int v) const {
        assert(0 <= u && u < n);
        assert(0 <= v && v < n);
        vector< pair<int,int> > upFromU, upFromV;

        int fu = nxt[u], fv = nxt[v];
        while (fu != fv) {  // u and v are on different chains
            if (depth[fu] >= depth[fv]) { // move u up
                upFromU.push_back({u, fu});
                u = parent[fu];
                fu = nxt[u];
            } else { // move v up
                upFromV.push_back({fv, v});
                v = parent[fv];
                fv = nxt[v];
            }
        }
        upFromU.push_back({u, v});
        reverse(upFromV.begin(), upFromV.end());
        upFromU.insert(upFromU.end(), upFromV.begin(), upFromV.end());
        return upFromU;
    }

    // return true if u is ancestor
    bool isAncestor(int u, int v) const {
        return in[u] <= in[v] && out[v] <= out[u];
    }

// private:
    int n;
    vector<vector<int>> g;
    vector<int> parent;   // par[u] = parent of u. par[root] = -1
    vector<int> depth;    // depth[u] = distance from root -> u
    vector<int> sz;       // sz[u] = size of subtree rooted at u
    int dfs_number;
    vector<int> nxt;      // nxt[u] = vertex on heavy path of u, nearest to root
    vector<int> in, out;  // subtree(u) is in range [in[u], out[u]-1]
    vector<int> order;    // euler tour

    void dfs_sz(int u, int fu) {
        parent[u] = fu;
        sz[u] = 1;
        // remove parent from adjacency list
        auto it = std::find(g[u].begin(), g[u].end(), fu);
        if (it != g[u].end()) g[u].erase(it);

        for (int& v : g[u]) {
            depth[v] = depth[u] + 1;
            dfs_sz(v, u);

            sz[u] += sz[v];
            if (sz[v] > sz[g[u][0]]) swap(v, g[u][0]);
        }
    }

    void dfs_hld(int u) {
        order[dfs_number] = u;
        in[u] = dfs_number++;

        for (int v : g[u]) {
            nxt[v] = (v == g[u][0] ? nxt[u] : v);
            dfs_hld(v);
        }
        out[u] = dfs_number;
    }
};
// }}}
#line 9 "DataStructure/test/hld_vertexsetpathcomposite.test.cpp"

using modular = ModInt<998244353>;

// SegTree ops
struct F {
    modular a, b;
};
F op(const F& l, const F& r) {
    return F{
        l.a*r.a,
        r.a*l.b + r.b
    };
}

struct Node {
    F forward, backward;
};

Node op(Node l, Node r) {
    return Node {
        op(l.forward, r.forward),
        op(r.backward, l.backward)
    };
}

Node e() {
    return Node {
        F{1, 0},
        F{1, 0}
    };
}

#define REP(i, a) for (int i = 0, _##i = (a); i < _##i; ++i)

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, q; cin >> n >> q;
    vector<F> fs(n);
    REP(i,n) {
        int a, b; cin >> a >> b;
        fs[i] = {a, b};
    }

    vector<vector<int>> g(n);
    REP(i,n-1) {
        int u, v; cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    HLD hld(g, 0);

    vector<Node> nodes;
    REP(i,n) {
        auto f = fs[hld.order[i]];
        nodes.push_back({f, f});
    }
    SegTree<Node, op, e> tree(nodes);

    while (q--) {
        int typ; cin >> typ;
        if (typ == 0) {
            int p, a, b; cin >> p >> a >> b;
            tree.set(hld.in[p], {{a, b}, {a, b}});
        } else {
            int start, end, x; cin >> start >> end >> x;

            auto segments = hld.getSegments(start, end);
            F res {1, 0};
            for (auto [u, v] : segments) {
                if (hld.in[u] <= hld.in[v]) {
                    res = op(res, tree.prod(hld.in[u], hld.in[v] + 1).forward);
                } else {
                    res = op(res, tree.prod(hld.in[v], hld.in[u] + 1).backward);
                }
            }
            cout << (res.a * modular(x) + res.b) << '\n';
        }
    }
    return 0;
}
Back to top page