kyopro_library

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

View the Project on GitHub dyktr06/kyopro_library

:heavy_check_mark: test/library_checker/tree/vertex_set_path_composite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include <bits/stdc++.h>
using namespace std;

#include "../../../lib/data_structure/segment_tree.hpp"
#include "../../../lib/graph/heavy_light_decomposition.hpp"
#include "../../../lib/math/modint.hpp"

using mint = ModInt<998244353>;

int main(){
    int n, q; cin >> n >> q;
    using T = pair<mint, mint>;
    vector<T> f(n);
    for(int i = 0; i < n; i++){
        cin >> f[i].first >> f[i].second;
    }
    HeavyLightDecomposition hl(n);
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        hl.add_edge(u, v);
    }
    hl.build();
    
    auto op = [](T x1, T x2) -> T { 
        return {x1.first * x2.first, (x2.first * x1.second + x2.second)};
    };
    T ex = {1, 0};
    SegTree<T> seg(n, op, ex), seg2(n, op, ex);
    for(int i = 0; i < n; i++){
        seg.update(hl.get(i), {f[i].first, f[i].second});
        seg2.update(n - hl.get(i) - 1, {f[i].first, f[i].second});
    }
    mint ans = 0;
    T g = ex;
    auto query = [&](int l, int r){
        g = op(g, seg.query(l, r));
    };
    auto query2 = [&](int l, int r){
        g = op(g, seg2.query(l, r));
    };
    while(q--){
        int t; cin >> t;
        if(t == 0){
            int p, c, d; cin >> p >> c >> d;
            seg.update(hl.get(p), {c, d});
            seg2.update(n - hl.get(p) - 1, {c, d});
        }else{
            int u, v, x; cin >> u >> v >> x;
            g = ex;
            hl.path_noncommutative_query(u, v, query, query2);
            ans = g.first * x + g.second;
            cout << ans << "\n";
        }
    }
}
#line 1 "test/library_checker/tree/vertex_set_path_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include <bits/stdc++.h>
using namespace std;

#line 2 "lib/data_structure/segment_tree.hpp"

/**
 * @brief Segment Tree
 * @docs docs/data_structure/segment_tree.md
 */

template <typename T>
struct SegTree{
    using FX = function<T(T, T)>; // T•T -> T となる関数の型
    int n;
    const FX fx;
    const T ex;
    vector<T> dat;

    SegTree(int n_, const FX &fx_, const T &ex_) : n(), fx(fx_), ex(ex_){
        int x = 1;
        while(n_ > x){
            x *= 2;
        }
        n = x;
        dat.resize(n * 2);
        for(int i = 0; i < n * 2; ++i){
            dat[i] = ex;
        }
    }
    SegTree(vector<T> &v, const FX &fx_, const T &ex_) : n(), fx(fx_), ex(ex_){
        int x = 1;
        while((int) v.size() > x){
            x *= 2;
        }
        n = x;
        dat.resize(n * 2);
        for(int i = 0; i < n; ++i){
            set(i, (i < (int) v.size() ? v[i] : ex));
        }
        build();
    }

    void set(int i, const T &x){ dat[i + n] = x; }

    void build(){
        for(int k = n - 1; k >= 1; k--) dat[k] = fx(dat[k * 2], dat[k * 2 + 1]);
    }

    T get(int i) const {
        return dat[i + n];
    }

    void update(int i, const T &x){
        i += n;
        dat[i] = x;
        while(i > 0){
            i >>= 1;  // parent
            dat[i] = fx(dat[i * 2], dat[i * 2 + 1]);
        }
    }

    T query(int a, int b){
        T vl = ex;
        T vr = ex;
        int l = a + n;
        int r = b + n;
        while(l < r){
            if(l & 1) vl = fx(vl, dat[l++]);
            if(r & 1) vr = fx(dat[--r], vr);
            l >>= 1;
            r >>= 1;
        }
        return fx(vl, vr);
    }
};
#line 2 "lib/graph/heavy_light_decomposition.hpp"

