Tifa's CP Library

:heavy_check_mark: src/test_cpverifier/library-checker/stern_brocot_tree.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/stern_brocot_tree"

#include "../../code/io/ios_pair.hpp"
#include "../../code/nt/sbt.hpp"

using sbt = tifa_libs::math::SBT<i64>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  while (n--) {
    strn s;
    std::cin >> s;
    u64 k;
    i64 a, b, c, d;
    switch (s.front()) {
      case 'E':
        std::cin >> a >> b;
        {
          auto res = sbt(a, b).path();
          std::cout << res.size();
          for (i64 i : res) std::cout << ' ' << (i < 0 ? 'L' : 'R') << ' ' << (i < 0 ? -i : i);
          std::cout << '\n';
        }
        break;
      case 'D':
        std::cin >> k;
        {
          sbt s;
          char ch;
          for (i64 i = 0, _; i < (i64)k; ++i) {
            std::cin >> ch >> _;
            ch == 'R' ? s.movr(_) : s.movl(_);
          }
          std::cout << s.current() << '\n';
        }
        break;
      case 'L':
        std::cin >> a >> b >> c >> d;
        std::cout << sbt::lca(sbt(a, b), sbt(c, d)).current() << '\n';
        break;
      case 'A':
        std::cin >> d >> a >> b;
        {
          auto _ = sbt(a, b);
          if (_.dep() - d < 0) {
            std::cout << "-1\n";
            break;
          }
          _.movf(_.dep() - d);
          std::cout << _.current() << '\n';
        }
        break;
      case 'R':
        std::cin >> a >> b;
        {
          auto _ = sbt(a, b);
          std::cout << _.lbound() << ' ' << _.rbound() << '\n';
        }
        break;
    }
  }
  return 0;
}
#line 1 "src/test_cpverifier/library-checker/stern_brocot_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/stern_brocot_tree"

#line 1 "src/code/io/ios_pair.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/io/ios_pair.hpp"

template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { return is >> p.first >> p.second; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, std::pair<T, U> const &p) { return os << p.first << ' ' << p.second; }


#line 1 "src/code/nt/sbt.hpp"



#line 1 "src/code/nt/gcd.hpp"



#line 1 "src/code/util/traits.hpp"



#line 5 "src/code/util/traits.hpp"

namespace tifa_libs {

template <class T>
concept iterable_c = requires(T v) {
  { v.begin() } -> std::same_as<typename T::iterator>;
  { v.end() } -> std::same_as<typename T::iterator>;
};

template <class T>
concept container_c = iterable_c<T> && !std::derived_from<T, std::basic_string<typename T::value_type>>;

template <class T>
constexpr bool is_char_v = std::is_same_v<T, char> || std::is_same_v<T, signed char> || std::is_same_v<T, unsigned char>;
template <class T>
concept char_c = is_char_v<T>;

template <class T>
constexpr bool is_s128_v = std::is_same_v<T, __int128_t> || std::is_same_v<T, __int128>;
template <class T>
concept s128_c = is_s128_v<T>;

template <class T>
constexpr bool is_u128_v = std::is_same_v<T, __uint128_t> || std::is_same_v<T, unsigned __int128>;
template <class T>
concept u128_c = is_u128_v<T>;

template <class T>
constexpr bool is_i128_v = is_s128_v<T> || is_u128_v<T>;
template <class T>
concept i128_c = is_u128_v<T>;

template <class T>
constexpr bool is_int_v = std::is_integral_v<T> || is_i128_v<T>;
template <class T>
concept int_c = is_int_v<T>;

template <class T>
constexpr bool is_sint_v = is_s128_v<T> || (is_int_v<T> && std::is_signed_v<T>);
template <class T>
concept sint_c = is_sint_v<T>;

template <class T>
constexpr bool is_uint_v = is_u128_v<T> || (is_int_v<T> && std::is_unsigned_v<T>);
template <class T>
concept uint_c = is_uint_v<T>;

template <class T>
concept mint_c = requires(T x) {
  { x.mod() } -> uint_c;
  { x.val() } -> uint_c;
};

template <class T>
constexpr bool is_arithm_v = std::is_arithmetic_v<T> || is_int_v<T>;
template <class T>
concept arithm_c = is_arithm_v<T>;

template <class T>
struct to_sint : std::make_signed<T> {};
template <>
struct to_sint<u128> {
  using type = u128;
};
template <>
struct to_sint<i128> {
  using type = u128;
};
template <class T>
using to_sint_t = typename 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 = typename to_uint<T>::type;

}  // namespace tifa_libs


