library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_vertex_set_path_composite.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>

static const int MOD = 998244353;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

#include "../util/modint.cpp"

#include "../tree/hld.cpp"

#include "../datastructure/segtree.cpp"

struct Ml {
    using T = array<mint, 2>;
    static T f(T a, T b) { return {a[0]*b[0], a[1]*b[0]+b[1]}; }
    static T e() { return {1, 0}; }
};

struct Mr {
    using T = array<mint, 2>;
    static T f(T b, T a) { return {a[0]*b[0], a[1]*b[0]+b[1]}; }
    static T e() { return {1, 0}; }
};

int main() {
    int n, q;
    cin >> n >> q;
    HeavyLightDecomposition G(n);
    SegmentTree<Ml> segl(n);
    SegmentTree<Mr> segr(n);
    {
        vector<int> a(n), b(n);
        for (int i = 0; i < n; ++i) {
            scanf("%d %d", &a[i], &b[i]);
        }
        for (int i = 0; i < n - 1; ++i) {
            int l, r;
            scanf("%d %d", &l, &r);
            G.add_edge(l, r);
        }
        G.build();
        for (int i = 0; i < n; ++i) {
            int id = G.id[i];
            segl.set(id, {a[i], b[i]});
            segr.set(id, {a[i], b[i]});
        }
        segl.build(); segr.build();
    }
    auto fl = [&](int l, int r){ return segl.query(l, r); };
    auto fr = [&](int l, int r){ return segr.query(l, r); };
    auto merge = [&](Ml::T a, Ml::T b) -> Ml::T { return {a[0]*b[0], a[1]*b[0]+b[1]}; };
    for (int i = 0; i < q; ++i) {
        int t, a, b, c;
        scanf("%d %d %d %d", &t, &a, &b, &c);
        if(t == 0){
            a = G.id[a];
            segl.update(a, {b, c});
            segr.update(a, {b, c});
        }else {
            auto val = G.query_order(a, b, Ml::e(), fl, fr, merge, false);
            printf("%d\n", (val[0]*c + val[1]).val);
        }
    }
    return 0;
}
#line 1 "test/yosupo_vertex_set_path_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>

static const int MOD = 998244353;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

#line 1 "util/modint.cpp"
template <uint M>
struct modint {
    uint val;
public:
    static modint raw(int v) { modint x; x.val = v; return x; }
    modint() : val(0) {}
    template <class T>
    modint(T v) { ll x = (ll)(v%(ll)(M)); if (x < 0) x += M; val = uint(x); }
    modint(bool v) { val = ((unsigned int)(v) % M); }
    modint& operator++() { val++; if (val == M) val = 0; return *this; }
    modint& operator--() { if (val == 0) val = M; val--; return *this; }
    modint operator++(int) { modint result = *this; ++*this; return result; }
    modint operator--(int) { modint result = *this; --*this; return result; }
    modint& operator+=(const modint& b) { val += b.val; if (val >= M) val -= M; return *this; }
    modint& operator-=(const modint& b) { val -= b.val; if (val >= M) val += M; return *this; }
    modint& operator*=(const modint& b) { ull z = val; z *= b.val; val = (uint)(z % M); return *this; }
    modint& operator/=(const modint& b) { return *this = *this * b.inv(); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint() - *this; }
    modint pow(long long n) const { modint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
    modint inv() const { return pow(M-2); }
    friend modint operator+(const modint& a, const modint& b) { return modint(a) += b; }
    friend modint operator-(const modint& a, const modint& b) { return modint(a) -= b; }
    friend modint operator*(const modint& a, const modint& b) { return modint(a) *= b; }
    friend modint operator/(const modint& a, const modint& b) { return modint(a) /= b; }
    friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; }
    friend bool operator!=(const modint& a, const modint& b) { return a.val != b.val; }
};
using mint = modint<MOD>;

/**
 * @brief modint(固定MOD)
 * @docs _md/modint.md
 */
#line 19 "test/yosupo_vertex_set_path_composite.test.cpp"