/**
 * @brief Heavy Light Decomposition (重軽分解)
 * @docs docs/graph/heavy_light_decomposition.md
 */

class HeavyLightDecomposition{
protected:
    int V;
    vector<vector<int>> G;
    vector<int> stsize, parent, pathtop, depth, in, reverse_in, out;
    int root;

private:
    // Subtree Size
    void buildStsize(int curr, int prev){
        stsize[curr] = 1, parent[curr] = prev;
        for(int &v : G[curr]){
            if(v == prev){
                if(v == G[curr].back()) break;
                else swap(v, G[curr].back());
            }
            buildStsize(v, curr);
            stsize[curr] += stsize[v];
            if(stsize[v] > stsize[G[curr][0]]){
                swap(v, G[curr][0]);
            }
        }
    }

    void buildPath(int curr, int prev, int &t){
        in[curr] = t++;
        reverse_in[in[curr]] = curr;
        for(int v : G[curr]){
            if(v == prev) continue;

            if(v == G[curr][0]){
                pathtop[v] = pathtop[curr];
            } else{
                pathtop[v] = v;
            }
            depth[v] = depth[curr] + 1;
            buildPath(v, curr, t);
        }
        out[curr] = t;
    }

public:
    HeavyLightDecomposition(int node_size) : V(node_size), G(V), stsize(V, 0), parent(V, -1),
        pathtop(V, -1), depth(V, 0), in(V, -1), reverse_in(V, -1), out(V, -1){}

    void add_edge(int u, int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }

    void build(int _root = 0){
        root = _root;
        int t = 0;
        buildStsize(root, -1);
        pathtop[root] = root;
        buildPath(root, -1, t);
    }

    inline int get(int a){
        return in[a];
    }

    int la(int a, int k) {
        while(true){
            int u = pathtop[a];
            if(in[a] - k >= in[u]) return reverse_in[in[a] - k];
            k -= in[a] - in[u] + 1;
            a = parent[u];
        }
    }

    int lca(int a, int b){
        int pa = pathtop[a], pb = pathtop[b];
        while(pathtop[a] != pathtop[b]){
            if(in[pa] > in[pb]){
                a = parent[pa], pa = pathtop[a];
            } else{
                b = parent[pb], pb = pathtop[b];
            }
        }
        if(in[a] > in[b]) swap(a, b);
        return a;
    }

    int dist(int a, int b){ return depth[a] + depth[b] - 2 * depth[lca(a, b)]; }

    int jump(int from, int to, int k) {
        if(!k) return from;
        int l = lca(from, to);
        int d = dist(from, to);
        if(d < k) return -1;
        if(depth[from] - depth[l] >= k) return la(from, k);
        k -= depth[from] - depth[l];
        return la(to, depth[to] - depth[l] - k);
    }

    void subtree_query(int a, const function<void(int, int)> &func){
        func(in[a], out[a]);
    }

    void path_query(int a, int b, const function<void(int, int)> &func, bool include_root = true, bool reverse_path = false){
        vector<pair<int, int>> path;
        int pa = pathtop[a], pb = pathtop[b];
        while(pathtop[a] != pathtop[b]){
            if(in[pa] > in[pb]){
                path.emplace_back(in[pa], in[a] + 1);
                a = parent[pa], pa = pathtop[a];
            } else{
                path.emplace_back(in[pb], in[b] + 1);
                b = parent[pb], pb = pathtop[b];
            }
        }
        if(in[a] > in[b]) swap(a, b);

        if(include_root) path.emplace_back(in[a], in[b] + 1);
        else path.emplace_back(in[a] + 1, in[b] + 1);

        if(!reverse_path) reverse(path.begin(), path.end());
        else for(auto &p : path) p = make_pair(V - p.second, V - p.first);

        for(auto [u, v] : path){
            func(u, v);
        }
    }

