#pragma once
#include "../../util/consts/lib.hpp"
#include "../../util/traits/math/lib.hpp"
namespace tifa_libs {
template <uint_c T>
struct sbt {
T x{1}, y{1}; // now
T lx{0}, ly{1}; // lbound
T rx{1}, ry{0}; // rbound
CEXP sbt() NE = default;
CEXP void movl(int_c auto step = 1) NE {
using W = std::conditional_t<sint_c<decltype(step)>, to_sint_t<decltype(step * x)>, decltype(step * x)>;
rx += T((W)lx * step), ry += T((W)ly * step), x = rx + lx, y = ry + ly;
}
CEXP void movr(int_c auto step = 1) NE {
using W = std::conditional_t<sint_c<decltype(step)>, to_sint_t<decltype(step * x)>, decltype(step * x)>;
lx += T((W)rx * step), ly += T((W)ry * step), x = rx + lx, y = ry + ly;
}
// dir: true -> move to rchild; false -> move to lchil
template <class F>
requires requires(F f, bool dir, T step) { f(dir, step); }
static CEXP sbt walk_to(T x, T y, F&& f) NE {
sbt sbt;
bool dir = false;
while (y) {
T step = x / y;
if (dir = !dir, std::swap(x %= y, y); !y) --step;
if (!step) continue;
if (f(dir, step); dir) sbt.movr(step);
else sbt.movl(step);
}
return sbt;
}
static CEXP sbt walk_to(T x, T y) NE { return walk_to(x, y, fn_0); }
// dir: true -> move to rchild; false -> move to lchil
template <class F>
requires requires(F f, bool dir, T step) { f(dir, step); }
static CEXP sbt walk_to_lca(T x1, T y1, T x2, T y2, F&& f) NE {
sbt sbt;
bool dir = false;
while (y1 && y2) {
T step1 = x1 / y1, step2 = x2 / y2;
if (dir = !dir, std::swap(x1 %= y1, y1); !y1) --step1;
if (std::swap(x2 %= y2, y2); !y2) --step2;
if (T step = min(step1, step2); step) {
if (f(dir, step); dir) sbt.movr(step);
else sbt.movl(step);
}
if (step1 != step2) break;
}
return sbt;
}
static CEXP sbt walk_to_lca(T x1, T y1, T x2, T y2) NE { return walk_to_lca(x1, y1, x2, y2, fn_0); }
};
} // namespace tifa_libs
#line 2 "src/nt/sbt/lib.hpp"
#line 2 "src/util/consts/lib.hpp"
#line 2 "src/util/alias/num/lib.hpp"
#line 2 "src/util/util/lib.hpp"
// https://github.com/Tiphereth-A/CP-lib
#include <bits/extc++.h>
// clang-format off
namespace tifa_libs {
#define CEXP constexpr
#define CEXPE constexpr explicit
#define CR const&
#define CP const*
#define PC *const
#define CPC const*const
#define TPN typename
#define NE noexcept
#define CNE const noexcept
#define ND [[nodiscard]]
#define cT_(...) std::conditional_t<sizeof(__VA_ARGS__) <= sizeof(size_t) * 2, __VA_ARGS__, __VA_ARGS__ CR>
// NOLINTNEXTLINE(misc-const-correctness)
#define flt_(T, i, l, r, ...) for (T i = (l), i##e = (r)__VA_OPT__(, ) __VA_ARGS__; i < i##e; ++i)
#define retif_(cond, if_true, ...) if cond return if_true __VA_OPT__(; else return __VA_ARGS__)
#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif
using namespace std::ranges;
using namespace std::literals;
template <class T>
CEXP T abs(T x) NE { retif_((x < 0), -x, x); }
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/alias/num/lib.hpp"
// clang-format off
namespace tifa_libs {
#define mk0_(w, t) using w = t; using c##w = const t
#define mk_(w, t) mk0_(w, t); CEXP w operator""_##w(unsigned long long x) NE { return (w)x; }
mk_(i8, int8_t) mk_(u8, uint8_t) mk_(i16, int16_t) mk_(u16, uint16_t) mk_(i32, int32_t) mk_(u32, uint32_t) mk_(i64, int64_t) mk_(u64, uint64_t) mk_(isz, ssize_t) mk_(usz, size_t) mk_(chr, char) mk_(schr, signed char) mk_(uchr, unsigned char) mk_(sint, signed) mk_(uint, unsigned);
mk0_(i128, __int128_t); mk0_(u128, __uint128_t); mk0_(f32, float); mk0_(f64, double); mk0_(f128, long double);
#undef mk0_
#undef mk_
} // namespace tifa_libs
// clang-format on
#line 4 "src/util/consts/lib.hpp"
// clang-format off
namespace tifa_libs {
using std::numbers::pi_v;
template <std::floating_point FP>
inline FP eps_v = std::sqrt(std::numeric_limits<FP>::epsilon());
template <std::floating_point FP>
CEXP void set_eps(FP v) NE { eps_v<FP> = v; }
CEXP u32 TIME = ((__TIME__[0] & 15) << 20) | ((__TIME__[1] & 15) << 16) | ((__TIME__[3] & 15) << 12) | ((__TIME__[4] & 15) << 8) | ((__TIME__[6] & 15) << 4) | (__TIME__[7] & 15);
CEXP auto STR2U16 = [] { std::array<u32, 65536> table{}; table.fill(-1_u32); flt_ (u32, i, 48, 58) flt_ (u32, j, 48, 58) table[i << 8 | j] = (j & 15) * 10 + (i & 15); return table; }();
inline const auto fn_0 = [](auto&&...) NE {};
inline const auto fn_is0 = [](auto x) NE { return x == 0; };
} // namespace tifa_libs
// clang-format on
#line 2 "src/util/traits/math/lib.hpp"
// clang-format off
#line 4 "src/util/traits/math/lib.hpp"
namespace tifa_libs {
template <class T> concept char_c = std::same_as<T, char> || std::same_as<T, signed char> || std::same_as<T, unsigned char>;
#pragma GCC diagnostic ignored "-Wpedantic"
template <class T> concept s128_c = std::same_as<T, __int128_t> || std::same_as<T, __int128>;
template <class T> concept u128_c = std::same_as<T, __uint128_t> || std::same_as<T, unsigned __int128>;
template <class T> concept i128_c = s128_c<T> || u128_c<T>;
#pragma GCC diagnostic warning "-Wpedantic"
template <class T> concept imost64_c = std::integral<T> && sizeof(T) * __CHAR_BIT__ <= 64;
template <class T> concept smost64_c = imost64_c<T> && std::signed_integral<T>;
template <class T> concept umost64_c = imost64_c<T> && std::unsigned_integral<T>;
template <class T> concept int_c = i128_c<T> || imost64_c<T>;
template <class T> concept sint_c = s128_c<T> || smost64_c<T>;
template <class T> concept uint_c = u128_c<T> || umost64_c<T>;
template <class T> concept arithm_c = std::is_arithmetic_v<T> || int_c<T>;
template <class T> concept mint_c = requires(T x) { {x.mod()} -> uint_c; {x.val()} -> uint_c; };
template <class T> concept dft_c = requires(T x, std::vector<TPN T::data_t> v, u32 n) { {x.size()} -> std::same_as<u32>; x.bzr(n); x.dif(v, n); x.dit(v, n); };
template <class T> concept ntt_c = dft_c<T> && requires(T x) { T::max_size; T::G; };
template <class T> struct to_sint : std::make_signed<T> {};
template <> struct to_sint<u128> { using type = i128; };
template <> struct to_sint<i128> { using type = i128; };
template <class T> using to_sint_t = TPN to_sint<T>::type;
template <class T> struct to_uint : std::make_unsigned<T> {};
template <> struct to_uint<u128> { using type = u128; };
template <> struct to_uint<i128> { using type = u128; };
template <class T> using to_uint_t = TPN to_uint<T>::type;
template <arithm_c T> struct to_bigger : std::make_unsigned<T> {};
#define _(w,ww) template <> struct to_bigger<w> { using type = ww; }
#define _2(w,ww) _(i##w,i##ww); _(u##w,u##ww);
_2(8, 16); _2(16, 32); _2(32, 64); _2(64, 128); _(f32, f64); _(f64, f128);
#undef _2
#undef _
template <class T> using to_bigger_t = TPN to_bigger<T>::type;
template <arithm_c T> CEXP T inf_v = [] {
if CEXP(sint_c<T>) return T(to_uint_t<T>(-1) / 4 - 1);
else if CEXP(uint_c<T>) return T(-1) / 2 - 1;
else return std::numeric_limits<T>::max() / 2 - 1;
}();
} // namespace tifa_libs
// clang-format on
#line 5 "src/nt/sbt/lib.hpp"
namespace tifa_libs {
template <uint_c T>
struct sbt {
T x{1}, y{1}; // now
T lx{0}, ly{1}; // lbound
T rx{1}, ry{0}; // rbound
CEXP sbt() NE = default;
CEXP void movl(int_c auto step = 1) NE {
using W = std::conditional_t<sint_c<decltype(step)>, to_sint_t<decltype(step * x)>, decltype(step * x)>;
rx += T((W)lx * step), ry += T((W)ly * step), x = rx + lx, y = ry + ly;
}
CEXP void movr(int_c auto step = 1) NE {
using W = std::conditional_t<sint_c<decltype(step)>, to_sint_t<decltype(step * x)>, decltype(step * x)>;
lx += T((W)rx * step), ly += T((W)ry * step), x = rx + lx, y = ry + ly;
}
// dir: true -> move to rchild; false -> move to lchil
template <class F>
requires requires(F f, bool dir, T step) { f(dir, step); }
static CEXP sbt walk_to(T x, T y, F&& f) NE {
sbt sbt;
bool dir = false;
while (y) {
T step = x / y;
if (dir = !dir, std::swap(x %= y, y); !y) --step;
if (!step) continue;
if (f(dir, step); dir) sbt.movr(step);
else sbt.movl(step);
}
return sbt;
}
static CEXP sbt walk_to(T x, T y) NE { return walk_to(x, y, fn_0); }
// dir: true -> move to rchild; false -> move to lchil
template <class F>
requires requires(F f, bool dir, T step) { f(dir, step); }
static CEXP sbt walk_to_lca(T x1, T y1, T x2, T y2, F&& f) NE {
sbt sbt;
bool dir = false;
while (y1 && y2) {
T step1 = x1 / y1, step2 = x2 / y2;
if (dir = !dir, std::swap(x1 %= y1, y1); !y1) --step1;
if (std::swap(x2 %= y2, y2); !y2) --step2;
if (T step = min(step1, step2); step) {
if (f(dir, step); dir) sbt.movr(step);
else sbt.movl(step);
}
if (step1 != step2) break;
}
return sbt;
}
static CEXP sbt walk_to_lca(T x1, T y1, T x2, T y2) NE { return walk_to_lca(x1, y1, x2, y2, fn_0); }
};
} // namespace tifa_libs