Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/yukicoder/2342.KDT.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/2342
// competitive-verifier: TLE 3
// competitive-verifier: MLE 64
// 双対のverify

#include <iostream>
#include <tuple>
#include <vector>
#include "src/Math/ModInt.hpp"
#include "src/Graph/Graph.hpp"
#include "src/Graph/HeavyLightDecomposition.hpp"
#include "src/DataStructure/KDTree.hpp"
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;
 Graph g(N, N - 1);
 for (int i= 0; i < N - 1; ++i) cin >> g[i], --g[i];
 HeavyLightDecomposition tree(g, 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;
}
#line 1 "test/yukicoder/2342.KDT.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/2342
// competitive-verifier: TLE 3
// competitive-verifier: MLE 64
// 双対のverify

#include <iostream>
#include <tuple>
#include <vector>
#line 2 "src/Math/mod_inv.hpp"
#include <utility>
#include <type_traits>
#include <cassert>
template <class Uint> constexpr inline Uint mod_inv(Uint a, Uint mod) {
 std::make_signed_t<Uint> x= 1, y= 0, z= 0;
 for (Uint q= 0, b= mod, c= 0; b;) z= x, x= y, y= z - y * (q= a / b), c= a, a= b, b= c - b * q;
 return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
#line 2 "src/Internal/Remainder.hpp"
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;
struct MP_Na {  // mod < 2^32
 u32 mod;
 constexpr MP_Na(): mod(0) {}
 constexpr MP_Na(u32 m): mod(m) {}
 constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
 constexpr inline u32 set(u32 n) const { return n; }
 constexpr inline u32 get(u32 n) const { return n; }
 constexpr inline u32 norm(u32 n) const { return n; }
 constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo {  // mod < 2^32, mod < 2^62
 u_t mod;
 constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
 constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
 constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
 constexpr inline u_t set(u_t n) const { return mul(n, r2); }
 constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
 constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
 constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
 u_t iv, r2;
 static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
 constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br {  // 2^20 < mod <= 2^41
 u64 mod;
 constexpr MP_Br(): mod(0), x(0) {}
 constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
 static constexpr inline u64 set(u64 n) { return n; }
 constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
 u64 x;
 constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
 constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 {  // mod < 2^63, mod < 2^64
 u64 mod;
 constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
 constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
 constexpr inline u64 set(u64 n) const { return n; }
 constexpr inline u64 get(u64 n) const { return n; }
 constexpr inline u64 norm(u64 n) const { return n; }
 constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
 u8 s;
 u64 d, v;
 constexpr inline 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;
 }
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr 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;
}
}
#line 3 "src/Internal/modint_traits.hpp"
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>;
#line 6 "src/Math/ModInt.hpp"
namespace math_internal {
template <class MP, u64 MOD> struct SB: s_b {
protected:
 static constexpr MP md= MP(MOD);
};
template <class U, class B> struct MInt: public B {
 using Uint= U;
 static constexpr inline auto mod() { return B::md.mod; }
 constexpr MInt(): x(0) {}
 template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> constexpr MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
 constexpr MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
 constexpr MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
 constexpr MInt name const { \
  MInt ret; \
  return ret.x= op, 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
 constexpr MInt operator/(const MInt &r) const { return *this * r.inv(); }
 constexpr MInt &operator+=(const MInt &r) { return *this= *this + r; }
 constexpr MInt &operator-=(const MInt &r) { return *this= *this - r; }
 constexpr MInt &operator*=(const MInt &r) { return *this= *this * r; }
 constexpr MInt &operator/=(const MInt &r) { return *this= *this / r; }
 constexpr bool operator==(const MInt &r) const { return B::md.norm(x) == B::md.norm(r.x); }
 constexpr bool operator!=(const MInt &r) const { return !(*this == r); }
 constexpr bool operator<(const MInt &r) const { return B::md.norm(x) < B::md.norm(r.x); }
 constexpr inline MInt inv() const { return mod_inv<U>(val(), B::md.mod); }
 constexpr 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 MP_B= conditional_t < (MOD < (1 << 30)) & MOD, MP_Mo32, conditional_t < MOD < (1ull << 32), MP_Na, conditional_t<(MOD < (1ull << 62)) & MOD, MP_Mo64, conditional_t<MOD<(1ull << 41), MP_Br, conditional_t<MOD<(1ull << 63), MP_D2B1_1, MP_D2B1_2>>>>>;
template <u64 MOD> using ModInt= MInt < conditional_t<MOD<(1 << 30), u32, u64>, SB<MP_B<MOD>, MOD>>;
}
using math_internal::ModInt;
#line 4 "src/Internal/ListRange.hpp"
#include <iterator>
#line 6 "src/Internal/ListRange.hpp"
#define _LR(name, IT, CT) \
 template <class T> struct name { \
  using Iterator= typename std::vector<T>::IT; \
  Iterator bg, ed; \
  Iterator begin() const { return bg; } \
  Iterator end() const { return ed; } \
  size_t size() const { return std::distance(bg, ed); } \
  CT &operator[](int i) const { return bg[i]; } \
 }
_LR(ListRange, iterator, T);
_LR(ConstListRange, const_iterator, const T);
#undef _LR
template <class T> struct CSRArray {
 std::vector<T> dat;
 std::vector<int> p;
 size_t size() const { return p.size() - 1; }
 ListRange<T> operator[](int i) { return {dat.begin() + p[i], dat.begin() + p[i + 1]}; }
 ConstListRange<T> operator[](int i) const { return {dat.cbegin() + p[i], dat.cbegin() + p[i + 1]}; }
};
template <template <class> class F, class T> std::enable_if_t<std::disjunction_v<std::is_same<F<T>, ListRange<T>>, std::is_same<F<T>, ConstListRange<T>>, std::is_same<F<T>, CSRArray<T>>>, std::ostream &> operator<<(std::ostream &os, const F<T> &r) {
 os << '[';
 for (int _= 0, __= r.size(); _ < __; ++_) os << (_ ? ", " : "") << r[_];
 return os << ']';
}
#line 3 "src/Graph/Graph.hpp"
struct Edge: std::pair<int, int> {
 using std::pair<int, int>::pair;
 Edge &operator--() { return --first, --second, *this; }
 int to(int v) const { return first ^ second ^ v; }
 friend std::istream &operator>>(std::istream &is, Edge &e) { return is >> e.first >> e.second, is; }
};
struct Graph: std::vector<Edge> {
 size_t n;
 Graph(size_t n= 0, size_t m= 0): vector(m), n(n) {}
 size_t vertex_size() const { return n; }
 size_t edge_size() const { return size(); }
 size_t add_vertex() { return n++; }
 size_t add_edge(int s, int d) { return emplace_back(s, d), size() - 1; }
 size_t add_edge(Edge e) { return emplace_back(e), size() - 1; }
#define _ADJ_FOR(a, b) \
 for (auto [u, v]: *this) a; \
 for (size_t i= 0; i < n; ++i) p[i + 1]+= p[i]; \
 for (int i= size(); i--;) { \
  auto [u, v]= (*this)[i]; \
  b; \
 }
#define _ADJ(a, b) \
 vector<int> p(n + 1), c(size() << !dir); \
 if (!dir) { \
  _ADJ_FOR((++p[u], ++p[v]), (c[--p[u]]= a, c[--p[v]]= b)) \
 } else if (dir > 0) { \
  _ADJ_FOR(++p[u], c[--p[u]]= a) \
 } else { \
  _ADJ_FOR(++p[v], c[--p[v]]= b) \
 } \
 return {c, p}
 CSRArray<int> adjacency_vertex(int dir) const { _ADJ(v, u); }
 CSRArray<int> adjacency_edge(int dir) const { _ADJ(i, i); }
#undef _ADJ
#undef _ADJ_FOR
};
#line 2 "src/Graph/HeavyLightDecomposition.hpp"
#include <array>
#line 5 "src/Graph/HeavyLightDecomposition.hpp"
class HeavyLightDecomposition {
 std::vector<int> P, PP, D, I, L, R;
public:
 HeavyLightDecomposition()= default;
 HeavyLightDecomposition(const Graph &g, int root= 0): HeavyLightDecomposition(g.adjacency_vertex(0), root) {}
 HeavyLightDecomposition(const CSRArray<int> &adj, int root= 0) {
  const int n= adj.size();
  P.assign(n, -2), PP.resize(n), D.resize(n), I.resize(n), L.resize(n), R.resize(n);
  auto f= [&, i= 0, v= 0, t= 0](int r) mutable {
   for (P[r]= -1, I[t++]= r; i < t; ++i)
    for (int u: adj[v= I[i]])
     if (P[v] != u) P[I[t++]= u]= v;
  };
  f(root);
  for (int 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= n; v--;) PP[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: adj[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;
 }
 int to_seq(int v) const { return L[v]; }
 int to_vertex(int i) const { return I[i]; }
 size_t size() const { return P.size(); }
 int parent(int v) const { return P[v]; }
 int head(int v) const { return PP[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); }
 // 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]; }
 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 depth(int v) const { return D[v]; }
 int dist(int u, int v) const { return D[u] + D[v] - D[lca(u, v)] * 2; }
 // half-open interval [l,r)
 std::pair<int, int> subtree(int v) const { return {L[v], R[v]}; }
 // sequence of closed intervals [l,r]
 std::vector<std::pair<int, int>> path(int u, int v, bool edge= 0) const {
  std::vector<std::pair<int, int>> up, down;
  while (PP[u] != PP[v]) {
   if (L[u] < L[v]) down.emplace_back(L[PP[v]], L[v]), v= P[PP[v]];
   else up.emplace_back(L[u], L[PP[u]]), u= P[PP[u]];
  }
  if (L[u] < L[v]) down.emplace_back(L[u] + edge, L[v]);
  else if (L[v] + edge <= L[u]) up.emplace_back(L[u], L[v] + edge);
  return up.insert(up.end(), down.rbegin(), down.rend()), up;
 }
};
#line 3 "src/DataStructure/KDTree.hpp"
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#line 8 "src/DataStructure/KDTree.hpp"
#include <cstdint>
#line 3 "src/Internal/HAS_CHECK.hpp"
#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)
#line 5 "src/Internal/tuple_traits.hpp"
#include <cstddef>
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()));
#line 2 "src/Internal/long_traits.hpp"
// clang-format off
template<class T>struct make_long{using type= T;};
template<>struct make_long<char>{using type= short;};
template<>struct make_long<unsigned char>{using type= unsigned short;};
template<>struct make_long<short>{using type= int;};
template<>struct make_long<unsigned short>{using type= unsigned;};
template<>struct make_long<int>{using type= long long;};
template<>struct make_long<unsigned>{using type= unsigned long long;};
template<>struct make_long<long long>{using type= __int128_t;};
template<>struct make_long<unsigned long long>{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
#line 12 "src/DataStructure/KDTree.hpp"
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 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 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) 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].flg) M::cp(ns[t].laz, x);
  else ns[t].laz= x, ns[t].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].flg) ns[t].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) {
   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) {
   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);
 }
 template <bool z> 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) {
   if constexpr (z) ns[t].val= x;
   else ns[t].val= M::op(ns[t].val, x);
  } else if (!(isok= set<z>(ns[t].ch[0], pos, x))) isok= set<z>(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<1, 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<1, 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<0, 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 prod_cuboid(PK2... xs) {
  static_assert(monoid_v<M>, "\"prod_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 prod_ball(PK... xs, pos_t r) {
  static_assert(monoid_v<M>, "\"prod_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... xs, T v) { assert(ns.size()), assert(set<1>(0, {xs...}, v)); }
 void mul(PK... xs, T v) {
  static_assert(monoid_v<M>, "\"mul\" is not available");
  assert(ns.size()), assert(set<0>(0, {xs...}, v));
 }
 T get(PK... xs) {
  assert(ns.size());
  auto [ret, flg]= get(0, {xs...});
  return assert(flg), ret;
 }
 Pos nearest_neighbor(PK... xs) const {
  assert(ns.size());
  std::pair<int, long_pos_t> ret= {-1, -1};
  return nns(0, {xs...}, 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;
#line 13 "test/yukicoder/2342.KDT.test.cpp"
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;
 Graph g(N, N - 1);
 for (int i= 0; i < N - 1; ++i) cin >> g[i], --g[i];
 HeavyLightDecomposition tree(g, 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;
}

Test cases

Env Name Status Elapsed Memory
g++-13 00_sample_01.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 00_sample_02.txt :heavy_check_mark: AC 5 ms 4 MB
g++-13 01_small_01.txt :heavy_check_mark: AC 11 ms 4 MB
g++-13 01_small_02.txt :heavy_check_mark: AC 12 ms 4 MB
g++-13 01_small_03.txt :heavy_check_mark: AC 10 ms 4 MB
g++-13 01_small_04.txt :heavy_check_mark: AC 10 ms 4 MB
g++-13 01_small_05.txt :heavy_check_mark: AC 11 ms 4 MB
g++-13 02_large_01.txt :heavy_check_mark: AC 1248 ms 13 MB
g++-13 02_large_02.txt :heavy_check_mark: AC 1259 ms 13 MB
g++-13 02_large_03.txt :heavy_check_mark: AC 1272 ms 13 MB
g++-13 02_large_04.txt :heavy_check_mark: AC 1262 ms 13 MB
g++-13 02_large_05.txt :heavy_check_mark: AC 1237 ms 13 MB
g++-13 02_large_06.txt :heavy_check_mark: AC 1282 ms 13 MB
g++-13 02_large_07.txt :heavy_check_mark: AC 1257 ms 13 MB
g++-13 02_large_08.txt :heavy_check_mark: AC 1273 ms 13 MB
g++-13 02_large_09.txt :heavy_check_mark: AC 1283 ms 13 MB
g++-13 02_large_10.txt :heavy_check_mark: AC 1260 ms 13 MB
g++-13 03_path_01.txt :heavy_check_mark: AC 163 ms 13 MB
g++-13 03_path_02.txt :heavy_check_mark: AC 165 ms 13 MB
g++-13 03_path_03.txt :heavy_check_mark: AC 163 ms 13 MB
g++-13 03_path_04.txt :heavy_check_mark: AC 169 ms 13 MB
g++-13 03_path_05.txt :heavy_check_mark: AC 163 ms 13 MB
g++-13 04_star_01.txt :heavy_check_mark: AC 332 ms 13 MB
g++-13 04_star_02.txt :heavy_check_mark: AC 359 ms 13 MB
g++-13 04_star_03.txt :heavy_check_mark: AC 381 ms 13 MB
g++-13 05_almost_path_01.txt :heavy_check_mark: AC 855 ms 13 MB
g++-13 05_almost_path_02.txt :heavy_check_mark: AC 825 ms 13 MB
g++-13 05_almost_path_03.txt :heavy_check_mark: AC 830 ms 13 MB
g++-13 05_almost_path_04.txt :heavy_check_mark: AC 856 ms 13 MB
g++-13 05_almost_path_05.txt :heavy_check_mark: AC 867 ms 13 MB
g++-13 06_almost_star_01.txt :heavy_check_mark: AC 464 ms 13 MB
g++-13 06_almost_star_02.txt :heavy_check_mark: AC 528 ms 13 MB
g++-13 06_almost_star_03.txt :heavy_check_mark: AC 501 ms 13 MB
g++-13 07_killer_01.txt :heavy_check_mark: AC 1431 ms 13 MB
g++-13 07_killer_02.txt :heavy_check_mark: AC 1422 ms 13 MB
g++-13 07_killer_03.txt :heavy_check_mark: AC 1470 ms 13 MB
g++-13 07_killer_04.txt :heavy_check_mark: AC 1440 ms 13 MB
g++-13 07_killer_05.txt :heavy_check_mark: AC 1294 ms 13 MB
clang++-18 00_sample_01.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 00_sample_02.txt :heavy_check_mark: AC 5 ms 4 MB
clang++-18 01_small_01.txt :heavy_check_mark: AC 12 ms 4 MB
clang++-18 01_small_02.txt :heavy_check_mark: AC 12 ms 4 MB
clang++-18 01_small_03.txt :heavy_check_mark: AC 11 ms 4 MB
clang++-18 01_small_04.txt :heavy_check_mark: AC 11 ms 4 MB
clang++-18 01_small_05.txt :heavy_check_mark: AC 13 ms 4 MB
clang++-18 02_large_01.txt :heavy_check_mark: AC 1404 ms 13 MB
clang++-18 02_large_02.txt :heavy_check_mark: AC 1385 ms 13 MB
clang++-18 02_large_03.txt :heavy_check_mark: AC 1381 ms 13 MB
clang++-18 02_large_04.txt :heavy_check_mark: AC 1387 ms 13 MB
clang++-18 02_large_05.txt :heavy_check_mark: AC 1359 ms 13 MB
clang++-18 02_large_06.txt :heavy_check_mark: AC 1413 ms 13 MB
clang++-18 02_large_07.txt :heavy_check_mark: AC 1379 ms 13 MB
clang++-18 02_large_08.txt :heavy_check_mark: AC 1411 ms 13 MB
clang++-18 02_large_09.txt :heavy_check_mark: AC 1429 ms 13 MB
clang++-18 02_large_10.txt :heavy_check_mark: AC 1386 ms 13 MB
clang++-18 03_path_01.txt :heavy_check_mark: AC 168 ms 13 MB
clang++-18 03_path_02.txt :heavy_check_mark: AC 179 ms 13 MB
clang++-18 03_path_03.txt :heavy_check_mark: AC 171 ms 13 MB
clang++-18 03_path_04.txt :heavy_check_mark: AC 173 ms 13 MB
clang++-18 03_path_05.txt :heavy_check_mark: AC 177 ms 13 MB
clang++-18 04_star_01.txt :heavy_check_mark: AC 366 ms 13 MB
clang++-18 04_star_02.txt :heavy_check_mark: AC 394 ms 13 MB
clang++-18 04_star_03.txt :heavy_check_mark: AC 416 ms 13 MB
clang++-18 05_almost_path_01.txt :heavy_check_mark: AC 932 ms 13 MB
clang++-18 05_almost_path_02.txt :heavy_check_mark: AC 897 ms 13 MB
clang++-18 05_almost_path_03.txt :heavy_check_mark: AC 891 ms 13 MB
clang++-18 05_almost_path_04.txt :heavy_check_mark: AC 923 ms 13 MB
clang++-18 05_almost_path_05.txt :heavy_check_mark: AC 942 ms 13 MB
clang++-18 06_almost_star_01.txt :heavy_check_mark: AC 509 ms 13 MB
clang++-18 06_almost_star_02.txt :heavy_check_mark: AC 592 ms 13 MB
clang++-18 06_almost_star_03.txt :heavy_check_mark: AC 552 ms 13 MB
clang++-18 07_killer_01.txt :heavy_check_mark: AC 1600 ms 13 MB
clang++-18 07_killer_02.txt :heavy_check_mark: AC 1597 ms 13 MB
clang++-18 07_killer_03.txt :heavy_check_mark: AC 1622 ms 13 MB
clang++-18 07_killer_04.txt :heavy_check_mark: AC 1583 ms 13 MB
clang++-18 07_killer_05.txt :heavy_check_mark: AC 1445 ms 13 MB
Back to top page