    void path_noncommutative_query(int a, int b, const function<void(int, int)> &func, const function<void(int, int)> &func2){
        int l = lca(a, b);
        path_query(a, l, func2, false, true);
        path_query(l, b, func, true, false);
    }
};
#line 2 "lib/math/modint.hpp"

#line 5 "lib/math/modint.hpp"

/**
 * @brief ModInt
 * @docs docs/math/modint.md
 */

template <long long Modulus>
struct ModInt{
    long long val;
    static constexpr int mod() { return Modulus; }
    constexpr ModInt(const long long _val = 0) noexcept : val(_val) {
        normalize();
    }
    void normalize(){
        val = (val % Modulus + Modulus) % Modulus;
    }
    inline ModInt &operator+=(const ModInt &rhs) noexcept {
        if(val += rhs.val, val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt &operator-=(const ModInt &rhs) noexcept {
        if(val -= rhs.val, val < 0) val += Modulus;
        return *this;
    }
    inline ModInt &operator*=(const ModInt &rhs) noexcept {
        val = val * rhs.val % Modulus;
        return *this;
    }
    inline ModInt &operator/=(const ModInt &rhs) noexcept {
        val = val * inv(rhs.val).val % Modulus;
        return *this;
    }
    inline ModInt &operator++() noexcept {
        if(++val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt operator++(int) noexcept {
        ModInt t = val;
        if(++val >= Modulus) val -= Modulus;
        return t;
    }
    inline ModInt &operator--() noexcept {
        if(--val < 0) val += Modulus;
        return *this;
    }
    inline ModInt operator--(int) noexcept {
        ModInt t = val;
        if(--val < 0) val += Modulus;
        return t;
    }
    inline ModInt operator-() const noexcept { return (Modulus - val) % Modulus; }
    inline ModInt inv(void) const { return inv(val); }
    ModInt pow(long long n) const {
        assert(0 <= n);
        ModInt x = *this, r = 1;
        while(n){
            if(n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    ModInt inv(const long long n) const {
        long long a = n, b = Modulus, u = 1, v = 0;
        while(b){
            long long t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        u %= Modulus;
        if(u < 0) u += Modulus;
        return u;
    }
    friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
    friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
    friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
    friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
    friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
    friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
    friend inline std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
        is >> x.val;
        x.normalize();
        return is;
    }
    friend inline std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 8 "test/library_checker/tree/vertex_set_path_composite.test.cpp"

using mint = ModInt<998244353>;

int main(){
    int n, q; cin >> n >> q;
    using T = pair<mint, mint>;
    vector<T> f(n);
    for(int i = 0; i < n; i++){
        cin >> f[i].first >> f[i].second;
    }
    HeavyLightDecomposition hl(n);
    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        hl.add_edge(u, v);
    }
    hl.build();
    
    auto op = [](T x1, T x2) -> T { 
        return {x1.first * x2.first, (x2.first * x1.second + x2.second)};
    };
    T ex = {1, 0};
    SegTree<T> seg(n, op, ex), seg2(n, op, ex);
    for(int i = 0; i < n; i++){
        seg.update(hl.get(i), {f[i].first, f[i].second});
        seg2.update(n - hl.get(i) - 1, {f[i].first, f[i].second});
    }
    mint ans = 0;
    T g = ex;
    auto query = [&](int l, int r){
        g = op(g, seg.query(l, r));
    };
    auto query2 = [&](int l, int r){
        g = op(g, seg2.query(l, r));
    };
    while(q--){
        int t; cin >> t;
        if(t == 0){
            int p, c, d; cin >> p >> c >> d;
            seg.update(hl.get(p), {c, d});
            seg2.update(n - hl.get(p) - 1, {c, d});
        }else{
            int u, v, x; cin >> u >> v >> x;
            g = ex;
            hl.path_noncommutative_query(u, v, query, query2);
            ans = g.first * x + g.second;
            cout << ans << "\n";
        }
    }
}
Back to top page