結果

問題 No.2342 Triple Tree Query (Hard)
ユーザー hashiryohashiryo
提出日時 2023-10-30 16:22:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,387 ms / 10,000 ms
コード長 29,632 bytes
コンパイル時間 3,620 ms
コンパイル使用メモリ 252,096 KB
実行使用メモリ 15,780 KB
最終ジャッジ日時 2024-09-25 17:18:33
合計ジャッジ時間 53,597 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 14 ms
5,376 KB
testcase_03 AC 14 ms
5,376 KB
testcase_04 AC 12 ms
5,376 KB
testcase_05 AC 12 ms
5,376 KB
testcase_06 AC 14 ms
5,376 KB
testcase_07 AC 2,094 ms
15,724 KB
testcase_08 AC 2,101 ms
15,692 KB
testcase_09 AC 2,145 ms
15,780 KB
testcase_10 AC 2,069 ms
15,724 KB
testcase_11 AC 2,166 ms
15,724 KB
testcase_12 AC 2,137 ms
15,720 KB
testcase_13 AC 2,101 ms
15,768 KB
testcase_14 AC 2,154 ms
15,652 KB
testcase_15 AC 2,110 ms
15,712 KB
testcase_16 AC 2,169 ms
15,724 KB
testcase_17 AC 268 ms
15,644 KB
testcase_18 AC 263 ms
15,624 KB
testcase_19 AC 260 ms
15,596 KB
testcase_20 AC 263 ms
15,728 KB
testcase_21 AC 274 ms
15,756 KB
testcase_22 AC 575 ms
15,720 KB
testcase_23 AC 466 ms
15,580 KB
testcase_24 AC 566 ms
15,652 KB
testcase_25 AC 1,405 ms
15,596 KB
testcase_26 AC 1,350 ms
15,668 KB
testcase_27 AC 1,332 ms
15,720 KB
testcase_28 AC 1,393 ms
15,728 KB
testcase_29 AC 1,428 ms
15,724 KB
testcase_30 AC 662 ms
15,660 KB
testcase_31 AC 728 ms
15,648 KB
testcase_32 AC 666 ms
15,656 KB
testcase_33 AC 2,256 ms
15,724 KB
testcase_34 AC 2,282 ms
15,720 KB
testcase_35 AC 2,264 ms
15,644 KB
testcase_36 AC 2,387 ms
15,708 KB
testcase_37 AC 2,026 ms
15,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// clang-format off
std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
#define checkpoint() (void(0))
#define debug(...) (void(0))
#define debugArray(x,n) (void(0))
#define debugMatrix(x,h,w) (void(0))
// clang-format on
#include <type_traits>
template <class Int> constexpr inline Int mod_inv(Int a, Int mod) {
 static_assert(std::is_signed_v<Int>);
 Int x= 1, y= 0, b= mod;
 for (Int q= 0, z= 0; b;) z= x, x= y, y= z - y * (q= a / b), z= a, a= b, b= z - b * q;
 return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
#define CE constexpr
#define IL inline
#define NORM \
 if (n >= mod) n-= mod; \
 return n
#define PLUS(U, M) \
 CE IL U plus(U l, U r) const { \
  if (l+= r; l >= M) l-= M; \
  return l; \
 }
#define DIFF(U, C, M) \
 CE IL U diff(U l, U r) const { \
  if (l-= r; l >> C) l+= M; \
  return l; \
 }
#define SGN(U) \
 static CE IL U set(U n) { return n; } \
 static CE IL U get(U n) { return n; } \
 static CE IL U norm(U n) { return n; }
template <class u_t, class du_t, u8 B, u8 A> struct MP_Mo {
 u_t mod;
 CE MP_Mo(): mod(0), iv(0), r2(0) {}
 CE MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
 CE IL u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
 PLUS(u_t, mod << 1)
 DIFF(u_t, A, mod << 1)
 CE IL u_t set(u_t n) const { return mul(n, r2); }
 CE IL u_t get(u_t n) const {
  n= reduce(n);
  NORM;
 }
 CE IL u_t norm(u_t n) const { NORM; }
private:
 u_t iv, r2;
 static CE u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
 CE IL u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
struct MP_Na {
 u32 mod;
 CE MP_Na(): mod(0){};
 CE MP_Na(u32 m): mod(m) {}
 CE IL u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
 PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32)
};
struct MP_Br {  // mod < 2^31
 u32 mod;
 CE MP_Br(): mod(0), s(0), x(0) {}
 CE MP_Br(u32 m): mod(m), s(95 - __builtin_clz(m - 1)), x(((u128(1) << s) + m - 1) / m) {}
 CE IL u32 mul(u32 l, u32 r) const { return rem(u64(l) * r); }
 PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) private: u8 s;
 u64 x;
 CE IL u64 quo(u64 n) const { return (u128(x) * n) >> s; }
 CE IL u32 rem(u64 n) const { return n - quo(n) * mod; }
};
struct MP_Br2 {  // 2^20 < mod <= 2^41
 u64 mod;
 CE MP_Br2(): mod(0), x(0) {}
 CE MP_Br2(u64 m): mod(m), x((u128(1) << 84) / m) {}
 CE IL u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
 PLUS(u64, mod << 1)
 DIFF(u64, 63, mod << 1)
 static CE IL u64 set(u64 n) { return n; }
 CE IL u64 get(u64 n) const { NORM; }
 CE IL u64 norm(u64 n) const { NORM; }
private:
 u64 x;
 CE IL u128 quo(const u128 &n) const { return (n * x) >> 84; }
 CE IL u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
struct MP_D2B1 {
 u8 s;
 u64 mod, d, v;
 CE MP_D2B1(): s(0), mod(0), d(0), v(0) {}
 CE MP_D2B1(u64 m): s(__builtin_clzll(m)), mod(m), d(m << s), v(u128(-1) / d) {}
 CE IL u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
 PLUS(u64, mod) DIFF(u64, 63, mod) SGN(u64) private: CE IL u64 rem(const u128 &u) const {
  u128 q= (u >> 64) * v + u;
  u64 r= u64(u) - (q >> 64) * d - d;
  if (r > u64(q)) r+= d;
  if (r >= d) r-= d;
  return r;
 }
};
template <class u_t, class MP> CE u_t pow(u_t x, u64 k, const MP &md) {
 for (u_t ret= md.set(1);; x= md.mul(x, x))
  if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
#undef NORM
#undef PLUS
#undef DIFF
#undef SGN
#undef CE
}
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
namespace math_internal {
#define CE constexpr
template <class MP, u64 MOD> struct SB: s_b {
protected:
 static CE MP md= MP(MOD);
};
template <class Int, class U, class B> struct MInt: public B {
 using Uint= U;
 static CE inline auto mod() { return B::md.mod; }
 CE MInt(): x(0) {}
 template <class T, enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>, nullptr_t> = nullptr> CE MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
 CE MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
 CE MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
 CE MInt name const { \
  MInt ret; \
  ret.x= op; \
  return ret; \
 }
 FUNC(operator+(const MInt& r), B::md.plus(x, r.x))
 FUNC(operator-(const MInt& r), B::md.diff(x, r.x))
 FUNC(operator*(const MInt& r), B::md.mul(x, r.x))
 FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
 CE MInt operator/(const MInt& r) const { return *this * r.inv(); }
 CE MInt& operator+=(const MInt& r) { return *this= *this + r; }
 CE MInt& operator-=(const MInt& r) { return *this= *this - r; }
 CE MInt& operator*=(const MInt& r) { return *this= *this * r; }
 CE MInt& operator/=(const MInt& r) { return *this= *this / r; }
 CE bool operator==(const MInt& r) const { return B::md.norm(x) == B::md.norm(r.x); }
 CE bool operator!=(const MInt& r) const { return !(*this == r); }
 CE bool operator<(const MInt& r) const { return B::md.norm(x) < B::md.norm(r.x); }
 CE inline MInt inv() const { return mod_inv<Int>(val(), B::md.mod); }
 CE inline Uint val() const { return B::md.get(x); }
 friend ostream& operator<<(ostream& os, const MInt& r) { return os << r.val(); }
 friend istream& operator>>(istream& is, MInt& r) {
  i64 v;
  return is >> v, r= MInt(v), is;
 }
private:
 Uint x;
};
template <u64 MOD> using ModInt= conditional_t < (MOD < (1 << 30)) & MOD, MInt<int, u32, SB<MP_Mo<u32, u64, 32, 31>, MOD>>, conditional_t < (MOD < (1ull << 62)) & MOD, MInt<i64, u64, SB<MP_Mo<u64, u128, 64, 63>, MOD>>, conditional_t<MOD<(1u << 31), MInt<int, u32, SB<MP_Na, MOD>>, conditional_t<MOD<(1ull << 32), MInt<i64, u32, SB<MP_Na, MOD>>, conditional_t<MOD <= (1ull << 41), MInt<i64, u64, SB<MP_Br2, MOD>>, MInt<i64, u64, SB<MP_D2B1, MOD>>>>>>>;
#undef CE
}
using math_internal::ModInt;
template <class mod_t, size_t LM> mod_t get_inv(int n) {
 static_assert(is_modint_v<mod_t>);
 static const auto m= mod_t::mod();
 static mod_t dat[LM];
 static int l= 1;
 if (l == 1) dat[l++]= 1;
 while (l <= n) dat[l++]= dat[m % l] * (m - m / l);
 return dat[n];
}
template <class T> struct ListRange {
 using Iterator= typename std::vector<T>::const_iterator;
 Iterator bg, ed;
 Iterator begin() const { return bg; }
 Iterator end() const { return ed; }
 size_t size() const { return std::distance(bg, ed); }
 const T &operator[](int i) const { return bg[i]; }
};
template <class T> class CsrArray {
 std::vector<T> csr;
 std::vector<int> pos;
public:
 CsrArray()= default;
 CsrArray(const std::vector<T> &c, const std::vector<int> &p): csr(c), pos(p) {}
 size_t size() const { return pos.size() - 1; }
 const ListRange<T> operator[](int i) const { return {csr.cbegin() + pos[i], csr.cbegin() + pos[i + 1]}; }
};
template <class Cost= void> class Tree {
 template <class D, class T> struct Edge_B {
  int to;
  T cost;
  operator int() const { return to; }
 };
 template <class D> struct Edge_B<D, void> {
  int to;
  operator int() const { return to; }
 };
 using Edge= Edge_B<void, Cost>;
 std::vector<std::conditional_t<std::is_same_v<Cost, void>, std::pair<int, int>, std::tuple<int, int, Cost>>> es;
 std::vector<Edge> g;
 std::vector<int> P, PP, D, I, L, R, pos;
public:
 Tree(int n): P(n, -2) {}
 template <class T= Cost, std::enable_if_t<std::is_same_v<T, void>, std::nullptr_t> = nullptr> void add_edge(int u, int v) { es.emplace_back(u, v), es.emplace_back(v, u); }
 template <class T, std::enable_if_t<std::is_convertible_v<T, Cost>, std::nullptr_t> = nullptr> void add_edge(int u, int v, T c) { es.emplace_back(u, v, c), es.emplace_back(v, u, c); }
 template <class T, class U, std::enable_if_t<std::conjunction_v<std::is_convertible<T, Cost>, std::is_convertible<U, Cost>>, std::nullptr_t> = nullptr> void add_edge(int u, int v, T c, U d) /* c:u->v, d:v->u */ { es.emplace_back(u, v, c), es.emplace_back(v, u, d); }
 void build(int root= 0) {
  size_t n= P.size();
  I.resize(n), PP.resize(n), std::iota(PP.begin(), PP.end(), 0), D.assign(n, 0), L.assign(n, 0), R.assign(n, 0), pos.resize(n + 1), g.resize(es.size());
  for (const auto &e: es) ++pos[std::get<0>(e)];
  std::partial_sum(pos.begin(), pos.end(), pos.begin());
  if constexpr (std::is_same_v<Cost, void>)
   for (const auto &[f, t]: es) g[--pos[f]]= {t};
  else
   for (const auto &[f, t, c]: es) g[--pos[f]]= {t, c};
  auto f= [&, i= 0, v= 0, t= 0](int r) mutable {
   for (P[r]= -1, I[t++]= r; i < t; ++i)
    for (int u: operator[](v= I[i]))
     if (P[v] != u) P[I[t++]= u]= v;
  };
  f(root);
  for (size_t r= 0; r < n; ++r)
   if (P[r] == -2) f(r);
  std::vector<int> Z(n, 1), nx(n, -1);
  for (int i= n, v; i--;) {
   if (P[v= I[i]] == -1) continue;
   if (Z[P[v]]+= Z[v]; nx[P[v]] == -1) nx[P[v]]= v;
   if (Z[nx[P[v]]] < Z[v]) nx[P[v]]= v;
  }
  for (int v: I)
   if (nx[v] != -1) PP[nx[v]]= v;
  for (int v: I)
   if (P[v] != -1) PP[v]= PP[PP[v]], D[v]= D[P[v]] + 1;
  for (int i= n; i--;) L[I[i]]= i;
  for (int v: I) {
   int ir= R[v]= L[v] + Z[v];
   for (int u: operator[](v))
    if (u != P[v] && u != nx[v]) L[u]= ir-= Z[u];
   if (nx[v] != -1) L[nx[v]]= L[v] + 1;
  }
  for (int i= n; i--;) I[L[i]]= i;
 }
 size_t size() const { return P.size(); }
 const ListRange<Edge> operator[](int v) const { return {g.cbegin() + pos[v], g.cbegin() + pos[v + 1]}; }
 int depth(int v) const { return D[v]; }
 int to_seq(int v) const { return L[v]; }
 int to_node(int i) const { return I[i]; }
 int parent(int v) const { return P[v]; }
 int root(int v) const {
  for (v= PP[v];; v= PP[P[v]])
   if (P[v] == -1) return v;
 }
 bool connected(int u, int v) const { return root(u) == root(v); }
 int lca(int u, int v) const {
  for (;; v= P[PP[v]]) {
   if (L[u] > L[v]) std::swap(u, v);
   if (PP[u] == PP[v]) return u;
  }
 }
 int la(int v, int k) const {
  assert(k <= D[v]);
  for (int u;; k-= L[v] - L[u] + 1, v= P[u])
   if (L[v] - k >= L[u= PP[v]]) return I[L[v] - k];
 }
 int jump(int u, int v, int k) const {
  if (!k) return u;
  if (u == v) return -1;
  if (k == 1) return in_subtree(v, u) ? la(v, D[v] - D[u] - 1) : P[u];
  int w= lca(u, v), d_uw= D[u] - D[w], d_vw= D[v] - D[w];
  return k > d_uw + d_vw ? -1 : k <= d_uw ? la(u, k) : la(v, d_uw + d_vw - k);
 }
 int dist(int u, int v) const { return depth(u) + depth(v) - depth(lca(u, v)) * 2; }
 // u is in v
 bool in_subtree(int u, int v) const { return L[v] <= L[u] && L[u] < R[v]; }
 int subtree_size(int v) const { return R[v] - L[v]; }
 // half-open interval
 std::array<int, 2> subtree(int v) const { return std::array{L[v], R[v]}; }
 // sequence of closed intervals
 template <bool edge= 0> std::vector<std::array<int, 2>> path(int u, int v) const {
  std::vector<std::array<int, 2>> up, down;
  while (PP[u] != PP[v]) {
   if (L[u] < L[v]) down.emplace_back(std::array{L[PP[v]], L[v]}), v= P[PP[v]];
   else up.emplace_back(std::array{L[u], L[PP[u]]}), u= P[PP[u]];
  }
  if (L[u] < L[v]) down.emplace_back(std::array{L[u] + edge, L[v]});
  else if (L[v] + edge <= L[u]) up.emplace_back(std::array{L[u], L[v] + edge});
  return up.insert(up.end(), down.rbegin(), down.rend()), up;
 }
};
#define MEMBER_MACRO(member, Dummy, name, type1, type2, last) \
 template <class tClass> struct name##member { \
  template <class U, Dummy> static type1 check(U *); \
  static type2 check(...); \
  static tClass *mClass; \
  last; \
 };