#line 5 "src/code/nt/gcd.hpp"

namespace tifa_libs::math {

namespace gcd_impl_ {
template <uint_c T, uint_c U>
constexpr std::common_type_t<T, U> gcd__(T u, U v) {
  using W = std::common_type_t<T, U>;
  if (!u || !v) return u ^ v;
  auto k = std::__countr_zero(u | v);
  u >>= k, v >>= k;
  do {
    if (W _ = v >> std::__countr_zero(v); u > _) v = u - _, u = _;
    else v = _ - u;
  } while (v);
  return u << k;
}
}  // namespace gcd_impl_

template <int_c T, int_c U>
constexpr std::common_type_t<T, U> gcd(T a, U b) { return gcd_impl_::gcd__((to_uint_t<T>)abs(a), (to_uint_t<U>)abs(b)); }

}  // namespace tifa_libs::math


#line 5 "src/code/nt/sbt.hpp"

namespace tifa_libs::math {

// x / y (x > 0, y > 0). default 1 / 1
template <std::signed_integral T>
class SBT {
  T lx, ly, x, y, rx, ry;
  vec<T> seq;

 public:
  explicit constexpr SBT() : lx(0), ly(1), x(1), y(1), rx(1), ry(0) {}
  constexpr SBT(T X, T Y) : SBT() {
    assert(x > 0 && Y > 0);
    if (T g = gcd(X, Y); g > 1) X /= g, Y /= g;
    while (std::min(X, Y))
      if (X > Y) {
        T _ = X / Y;
        movr(_ - !(X -= _ * Y));
      } else {
        T _ = Y / X;
        movl(_ - !(Y -= _ * X));
      }
  }
  explicit constexpr SBT(vec<T> const &seq_) : SBT() {
    for (auto &&d : seq_) {
      assert(d != 0);
      if (d > 0) movr(d);
      if (d < 0) movl(d);
    }
  }

  friend constexpr auto operator<=>(SBT const &l, SBT const &r) { return l.x * r.y - r.x * l.y; }
  friend constexpr bool operator==(SBT const &l, SBT const &r) { return l.x == r.x && l.y == r.y; }

  constexpr ptt<T> current() const { return {x, y}; }
  constexpr ptt<T> lbound() const { return {lx, ly}; }
  constexpr ptt<T> rbound() const { return {rx, ry}; }
  // path from (1, 1) to @current(). rchild be positive and vice versa
  constexpr vec<T> path() const { return seq; }

  constexpr T dep() const {
    T res = 0;
    for (auto &&s : seq) res += abs(s);
    return res;
  }
  // move towards lchild with @d steps
  constexpr void movl(T d = 1) {
    if (d <= 0) return;
    if (seq.empty() || seq.back() > 0) seq.push_back(0);
    seq.back() -= d;
    rx += lx * d, ry += ly * d;
    x = rx + lx, y = ry + ly;
  }
  // move towards rchild with @d steps
  constexpr void movr(T d = 1) {
    if (d <= 0) return;
    if (seq.empty() || seq.back() < 0) seq.push_back(0);
    seq.back() += d;
    lx += rx * d, ly += ry * d;
    x = rx + lx, y = ry + ly;
  }
  // move towards fa with @d steps
  // @return true if succeed, or false if falied
  constexpr bool movf(T d = 1) {
    if (d <= 0) return true;
    while (d) {
      if (seq.empty()) return false;
      T _ = std::min(d, abs(seq.back()));
      if (seq.back() > 0) {
        x -= rx * _, y -= ry * _;
        lx = x - rx, ly = y - ry;
        seq.back() -= _;
      } else {
        x -= lx * _, y -= ly * _;
        rx = x - lx, ry = y - ly;
        seq.back() += _;
      }
      d -= _;
      if (!seq.back()) seq.pop_back();
      if (!_) break;
    }
    return true;
  }