#line 1 "tree/hld.cpp"

class HeavyLightDecomposition {
    void dfs_sz(int v){
        for (auto &&u : G[v]) {
            if(u == par[v]) continue;
            par[u] = v; dep[u] = dep[v] + 1;
            dfs_sz(u);
            sub_size[v] += sub_size[u];
            if(sub_size[u] > sub_size[G[v][0]]) swap(u, G[v][0]);
        }
    }
    void dfs_hld(int v, int c, int &pos){
        id[v] = pos++;
        id_inv[id[v]]= v;
        tree_id[v] = c;
        for (auto &&u : G[v]) {
            if(u == par[v]) continue;
            head[u] = (u == G[v][0] ? head[v] : u);
            dfs_hld(u, c, pos);
        }
    }
public:
    int n;
    vector<vector<int>> G;
    vector<int> par, dep, sub_size, id, id_inv, tree_id, head;
    explicit HeavyLightDecomposition(int n) : n(n), G(n), par(n), dep(n), sub_size(n, 1), id(n), id_inv(n), tree_id(n), head(n){}
    explicit HeavyLightDecomposition(vector<vector<int>> &G) :G(G), n(G.size()), par(n), dep(n), sub_size(n, 1), id(n), id_inv(n), tree_id(n), head(n) {}

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

    void build(vector<int> roots = {0}){
        int c = 0, pos = 0;
        for (auto &&i : roots) {
            dfs_sz(i);
            head[i] = i;
            dfs_hld(i, c++, pos);
        }
    }

    int lca(int u, int v){
        while(true){
            if(id[u] > id[v]) swap(u, v);
            if(head[u] == head[v]) return u;
            v = par[head[v]];
        }
    }

    int ancestor(int v, int k) {
        if(dep[v] < k) return -1;
        while(true) {
            int u = head[v];
            if(id[v] - k >= id[u]) return id_inv[id[v] - k];
            k -= id[v]-id[u]+1;
            v = par[u];
        }
    }

    int distance(int u, int v){ return dep[u] + dep[v] - 2*dep[lca(u, v)]; }

    template<typename F>
    void add(int u, int v, const F &f, bool edge){
        while (head[u] != head[v]){
            if(id[u] > id[v]) swap(u, v);
            f(id[head[v]], id[v]+1);
            v = par[head[v]];
        }
        if(id[u] > id[v]) swap(u, v);
        f(id[u]+edge, id[v]+1);
    }

    template<typename T, typename Q, typename F>
    T query(int u, int v, const T &e, const Q &q, const F &f, bool edge){
        T l = e, r = e;
        while(head[u] != head[v]){
            if(id[u] > id[v]) swap(u, v), swap(l, r);
            l = f(l, q(id[head[v]], id[v]+1));
            v = par[head[v]];
        }
        if(id[u] > id[v]) swap(u, v), swap(l, r);
        return f(q(id[u]+edge, id[v]+1), f(l, r));
    }

    template<typename T, typename QL, typename QR, typename F>
    T query_order(int u, int v, const T &e, const QL &ql, const QR &qr, const F &f, bool edge){
        T l = e, r = e;
        while(head[u] != head[v]){
            if(id[u] > id[v]) {
                l = f(l, qr(id[head[u]], id[u]+1));
                u = par[head[u]];
            }else {
                r = f(ql(id[head[v]], id[v]+1), r);
                v = par[head[v]];
            }
        }
        T mid = (id[u] > id[v] ? qr(id[v]+edge, id[u]+1) : ql(id[u]+edge, id[v]+1));
        return f(f(l, mid), r);
    }
};
#line 21 "test/yosupo_vertex_set_path_composite.test.cpp"

#line 1 "datastructure/segtree.cpp"
template <class M>
struct SegmentTree{
    using T = typename M::T;
    int sz, n, height{};
    vector<T> seg;
    explicit SegmentTree(int n) : n(n) {
        sz = 1; while(sz < n) sz <<= 1, height++;
        seg.assign(2*sz, M::e());
    }

    void set(int k, const T &x){ seg[k + sz] = x; }