#define HAS_CHECK(member, Dummy) MEMBER_MACRO(member, Dummy, has_, std::true_type, std::false_type, static const bool value= decltype(check(mClass))::value)
#define HAS_MEMBER(member) HAS_CHECK(member, int dummy= (&U::member, 0))
#define HAS_TYPE(member) HAS_CHECK(member, class dummy= typename U::member)
#define HOGE_OR(member, name, type2) \
 MEMBER_MACRO(member, class dummy= typename U::member, name, typename U::member, type2, using type= decltype(check(mClass))) \
 template <class tClass> using name##member##_t= typename name##member<tClass>::type;
#define NULLPTR_OR(member) HOGE_OR(member, nullptr_or_, std::nullptr_t);
#define MYSELF_OR(member) HOGE_OR(member, myself_or_, tClass);
template <class T> static constexpr bool tuple_like_v= false;
template <class... Args> static constexpr bool tuple_like_v<std::tuple<Args...>> = true;
template <class T, class U> static constexpr bool tuple_like_v<std::pair<T, U>> = true;
template <class T, size_t K> static constexpr bool tuple_like_v<std::array<T, K>> = true;
template <class T> auto to_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::make_tuple(x...); }, t);
}
template <class T> auto forward_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::forward_as_tuple(x...); }, t);
}
template <class T> static constexpr bool array_like_v= false;
template <class T, size_t K> static constexpr bool array_like_v<std::array<T, K>> = true;
template <class T, class U> static constexpr bool array_like_v<std::pair<T, U>> = std::is_convertible_v<T, U>;
template <class T> static constexpr bool array_like_v<std::tuple<T>> = true;
template <class T, class U, class... Args> static constexpr bool array_like_v<std::tuple<T, U, Args...>> = array_like_v<std::tuple<T, Args...>> && std::is_convertible_v<U, T>;
template <class T> auto to_array(const T &t) {
 if constexpr (array_like_v<T>) return std::apply([](auto &&...x) { return std::array{x...}; }, t);
}
template <class T> using to_tuple_t= decltype(to_tuple(T()));
template <class T> using to_array_t= decltype(to_array(T()));
template <class T> struct other_than_first_argument_type_impl {
 using type= void;
};
template <class T, class... Args> struct other_than_first_argument_type_impl<std::tuple<T, Args...>> {
 using type= std::tuple<Args...>;
};
template <class T> using other_than_first_argument_type_t= typename other_than_first_argument_type_impl<T>::type;
// clang-format off
template<class T>struct make_long{using type= T;};
template<>struct make_long<int8_t>{using type= int16_t;};
template<>struct make_long<uint8_t>{using type= uint16_t;};
template<>struct make_long<int16_t>{using type= int32_t;};
template<>struct make_long<uint16_t>{using type= uint32_t;};
template<>struct make_long<int32_t>{using type= int64_t;};
template<>struct make_long<uint32_t>{using type= uint64_t;};
template<>struct make_long<int64_t>{using type= __int128_t;};
template<>struct make_long<uint64_t>{using type= __uint128_t;};
template<>struct make_long<float>{using type= double;};
template<>struct make_long<double>{using type= long double;};
template<class T> using make_long_t= typename make_long<T>::type;
// clang-format on
namespace kdtree_internal {
template <class pos_t, size_t K, class M, class A, class B> class KDTreeImpl {};
template <class pos_t, size_t K, class M, class... PK, class... PK2> class KDTreeImpl<pos_t, K, M, std::tuple<PK...>, std::tuple<PK2...>> {
 HAS_MEMBER(op);
 HAS_MEMBER(ti);
 HAS_MEMBER(mp);
 HAS_MEMBER(cp);
 HAS_TYPE(T);
 HAS_TYPE(E);
 MYSELF_OR(T);
 NULLPTR_OR(E);
 using Sec= std::array<pos_t, 2>;
 using Pos= std::array<pos_t, K>;
 using Range= std::array<Sec, K>;
 using long_pos_t= make_long_t<pos_t>;
 template <class L> static constexpr bool monoid_v= std::conjunction_v<has_T<L>, has_op<L>, has_ti<L>>;
 template <class L> static constexpr bool dual_v= std::conjunction_v<has_T<L>, has_E<L>, has_mp<L>, has_cp<L>>;
 struct Node_BB {
  int ch[2]= {-1, -1};
  Pos pos;
  pos_t range[K][2];
 };
 template <class U> struct Node_B: Node_BB {
  U val;
 };
 template <class D, bool sg, bool du> struct Node_D: Node_B<M> {};
 template <bool sg, bool du> struct Node_D<void, sg, du>: Node_BB {};
 template <class D> struct Node_D<D, 1, 0>: Node_B<typename M::T> {
  typename M::T sum;
 };
 template <class D> struct Node_D<D, 0, 1>: Node_B<typename M::T> {
  typename M::E laz;
  bool laz_flg= false;
 };
 template <class D> struct Node_D<D, 1, 1>: Node_B<typename M::T> {
  typename M::T sum;
  typename M::E laz;
  bool laz_flg= false;
 };
 using Node= Node_D<M, monoid_v<M>, dual_v<M>>;
 using Iter= typename std::vector<int>::iterator;
 using T= std::conditional_t<std::is_void_v<M>, std::nullptr_t, myself_or_T_t<M>>;
 using E= nullptr_or_E_t<M>;
 template <class P> using canbe_Pos= std::is_convertible<to_tuple_t<P>, std::tuple<PK...>>;
 template <class P> using canbe_PosV= std::is_convertible<to_tuple_t<P>, std::tuple<PK..., T>>;
 template <class P, class U> static constexpr bool canbe_Pos_and_T_v= std::conjunction_v<canbe_Pos<P>, std::is_convertible<U, T>>;
 std::vector<Node> ns;
 static inline T def_val() {
  if constexpr (monoid_v<M>) return M::ti();
  else return T();
 }
 template <bool z, size_t k, class P> static inline auto get_(const P &p) {
  if constexpr (z == 0) return std::get<k>(p);
  else return std::get<k>(p.first);
 }
 template <class P, size_t... I> Range to_range(const P &p, std::index_sequence<I...>) { return {(assert(std::get<I + I>(p) <= std::get<I + I + 1>(p)), Sec{std::get<I + I>(p), std::get<I + I + 1>(p)})...}; }
 inline void update(int t) {
  ns[t].sum= ns[t].val;
  if (ns[t].ch[0] != -1) ns[t].sum= M::op(ns[t].sum, ns[ns[t].ch[0]].sum);
  if (ns[t].ch[1] != -1) ns[t].sum= M::op(ns[t].sum, ns[ns[t].ch[1]].sum);
 }
 inline void propagate(int t, const E &x) {
  if (t == -1) return;
  if (ns[t].laz_flg) M::cp(ns[t].laz, x);
  else ns[t].laz= x, ns[t].laz_flg= true;
  M::mp(ns[t].val, x);
  if constexpr (monoid_v<M>) M::mp(ns[t].sum, x);
 }
 inline void push(int t) {
  if (ns[t].laz_flg) ns[t].laz_flg= false, propagate(ns[t].ch[0], ns[t].laz), propagate(ns[t].ch[1], ns[t].laz);
 }
 template <bool z, class P, size_t k> inline void set_range(int t, int m, Iter bg, Iter ed, const P *p) {
  auto [mn, mx]= std::minmax_element(bg, ed, [&](int a, int b) { return get_<z, k>(p[a]) < get_<z, k>(p[b]); });
  ns[t].range[k][0]= get_<z, k>(p[*mn]), ns[t].range[k][1]= get_<z, k>(p[*mx]), ns[t].pos[k]= get_<z, k>(p[m]);
 }
 template <bool z, class P, size_t... I> inline void set_range_lp(int t, int m, Iter bg, Iter ed, const P *p, std::index_sequence<I...>) { (void)(int[]){(set_range<z, P, I>(t, m, bg, ed, p), 0)...}; }
 template <bool z, uint8_t div, class P> inline int build(int &ts, Iter bg, Iter ed, const P *p, const T &v= def_val()) {
  if (bg == ed) return -1;
  auto md= bg + (ed - bg) / 2;
  int t= ts++;
  std::nth_element(bg, md, ed, [&](int a, int b) { return get_<z, div>(p[a]) < get_<z, div>(p[b]); }), set_range_lp<z>(t, *md, bg, ed, p, std::make_index_sequence<K>());
  if constexpr (z == 0) {
   if constexpr (!std::is_void_v<M>) {
    if constexpr (std::tuple_size_v<P> == K + 1) ns[t].val= std::get<K>(p[*md]);
    else ns[t].val= v;
   }
  } else ns[t].val= p[*md].second;
  static constexpr uint8_t nx= div + 1 == K ? 0 : div + 1;
  ns[t].ch[0]= build<z, nx>(ts, bg, md, p, v), ns[t].ch[1]= build<z, nx>(ts, md + 1, ed, p, v);
  if constexpr (monoid_v<M>) update(t);
  return t;
 }
 template <bool z, uint8_t div, class P> inline int build(Iter bg, Iter ed, const P *p, int &ts) {
  if (bg == ed) return -1;
  auto md= bg + (ed - bg) / 2;
  int t= ts++;
  std::nth_element(bg, md, ed, [&](int a, int b) { return get_<z, div>(p[a]) < get_<z, div>(p[b]); }), set_range_lp<z>(t, bg, ed, p, std::make_index_sequence<K>());
  if constexpr (z == 0) {
   if constexpr (!std::is_void_v<M>) {
    if constexpr (std::tuple_size_v<P> == K + 1) ns[t].val= std::get<K>(p[t]);
    else ns[t].val= def_val();
   }
  } else ns[t].val= p[t].second;
  static constexpr uint8_t nx= div + 1 == K ? 0 : div + 1;
  ns[t].ch[0]= build<z, nx>(bg, md, p, ts), ns[t].ch[1]= build<z, nx>(md + 1, ed, p, ts);
  if constexpr (monoid_v<M>) update(t);
  return t;
 }
 static inline auto in_cuboid(const Range &r) {
  return [r](const Pos &pos) {
   for (uint8_t k= K; k--;)
    if (r[k][1] < pos[k] || pos[k] < r[k][0]) return false;
   return true;
  };
 }
 static inline auto out_cuboid(const Range &r) {
  return [r](const pos_t rr[K][2]) {
   for (uint8_t k= K; k--;)
    if (rr[k][1] < r[k][0] || r[k][1] < rr[k][0]) return true;
   return false;
  };
 }
 static inline auto inall_cuboid(const Range &r) {
  return [r](const pos_t rr[K][2]) {
   for (uint8_t k= K; k--;)
    if (rr[k][0] < r[k][0] || r[k][1] < rr[k][1]) return false;
   return true;
  };
 }
 static inline long_pos_t min_dist2(const pos_t r[K][2], const Pos &pos) {
  long_pos_t d2= 0, dx;
  for (uint8_t k= K; k--;) dx= std::clamp(pos[k], r[k][0], r[k][1]) - pos[k], d2+= dx * dx;
  return d2;
 }
 static inline auto in_ball(const Pos &c, long_pos_t r2) {
  return [c, r2](const Pos &pos) {
   long_pos_t d2= 0, dx;
   for (uint8_t k= K; k--;) dx= pos[k] - c[k], d2+= dx * dx;
   return d2 <= r2;
  };
 }
 static inline auto inall_ball(const Pos &c, long_pos_t r2) {
  return [c, r2](const pos_t rr[K][2]) {
   long_pos_t d2= 0, dx0, dx1;
   for (uint8_t k= K; k--;) dx0= rr[k][0] - c[k], dx1= rr[k][1] - c[k], d2+= std::max(dx0 * dx0, dx1 * dx1);
   return d2 <= r2;
  };
 }
 static inline auto out_ball(const Pos &c, long_pos_t r2) {
  return [c, r2](const pos_t r[K][2]) { return min_dist2(r, c) > r2; };
 }
 inline void nns(int t, const Pos &pos, std::pair<int, long_pos_t> &ret) const {
  if (t == -1) return;
  long_pos_t d2= min_dist2(ns[t].range, pos);
  if (ret.first != -1 && d2 >= ret.second) return;
  long_pos_t dx= d2= 0;
  for (uint8_t k= K; k--;) dx= pos[k] - ns[t].pos[k], d2+= dx * dx;
  if (ret.first == -1 || d2 < ret.second) ret= {t, d2};
  bool f= 0;
  if (auto [l, r]= ns[t].ch; l != -1 && r != -1) f= min_dist2(ns[l].range, pos) > min_dist2(ns[r].range, pos);
  nns(ns[t].ch[f], pos, ret), nns(ns[t].ch[!f], pos, ret);
 }
 template <class In, class Out> inline void col(int t, const In &in, const Out &out, std::vector<T> &ret) const {
  if (t == -1 || out(ns[t].range)) return;
  if (in(ns[t].pos)) ret.push_back(ns[t].val);
  col(ns[t].ch[0], in, out, ret), col(ns[t].ch[1], in, out, ret);
 }
 template <class In, class InAll, class Out> inline T fld(int t, const In &in, const InAll &inall, const Out &out) {
  if (t == -1 || out(ns[t].range)) return def_val();
  if (inall(ns[t].range)) return ns[t].sum;
  if constexpr (dual_v<M>) push(t);
  T ret= M::op(fld(ns[t].ch[0], in, inall, out), fld(ns[t].ch[1], in, inall, out));
  return in(ns[t].pos) ? M::op(ret, ns[t].val) : ret;
 }
 template <class In, class InAll, class Out> inline void app(int t, const In &in, const InAll &inall, const Out &out, const E &x) {
  if (t == -1 || out(ns[t].range)) return;
  if (inall(ns[t].range)) return propagate(t, x);
  if (push(t); in(ns[t].pos)) M::mp(ns[t].val, x);
  app(ns[t].ch[0], in, inall, out, x), app(ns[t].ch[1], in, inall, out, x);
  if constexpr (monoid_v<M>) update(t);
 }
 inline bool set(int t, const Pos &pos, const T &x) {
  if (t == -1) return false;
  bool isok= true;
  for (uint8_t k= K; k--; isok&= pos[k] == ns[t].pos[k])
   if (ns[t].range[k][1] < pos[k] || pos[k] < ns[t].range[k][0]) return false;
  if constexpr (dual_v<M>) push(t);
  if (isok) ns[t].val= x;
  else if (!(isok= set(ns[t].ch[0], pos, x))) isok= set(ns[t].ch[1], pos, x);
  if constexpr (monoid_v<M>)
   if (isok) update(t);
  return isok;
 }
 inline std::pair<T, bool> get(int t, const Pos &pos) {
  if (t == -1) return {T(), false};
  bool myself= true;
  for (uint8_t k= K; k--; myself&= pos[k] == ns[t].pos[k])
   if (ns[t].range[k][1] < pos[k] || pos[k] < ns[t].range[k][0]) return {T(), false};
  if (myself) return {ns[t].val, true};
  if constexpr (dual_v<M>) push(t);
  auto ret= get(ns[t].ch[0], pos);
  return !ret.second ? get(ns[t].ch[1], pos) : ret;
 }
public:
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> KDTreeImpl(const P *p, size_t n): ns(n) {
  std::vector<int> ids(n);
  int ts= 0;
  std::iota(ids.begin(), ids.end(), 0), build<0, 0>(ts, ids.begin(), ids.end(), p);
 }
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> KDTreeImpl(const std::vector<P> &p): KDTreeImpl(p.data(), p.size()) {}
 template <class P, typename= std::enable_if_t<canbe_Pos<P>::value>> KDTreeImpl(const std::set<P> &p): KDTreeImpl(std::vector(p.begin(), p.end())) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> KDTreeImpl(const P *p, size_t n, U v): ns(n) {
  std::vector<int> ids(n);
  int ts= 0;
  std::iota(ids.begin(), ids.end(), 0), build<0, 0>(ts, ids.begin(), ids.end(), p, v);
 }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> KDTreeImpl(const std::vector<P> &p, U v): KDTreeImpl(p.data(), p.size(), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> KDTreeImpl(const std::set<P> &p, U v): KDTreeImpl(std::vector(p.begin(), p.end()), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> KDTreeImpl(const std::pair<P, U> *p, size_t n): ns(n) {
  std::vector<int> ids(n);
  int ts= 0;
  std::iota(ids.begin(), ids.end(), 0), build<1, 0>(ts, ids.begin(), ids.end(), p);
 }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> KDTreeImpl(const std::vector<std::pair<P, U>> &p): KDTreeImpl(p.data(), p.size()) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> KDTreeImpl(const std::map<P, U> &p): KDTreeImpl(std::vector(p.begin(), p.end())) {}
 std::vector<T> enum_cuboid(PK2... xs) {
  static_assert(!std::is_void_v<M>, "\"enum_cuboid\" is not available");
  std::vector<T> ret;
  auto r= to_range(std::forward_as_tuple(xs...), std::make_index_sequence<K>());
  return col(-ns.empty(), in_cuboid(r), out_cuboid(r), ret), ret;
 }
 std::vector<T> enum_ball(PK... xs, pos_t r) const {
  static_assert(!std::is_void_v<M>, "\"enum_ball\" is not available");
  std::vector<T> ret;
  long_pos_t r2= long_pos_t(r) * r;
  return col(-ns.empty(), in_ball({xs...}, r2), out_ball({xs...}, r2), ret), ret;
 }
 T fold_cuboid(PK2... xs) {
  static_assert(monoid_v<M>, "\"fold_cuboid\" is not available");
  auto r= to_range(std::forward_as_tuple(xs...), std::make_index_sequence<K>());
  return fld(-ns.empty(), in_cuboid(r), inall_cuboid(r), out_cuboid(r));
 }
 T fold_ball(PK... xs, pos_t r) {
  static_assert(monoid_v<M>, "\"fold_ball\" is not available");
  long_pos_t r2= long_pos_t(r) * r;
  return fld(-ns.empty(), in_ball({xs...}, r2), inall_ball({xs...}, r2), out_ball({xs...}, r2));
 }
 void apply_cuboid(PK2... xs, E a) {
  static_assert(dual_v<M>, "\"apply_cuboid\" is not available");
  auto r= to_range(std::forward_as_tuple(xs...), std::make_index_sequence<K>());
  app(-ns.empty(), in_cuboid(r), inall_cuboid(r), out_cuboid(r), a);
 }
 void apply_ball(PK... xs, pos_t r, E a) {
  static_assert(dual_v<M>, "\"apply_ball\" is not available");
  long_pos_t r2= long_pos_t(r) * r;
  app(-ns.empty(), in_ball({xs...}, r2), inall_ball({xs...}, r2), out({xs...}, r2), a);
 }
 void set(PK... p, T v) { assert(ns.size()), assert(set(0, {p...}, v)); }
 T get(PK... p) {
  assert(ns.size());
  auto [ret, flg]= get(0, {p...});
  return assert(flg), ret;
 }
 Pos nearest_neighbor(PK... p) const {
  assert(ns.size());
  std::pair<int, long_pos_t> ret= {-1, -1};
  return nns(0, {p...}, ret), ns[ret.first].pos;
 }
};
template <class pos_t, size_t K, class M= void> using KDTree= KDTreeImpl<pos_t, K, M, to_tuple_t<std::array<pos_t, K>>, to_tuple_t<std::array<pos_t, K + K>>>;
}
using kdtree_internal::KDTree;
using namespace std;
using Mint= ModInt<998244353>;
struct RaffineQ {
 using T= Mint;
 using E= array<Mint, 2>;
 static void mp(T &v, const E &x) { v= x[0] * v + x[1]; }
 static void cp(E &p, const E &n) { p[0]*= n[0], p[1]*= n[0], p[1]+= n[1]; }
};
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N, Q;
 cin >> N >> Q;
 Tree tree(N);
 for (int i= N - 1; i--;) {
  int A, B;
  cin >> A >> B;
  tree.add_edge(A - 1, B - 1);
 }
 tree.build(0);
 vector<tuple<int, int, Mint>> xy;
 for (int v= 0; v < N; ++v) {
  Mint X;
  cin >> X;
  xy.emplace_back(tree.to_seq(v), tree.depth(v), X);
 }
 KDTree<int, 2, RaffineQ> kdt(xy);
 while (Q--) {
  int t;
  cin >> t;
  if (t == 1) {
   int V;
   cin >> V, --V;
   cout << kdt.get(tree.to_seq(V), tree.depth(V)) << "\n";
  } else if (t == 2) {
   int V, K, C, D;
   cin >> V >> K >> C >> D, --V;
   for (int i= 0; i <= K; ++i) {
    int p= tree.parent(V);
    auto [l, r]= tree.subtree(V);
    int d= tree.depth(V);
    if (p == -1) {
     kdt.apply_cuboid(l, r - 1, d, d + K - i, {C, D});
     break;
    }
    kdt.apply_cuboid(l, r - 1, d + K - i - 1, d + K - i, {C, D});
    V= p;
   }
  } else if (t == 3) {
   int V, C, D;
   cin >> V >> C >> D, --V;
   auto [l, r]= tree.subtree(V);
   kdt.apply_cuboid(l, r - 1, 0, N, {C, D});
  } else {
   int U, V, C, D;
   cin >> U >> V >> C >> D, --U, --V;
   for (auto [a, b]: tree.path(U, V)) a < b ? kdt.apply_cuboid(a, b, 0, N, {C, D}) : kdt.apply_cuboid(b, a, 0, N, {C, D});
  }
 }
 return 0;
}
0