  static constexpr SBT lca(SBT const &l, SBT const &r) {
    SBT ret;
    for (u32 i = 0; i < std::min((u32)l.seq.size(), (u32)r.seq.size()); ++i) {
      T val1 = l.seq[i], val2 = r.seq[i];
      if ((val1 < 0) != (val2 < 0)) break;
      if (val1 < 0) ret.movl(std::min(-val1, -val2));
      if (val1 > 0) ret.movr(std::min(val1, val2));
      if (val1 != val2) break;
    }
    return ret;
  }
};

}  // namespace tifa_libs::math


#line 5 "src/test_cpverifier/library-checker/stern_brocot_tree.test.cpp"

using sbt = tifa_libs::math::SBT<i64>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  while (n--) {
    strn s;
    std::cin >> s;
    u64 k;
    i64 a, b, c, d;
    switch (s.front()) {
      case 'E':
        std::cin >> a >> b;
        {
          auto res = sbt(a, b).path();
          std::cout << res.size();
          for (i64 i : res) std::cout << ' ' << (i < 0 ? 'L' : 'R') << ' ' << (i < 0 ? -i : i);
          std::cout << '\n';
        }
        break;
      case 'D':
        std::cin >> k;
        {
          sbt s;
          char ch;
          for (i64 i = 0, _; i < (i64)k; ++i) {
            std::cin >> ch >> _;
            ch == 'R' ? s.movr(_) : s.movl(_);
          }
          std::cout << s.current() << '\n';
        }
        break;
      case 'L':
        std::cin >> a >> b >> c >> d;
        std::cout << sbt::lca(sbt(a, b), sbt(c, d)).current() << '\n';
        break;
      case 'A':
        std::cin >> d >> a >> b;
        {
          auto _ = sbt(a, b);
          if (_.dep() - d < 0) {
            std::cout << "-1\n";
            break;
          }
          _.movf(_.dep() - d);
          std::cout << _.current() << '\n';
        }
        break;
      case 'R':
        std::cin >> a >> b;
        {
          auto _ = sbt(a, b);
          std::cout << _.lbound() << ' ' << _.rbound() << '\n';
        }
        break;
    }
  }
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-12 edge_decode_00 :heavy_check_mark: AC 41 ms 4 MB
g++-12 edge_encode_00 :heavy_check_mark: AC 467 ms 4 MB
g++-12 edge_lca_00 :heavy_check_mark: AC 207971 ms 4 MB
g++-12 edge_range_00 :heavy_check_mark: AC 453 ms 4 MB
g++-12 example_00 :heavy_check_mark: AC 7 ms 4 MB
g++-12 hand_00 :heavy_check_mark: AC 7 ms 4 MB
g++-12 hand_01 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_ancestor_no_00 :heavy_check_mark: AC 57 ms 4 MB
g++-12 random_ancestor_yes_00 :heavy_check_mark: AC 68 ms 4 MB
g++-12 random_decode_00 :heavy_check_mark: AC 114 ms 4 MB
g++-12 random_encode_00 :heavy_check_mark: AC 142 ms 4 MB
g++-12 random_lca_00 :heavy_check_mark: AC 101 ms 4 MB
g++-12 random_range_00 :heavy_check_mark: AC 76 ms 4 MB
g++-12 small_ancestor_00 :heavy_check_mark: AC 31 ms 4 MB
g++-12 small_decode_00 :heavy_check_mark: AC 51 ms 4 MB
g++-12 small_encode_00 :heavy_check_mark: AC 62 ms 4 MB
g++-12 small_lca_00 :heavy_check_mark: AC 40 ms 4 MB
g++-12 small_range_00 :heavy_check_mark: AC 45 ms 4 MB
Back to top page