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/library_checker/tree/vertex_set_path_composite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include "../../../src/template/template.hpp"
#include "../../../src/template/static_modint.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/heavy_light_decomposition.hpp"
#include "../../../src/data_structure/segment_tree.hpp"
using mint = modint998244353;
struct S {
    mint a, b;
};
S op1(S x, S y) {
    return {x.a * y.a, x.b * y.a + y.b};
}
S op2(S x, S y) {
    return {x.a * y.a, y.b * x.a + x.b};
}
S e() {
    return {1, 0};
}
int main(void) {
    int n, q;
    cin >> n >> q;
    vector<ll> a(n), b(n);
    rep(i, 0, n) {
        cin >> a[i] >> b[i];
    }
    Graph<int> g(n);
    rep(i, 0, n - 1) {
        int u, v;
        cin >> u >> v;
        g.add_edge(u, v);
    }
    HeavyLightDecomposition<int> hld(g);
    SegmentTree<S, op1, e> seg1(n);
    SegmentTree<S, op2, e> seg2(n);
    rep(i, 0, n) {
        seg1.set(hld.idx(i).first, {a[i], b[i]});
        seg2.set(hld.idx(i).first, {a[i], b[i]});
    }
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            int p;
            mint c, d;
            cin >> p >> c >> d;
            seg1.set(hld.idx(p).first, {c, d});
            seg2.set(hld.idx(p).first, {c, d});
        } else {
            int u, v;
            mint x;
            cin >> u >> v >> x;
            mint ans = x;
            auto query = [&](int u, int v) -> void {
                if(u <= v) {
                    S res = seg1.prod(u, v);
                    ans = res.a * ans + res.b;
                } else {
                    S res = seg2.prod(v, u);
                    ans = res.a * ans + res.b;
                }
            };
            hld.path_query(u, v, true, query);
            cout << ans << '\n';
        }
    }
}
#line 1 "verify/library_checker/tree/vertex_set_path_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#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/template/static_modint.hpp"
template <uint32_t m>
struct StaticModint {
    using mint = StaticModint;
    static constexpr uint32_t mod() {
        return m;
    }
    static constexpr mint raw(const uint32_t v) {
        mint a;
        a._v = v;
        return a;
    }
    constexpr StaticModint()
        : _v(0) {}
    template <class T>
    constexpr StaticModint(const T& v) {
        static_assert(is_integral_v<T>);
        if constexpr(is_signed_v<T>) {
            int64_t x = int64_t(v % int64_t(m));
            if(x < 0) x += m;
            _v = uint32_t(x);
        } else _v = uint32_t(v % m);
    }
    constexpr uint32_t val() const {
        return _v;
    }
    constexpr mint& operator++() {
        return *this += 1;
    }
    constexpr mint& operator--() {
        return *this -= 1;
    }
    constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
    }
    constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr mint& operator+=(mint rhs) {
        if(_v >= m - rhs._v) _v -= m;
        _v += rhs._v;
        return *this;
    }
    constexpr mint& operator-=(mint rhs) {
        if(_v < rhs._v) _v += m;
        _v -= rhs._v;
        return *this;
    }
    constexpr mint& operator*=(mint rhs) {
        return *this = *this * rhs;
    }
    constexpr mint& operator/=(mint rhs) {
        return *this *= rhs.inv();
    }
    constexpr mint operator+() const {
        return *this;
    }
    constexpr mint operator-() const {
        return mint{} - *this;
    }
    constexpr mint pow(long long n) const {
        assert(0 <= n);
        if(n == 0) return 1;
        mint x = *this, r = 1;
        while(1) {
            if(n & 1) r *= x;
            n >>= 1;
            if(n == 0) return r;
            x *= x;
        }
    }
    constexpr mint inv() const {
        if constexpr(prime) {
            assert(_v);
            return pow(m - 2);
        } else {
            const auto eg = inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }
    friend constexpr mint operator+(mint lhs, mint rhs) {
        return lhs += rhs;
    }
    friend constexpr mint operator-(mint lhs, mint rhs) {
        return lhs -= rhs;
    }
    friend constexpr mint operator*(mint lhs, mint rhs) {
        return uint64_t(lhs._v) * rhs._v;
    }
    friend constexpr mint operator/(mint lhs, mint rhs) {
        return lhs /= rhs;
    }
    friend constexpr bool operator==(mint lhs, mint rhs) {
        return lhs._v == rhs._v;
    }
    friend constexpr bool operator!=(mint lhs, mint rhs) {
        return lhs._v != rhs._v;
    }
    friend istream& operator>>(istream& in, mint& x) {
        long long a;
        in >> a;
        x = a;
        return in;
    }
    friend ostream& operator<<(ostream& out, const mint& x) {
        return out << x.val();
    }

   private:
    uint32_t _v = 0;
    static constexpr bool prime = []() -> bool {
        if(m == 1) return 0;
        if(m == 2 or m == 7 or m == 61) return 1;
        if(m % 2 == 0) return 0;
        uint32_t d = m - 1;
        while(d % 2 == 0) d /= 2;
        for(uint32_t a : {2, 7, 61}) {
            uint32_t t = d;
            mint y = mint(a).pow(t);
            while(t != m - 1 && y != 1 && y != m - 1) {
                y *= y;
                t <<= 1;
            }
            if(y != m - 1 && t % 2 == 0) return 0;
        }
        return 1;
    }();
    static constexpr pair<int32_t, int32_t> inv_gcd(const int32_t a, const int32_t b) {
        if(a == 0) return {b, 0};
        int32_t s = b, t = a, m0 = 0, m1 = 1;
        while(t) {
            const int32_t u = s / t;
            s -= t * u;
            m0 -= m1 * u;
            swap(s, t);
            swap(m0, m1);
        }
        if(m0 < 0) m0 += b / s;
        return {s, m0};
    }
};
using modint998244353 = StaticModint<998244353>;
using modint1000000007 = StaticModint<1000000007>;
#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/segment_tree.hpp"
template <typename S, auto op, auto e>
struct SegmentTree {
    SegmentTree(const int N)
        : SegmentTree(vector<S>(N, e())) {}
    SegmentTree(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());
        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;
        data[p] = x;
        for(int i = 1; i <= log; ++i) {
            update(p >> i);
        }
    }
    S get(const int p) const {
        assert(0 <= p and p < n);
        return data[p + size];
    }
    S prod(int l, int r) const {
        assert(0 <= l and l <= r and r <= n);
        S sml = e(), smr = e();
        l += size;
        r += size;
        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];
    }

    template <bool (*f)(S)>
    int max_right(const int l) const {
        return max_right(l, [](const S& x) { return f(x); });
    }
    template <class F>
    int max_right(int l, const F& f) const {
        assert(0 <= l and l <= n);
        assert(f(e()));
        if(l == n) return n;
        l += size;
        S sm = e();
        do {
            while(l % 2 == 0) l >>= 1;
            if(!f(op(sm, data[l]))) {
                while(l < size) {
                    l = l * 2;
                    if(f(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 (*f)(S)>
    int min_left(const int r) const {
        return min_left(r, [](const S& x) { return f(x); });
    }
    template <class F>
    int min_left(int r, const F& f) const {
        assert(0 <= r and r <= n);
        assert(f(e()));
        if(r == 0) return 0;
        r += size;
        S sm = e();
        do {
            --r;
            while(r > 1 and (r % 2)) r >>= 1;
            if(!f(op(data[r], sm))) {
                while(r < size) {
                    r = 2 * r + 1;
                    if(f(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;
    inline void update(const int k) {
        data[k] = op(data[2 * k], data[2 * k + 1]);
    }
    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 7 "verify/library_checker/tree/vertex_set_path_composite.test.cpp"
using mint = modint998244353;
struct S {
    mint a, b;
};
S op1(S x, S y) {
    return {x.a * y.a, x.b * y.a + y.b};
}
S op2(S x, S y) {
    return {x.a * y.a, y.b * x.a + x.b};
}
S e() {
    return {1, 0};
}
int main(void) {
    int n, q;
    cin >> n >> q;
    vector<ll> a(n), b(n);
    rep(i, 0, n) {
        cin >> a[i] >> b[i];
    }
    Graph<int> g(n);
    rep(i, 0, n - 1) {
        int u, v;
        cin >> u >> v;
        g.add_edge(u, v);
    }
    HeavyLightDecomposition<int> hld(g);
    SegmentTree<S, op1, e> seg1(n);
    SegmentTree<S, op2, e> seg2(n);
    rep(i, 0, n) {
        seg1.set(hld.idx(i).first, {a[i], b[i]});
        seg2.set(hld.idx(i).first, {a[i], b[i]});
    }
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            int p;
            mint c, d;
            cin >> p >> c >> d;
            seg1.set(hld.idx(p).first, {c, d});
            seg2.set(hld.idx(p).first, {c, d});
        } else {
            int u, v;
            mint x;
            cin >> u >> v >> x;
            mint ans = x;
            auto query = [&](int u, int v) -> void {
                if(u <= v) {
                    S res = seg1.prod(u, v);
                    ans = res.a * ans + res.b;
                } else {
                    S res = seg2.prod(v, u);
                    ans = res.a * ans + res.b;
                }
            };
            hld.path_query(u, v, true, query);
            cout << ans << '\n';
        }
    }
}
Back to top page