#line 1 "test/yj_vertex_set_path_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite"
#include <cstdint>
#include <cstdio>
#include <utility>
#include <vector>
#line 1 "Graph/hl_decomposition.cpp"
/**
* @brief HL 分解
* @author えびちゃん
* @see https://codeforces.com/blog/entry/53170
*/
#include <cstddef>
#include <tuple>
#include <type_traits>
#line 15 "Graph/hl_decomposition.cpp"
struct value_on_directed_edge_tag {};
struct value_on_undirected_edge_tag {};
struct value_on_vertex_tag {};
template <typename RangeQuery, typename ValueAttribute>
class hl_decomposed_tree {
public:
using size_type = size_t;
using range_query_type = RangeQuery;
using value_type = typename range_query_type::value_type;
using attribute = ValueAttribute;
private:
size_type M_n = 0;
std::vector<size_type> M_p, M_hp; // parent, heavy path root
std::vector<size_type> M_in;
range_query_type M_fa, M_fd;
void M_dfs_size(
std::vector<std::vector<size_type>>& al, std::vector<size_type>& ss,
size_type v, size_type p
) {
ss[v] = 1;
M_p[v] = p;
if (al[v][0] == p) std::swap(al[v][0], al[v].back());
for (auto& u: al[v]) {
if (u == p) continue;
M_dfs_size(al, ss, u, v);
ss[v] += ss[u];
if (ss[u] > ss[al[v][0]]) std::swap(u, al[v][0]);
}
}
void M_dfs_heavy_path(
std::vector<std::vector<size_type>> const& al, std::vector<size_type> const& ss,
size_type v, size_type& t
) {
M_in[v] = t++;
for (auto u: al[v]) {
if (u == M_p[v]) continue;
M_hp[u] = ((u == al[v][0])? M_hp[v]: u);
M_dfs_heavy_path(al, ss, u, t);
}
}
void M_decompose(std::vector<std::vector<size_type>>& al, size_type r = 0) {
std::vector<size_type> ss(M_n, 0);
M_dfs_size(al, ss, r, M_n);
size_type in = 0;
M_dfs_heavy_path(al, ss, r, in);
}
size_type M_lca(size_type u, size_type v) const {
if (M_in[u] > M_in[v]) std::swap(u, v);
if (M_hp[u] == M_hp[v]) return u;
return M_lca(u, M_p[M_hp[v]]);
}
static void S_fold(value_type& this_, value_type that, bool foldl) {
if (foldl) {
this_ += that;
} else {
this_ = that + std::move(this_);
}
}
value_type M_fold_one_way(size_type u, size_type v, bool asc) {
value_type res{};
if (asc) {
while (M_hp[u] != M_hp[v]) {
size_type l = M_n-1 - M_in[u];
size_type r = M_n-1 - M_in[M_hp[u]];
S_fold(res, M_fa.fold(l, r+1), true);
u = M_p[M_hp[u]];
}
size_type l = M_n-1 - M_in[u];
size_type r = M_n-1 - M_in[v];
S_fold(res, M_fa.fold(l, r), true);
} else {
while (M_hp[u] != M_hp[v]) {
size_type l = M_in[M_hp[u]];
size_type r = M_in[u];
S_fold(res, M_fd.fold(l, r+1), false);
u = M_p[M_hp[u]];
}
size_type l = M_in[v]+1;
size_type r = M_in[u]+1;
S_fold(res, M_fd.fold(l, r), false);
}
return res;
}
template <typename Tp>
void M_act_one_way(size_type u, size_type v, Tp x, bool asc) {
if (asc) {
while (M_hp[u] != M_hp[v]) {
size_type l = M_n-1 - M_in[u];
size_type r = M_n-1 - M_in[M_hp[u]];
M_fa.act(l, r+1, x);
u = M_p[M_hp[u]];
}
size_type l = M_n-1 - M_in[u];
size_type r = M_n-1 - M_in[v];
M_fa.act(l, r, x);
} else {
while (M_hp[u] != M_hp[v]) {
size_type l = M_in[M_hp[u]];
size_type r = M_in[u];
M_fd.act(l, r+1, x);
u = M_p[M_hp[u]];
}
size_type l = M_in[v]+1;
size_type r = M_in[u]+1;
M_fd.act(l, r, x);
}
}
value_type M_fold(size_type u, size_type v) {
size_type w = M_lca(u, v);
value_type resl = M_fold_one_way(u, w, true);
value_type resr = M_fold_one_way(v, w, false);
if (std::is_same<attribute, value_on_vertex_tag>::value) {
resl += M_fd.fold(M_in[w], M_in[w]+1);
}
return resl += resr;
}
void M_set(size_type v, value_type x, bool asc) {
// on directed edges or on vertices
bool dir = std::is_same<ValueAttribute, value_on_directed_edge_tag>::value;
if (asc || !dir) M_fa.set(M_n-1 - M_in[v], x);
if (!asc || !dir) M_fd.set(M_in[v], x);
}
template <typename Tp>
void M_act(size_type u, size_type v, Tp x) {
size_type w = M_lca(u, v);
M_act_one_way(u, w, x, true);
M_act_one_way(v, w, x, false);
if (!std::is_same<attribute, value_on_directed_edge_tag>::value) {
M_act_one_way(v, w, x, true);
M_act_one_way(u, w, x, false);
if (std::is_same<attribute, value_on_vertex_tag>::value) {
M_act_one_way(w, M_p[w], x, true);
M_act_one_way(w, M_p[w], x, false);
}
}
}
public:
hl_decomposed_tree() = default;
template <
typename Va = ValueAttribute,
typename Tp = typename std::enable_if<std::is_same<Va, value_on_vertex_tag>::value, value_type>::type
>
hl_decomposed_tree(
std::vector<Tp> const& vs,
std::vector<std::pair<size_type, size_type>> const& es, size_type r = 0
): M_n(vs.size()+1), M_p(M_n, M_n), M_hp(M_n, r), M_in(M_n) {
size_type n = M_n-1;
std::vector<std::vector<size_type>> al(M_n);
for (auto const& [u, v]: es) {
al[u].push_back(v);
al[v].push_back(u);
}
al[r].push_back(n);
al[n].push_back(r);
M_decompose(al, n);
std::vector<value_type> a(M_n), d(M_n);
for (size_type i = 0; i < n; ++i) a[M_in[i]] = d[M_in[i]] = vs[i];
M_fa.assign(a.rbegin(), a.rend());
M_fd.assign(d.begin(), d.end());
}
template <
typename Va = ValueAttribute,
typename Sz = typename std::enable_if<!std::is_same<Va, value_on_vertex_tag>::value, size_type>::type
>
hl_decomposed_tree(
Sz n, std::vector<std::tuple<size_type, size_type, value_type>> const& es,
size_type r = 0
): M_n(n), M_p(n, n), M_hp(n, r), M_in(n) {
std::vector<std::vector<size_type>> al(n);
bool undir = std::is_same<Va, value_on_undirected_edge_tag>::value;
for (auto const& e: es) {
size_type u, v;
std::tie(u, v, std::ignore) = e;
al[u].push_back(v);
if (undir) al[v].push_back(u);
}
M_decompose(al);
std::vector<value_type> a(n), d(n);
for (auto const& [u, v, w]: es) {
if (u == M_p[v]) {
d[M_in[v]] = w;
if (undir) a[M_in[v]] = w;
} else {
a[M_in[u]] = w;
if (undir) d[M_in[u]] = w;
}
}
M_fa.assign(a.rbegin(), a.rend());
M_fd.assign(d.begin(), d.end());
}
value_type fold(size_type u, size_type v) { return M_fold(u, v); }
void set(size_type v, value_type x, bool asc = true) { M_set(v, x, asc); }
template <typename Rq = RangeQuery, typename Ta = typename Rq::action_type>
void act(size_type u, size_type v, Ta x) { M_act(u, v, x); }
};
#line 1 "DataStructure/basic_segment_tree.cpp"
/**
* @brief 単一更新セグメント木
* @author えびちゃん
* @docs docs/basic_segment_tree.md
*/
#line 11 "DataStructure/basic_segment_tree.cpp"
#include <algorithm>
#line 13 "DataStructure/basic_segment_tree.cpp"
template <typename Monoid>
class basic_segment_tree {
public:
using value_type = Monoid;
using size_type = size_t;
private:
std::vector<value_type> M_c;
size_type M_n;
public:
basic_segment_tree() = default;
explicit basic_segment_tree(size_type n): M_c(n+n), M_n(n) {}
explicit basic_segment_tree(size_type n, value_type const& x):
M_c(n+n, x), M_n(n)
{
for (size_type i = n; i--;) M_c[i] = M_c[i<<1|0] + M_c[i<<1|1];
}
template <typename InputIt>
basic_segment_tree(InputIt first, InputIt last) {
std::vector<value_type> tmp(first, last);
M_n = tmp.size();
M_c.resize(M_n);
M_c.insert(M_c.end(), tmp.begin(), tmp.end());
for (size_type i = M_n; i--;) M_c[i] = M_c[i<<1|0] + M_c[i<<1|1];
}
void assign(size_type n, value_type const& x) {
M_c.assign(n+n, x);
M_n = n;
for (size_type i = n; i--;) M_c[i] = M_c[i<<1|0] + M_c[i<<1|1];
}
template <typename InputIt>
void assign(InputIt first, InputIt last) {
std::vector<value_type> tmp(first, last);
M_n = tmp.size();
M_c.resize(M_n);
M_c.insert(M_c.end(), tmp.begin(), tmp.end());
for (size_type i = M_n; i--;) M_c[i] = M_c[i<<1|0] + M_c[i<<1|1];
}
void set(size_type i, value_type const& x) {
i += M_n;
M_c[i] = x;
while (i > 1) {
i >>= 1;
M_c[i] = M_c[i<<1|0] + M_c[i<<1|1];
}
}
void set(size_type i, value_type&& x) {
i += M_n;
M_c[i] = std::move(x);
while (i > 1) {
i >>= 1;
M_c[i] = M_c[i<<1|0] + M_c[i<<1|1];
}
}
value_type const& operator [](size_type i) const { return M_c[i + M_n]; }
value_type fold(size_type l, size_type r) const {
value_type resl{}, resr{};
l += M_n;
r += M_n;
while (l < r) {
if (l & 1) resl += M_c[l++];
if (r & 1) resr = M_c[--r] + std::move(resr);
l >>= 1;
r >>= 1;
}
return resl += resr;
}
template <typename Predicate>
size_type foldl_bisect(size_type l, Predicate pred) const {
size_type r = M_n+M_n;
value_type x{};
size_type h = 0;
if (l == M_n) return pred(x)? -1: l;
l += M_n;
auto bisect = [&](size_type v) -> size_type {
while (v < M_n) {
v <<= 1;
if (pred(x + M_c[v])) x += M_c[v++];
}
return v - M_n;
};
for (; l < r; ++h, l >>= 1, r >>= 1) {
if (l & 1) {
if (!pred(x + M_c[l])) return bisect(l);
x += M_c[l];
++l;
}
if (r & 1) --r;
}
while (r <<= 1, h--) {
if (((r+1) << h) <= M_n+M_n) {
if (!pred(x + M_c[r])) return bisect(r);
x += M_c[r];
++r;
}
}
return -1;
}
template <typename Predicate>
size_type foldr_bisect(size_type r, Predicate pred) const {
size_type l = M_n;
value_type x{};
size_type h = 0;
if (r == 0) return pred(x)? -1: 0;
r += M_n;
auto bisect = [&](size_type v) -> size_type {
while (v < M_n) {
v = (v << 1 | 1);
if (pred(M_c[v] + x)) x = M_c[v--] + std::move(x);
}
return v - M_n;
};
for (; l < r; ++h, l >>= 1, r >>= 1) {
if (l & 1) ++l;
if (r & 1) {
--r;
if (!pred(M_c[r] + x)) return bisect(r);
x = M_c[r] + std::move(x);
}
}
while (l <<= 1, h--) {
if (((l-1) << h) >= M_n) {
--l;
if (!pred(M_c[l] + x)) return bisect(l);
x = M_c[l] + std::move(x);
}
}
return -1;
}
};
#line 1 "ModularArithmetic/modint.cpp"
/**
* @brief 合同算術用クラス
* @author えびちゃん
*/
#line 10 "ModularArithmetic/modint.cpp"
#include <limits>
#include <type_traits>
#line 13 "ModularArithmetic/modint.cpp"
template <intmax_t Modulo>
class modint {
public:
using value_type = typename std::conditional<
(0 < Modulo && Modulo < std::numeric_limits<int>::max() / 2), int, intmax_t
>::type;
private:
static constexpr value_type S_cmod = Modulo; // compile-time
static value_type S_rmod; // runtime
value_type M_value = 0;
static constexpr value_type S_inv(value_type n, value_type m) {
value_type x = 0;
value_type y = 1;
value_type a = n;
value_type b = m;
for (value_type u = y, v = x; a;) {
value_type q = b / a;
std::swap(x -= q*u, u);
std::swap(y -= q*v, v);
std::swap(b -= q*a, a);
}
if ((x %= m) < 0) x += m;
return x;
}
static value_type S_normalize(intmax_t n, value_type m) {
if (n >= m) {
n %= m;
} else if (n < 0) {
if ((n %= m) < 0) n += m;
}
return n;
}
public:
modint() = default;
modint(intmax_t n): M_value(S_normalize(n, get_modulo())) {}
modint& operator =(intmax_t n) {
M_value = S_normalize(n, get_modulo());
return *this;
}
modint& operator +=(modint const& that) {
if ((M_value += that.M_value) >= get_modulo()) M_value -= get_modulo();
return *this;
}
modint& operator -=(modint const& that) {
if ((M_value -= that.M_value) < 0) M_value += get_modulo();
return *this;
}
modint& operator *=(modint const& that) {
intmax_t tmp = M_value;
tmp *= that.M_value;
M_value = tmp % get_modulo();
return *this;
}
modint& operator /=(modint const& that) {
intmax_t tmp = M_value;
tmp *= S_inv(that.M_value, get_modulo());
M_value = tmp % get_modulo();
return *this;
}
modint& operator ++() {
if (++M_value == get_modulo()) M_value = 0;
return *this;
}
modint& operator --() {
if (M_value-- == 0) M_value = get_modulo()-1;
return *this;
}
modint operator ++(int) { modint tmp(*this); ++*this; return tmp; }
modint operator --(int) { modint tmp(*this); --*this; return tmp; }
friend modint operator +(modint lhs, modint const& rhs) { return lhs += rhs; }
friend modint operator -(modint lhs, modint const& rhs) { return lhs -= rhs; }
friend modint operator *(modint lhs, modint const& rhs) { return lhs *= rhs; }
friend modint operator /(modint lhs, modint const& rhs) { return lhs /= rhs; }
modint operator +() const { return *this; }
modint operator -() const {
if (M_value == 0) return *this;
return modint(get_modulo() - M_value);
}
friend bool operator ==(modint const& lhs, modint const& rhs) {
return lhs.M_value == rhs.M_value;
}
friend bool operator !=(modint const& lhs, modint const& rhs) {
return !(lhs == rhs);
}
value_type get() const { return M_value; }
static value_type get_modulo() { return ((S_cmod > 0)? S_cmod: S_rmod); }
template <int M = Modulo, typename Tp = typename std::enable_if<(M <= 0)>::type>
static Tp set_modulo(value_type m) { S_rmod = m; }
};
template <intmax_t N>
constexpr typename modint<N>::value_type modint<N>::S_cmod;
template <intmax_t N>
typename modint<N>::value_type modint<N>::S_rmod;
#line 1 "utility/monoid/composite.cpp"
/**
* @brief 一次関数の合成を得る演算のモノイド
* @author えびちゃん
*/
#line 8 "utility/monoid/composite.cpp"
#ifndef H_composite_monoid
#define H_composite_monoid
template <typename Tp>
class composite_monoid {
public:
using value_type = Tp;
private:
value_type M_a = 1;
value_type M_b = 0;
public:
composite_monoid() = default; // identity
composite_monoid(value_type a, value_type b): M_a(a), M_b(b) {};
composite_monoid& operator +=(composite_monoid that) {
M_a *= that.M_a;
M_b *= that.M_a;
M_b += that.M_b;
return *this;
}
composite_monoid operator +(composite_monoid const& that) const {
return composite_monoid(*this) += that;
}
composite_monoid operator +(composite_monoid&& that) const {
return composite_monoid(*this) += std::move(that);
}
bool operator ==(composite_monoid const& that) const {
return (M_a == that.M_a && M_b == that.M_b);
}
bool operator !=(composite_monoid const& that) const { return !(*this == that); }
auto get() const { return std::make_pair(M_a, M_b); }
value_type operator ()(value_type x) const { return M_a * x + M_b; }
};
#endif /* !defined(H_composite_monoid) */
#line 12 "test/yj_vertex_set_path_composite.test.cpp"
constexpr intmax_t mod = 998244353;
using mi = modint<mod>;
int main() {
size_t n, q;
scanf("%zu %zu", &n, &q);
std::vector<composite_monoid<mi>> f(n);
for (auto& fi: f) {
int a, b;
scanf("%d %d", &a, &b);
fi = {a, b};
}
std::vector<std::pair<size_t, size_t>> es;
es.reserve(n-1);
for (size_t i = 1; i < n; ++i) {
size_t u, v;
scanf("%zu %zu", &u, &v);
es.emplace_back(u, v);
}
hl_decomposed_tree<basic_segment_tree<composite_monoid<mi>>, value_on_vertex_tag> g(f, es);
for (size_t i = 0; i < q; ++i) {
int t;
scanf("%d", &t);
if (t == 0) {
size_t p;
int c, d;
scanf("%zu %d %d", &p, &c, &d);
f[p] = {c, d};
g.set(p, f[p]);
} else if (t == 1) {
size_t u, v;
int x;
scanf("%zu %zu %d", &u, &v, &x);
printf("%d\n", g.fold(u, v)(x).get());
}
}
}