    void build(){
        for (int i = sz-1; i > 0; --i) seg[i] = M::f(seg[2*i], seg[2*i+1]);
    }

    void update(int k, const T &x){
        k += sz;
        seg[k] = x;
        while (k >>= 1) seg[k] = M::f(seg[2*k], seg[2*k+1]);
    }

    T query(int a, int b){
        T l = M::e(), r = M::e();
        for(a += sz, b += sz; a < b; a >>=1, b>>=1){
            if(a & 1) l = M::f(l, seg[a++]);
            if(b & 1) r = M::f(seg[--b], r);
        }
        return M::f(l, r);
    }

    template<class F>
    int search_right(int l, F cond){
        if(l == n) return n;
        T val = M::e();
        l += sz;
        do {
            while(!(l&1)) l >>= 1;
            if(!cond(M::f(val, seg[l]))){
                while(l < sz) {
                    l <<= 1;
                    if (cond(M::f(val, seg[l]))){
                        val = M::f(val, seg[l]);
                        l++;
                    }
                }
                return l - sz;
            }
            val = M::f(val, seg[l]);
            l++;
        } while((l & -l) != l);
        return n;
    }

    template<class F>
    int search_left(int r, F cond){
        if(r == 0) return 0;
        T val = M::e();
        r += sz;
        do {
            r--;
            while(r&1) r >>= 1;
            if(!cond(M::f(seg[r], val))){
                while(r < sz) {
                    r = ((r << 1)|1);
                    if (cond(M::f(seg[r], val))){
                        val = M::f(seg[r], val);
                        r--;
                    }
                }
                return r + 1 - sz;
            }
            val = M::f(seg[r], val);
        } while((r & -r) != r);
        return 0;
    }
    T operator[](const int &k) const { return seg[k + sz]; }
};


/*
struct Monoid{
    using T = array<mint, 2>;
    static T f(T a, T b) { return {a[0]*b[0], a[1]*b[0]+b[1]}; }
    static T e() { return {1, 0}; }
};
*/
#line 23 "test/yosupo_vertex_set_path_composite.test.cpp"

struct Ml {
    using T = array<mint, 2>;
    static T f(T a, T b) { return {a[0]*b[0], a[1]*b[0]+b[1]}; }
    static T e() { return {1, 0}; }
};

struct Mr {
    using T = array<mint, 2>;
    static T f(T b, T a) { return {a[0]*b[0], a[1]*b[0]+b[1]}; }
    static T e() { return {1, 0}; }
};

int main() {
    int n, q;
    cin >> n >> q;
    HeavyLightDecomposition G(n);
    SegmentTree<Ml> segl(n);
    SegmentTree<Mr> segr(n);
    {
        vector<int> a(n), b(n);
        for (int i = 0; i < n; ++i) {
            scanf("%d %d", &a[i], &b[i]);
        }
        for (int i = 0; i < n - 1; ++i) {
            int l, r;
            scanf("%d %d", &l, &r);
            G.add_edge(l, r);
        }
        G.build();
        for (int i = 0; i < n; ++i) {
            int id = G.id[i];
            segl.set(id, {a[i], b[i]});
            segr.set(id, {a[i], b[i]});
        }
        segl.build(); segr.build();
    }
    auto fl = [&](int l, int r){ return segl.query(l, r); };
    auto fr = [&](int l, int r){ return segr.query(l, r); };
    auto merge = [&](Ml::T a, Ml::T b) -> Ml::T { return {a[0]*b[0], a[1]*b[0]+b[1]}; };
    for (int i = 0; i < q; ++i) {
        int t, a, b, c;
        scanf("%d %d %d %d", &t, &a, &b, &c);
        if(t == 0){
            a = G.id[a];
            segl.update(a, {b, c});
            segr.update(a, {b, c});
        }else {
            auto val = G.query_order(a, b, Ml::e(), fl, fr, merge, false);
            printf("%d\n", (val[0]*c + val[1]).val);
        }
    }
    return 0;
}
Back to top page