// SPDX-License-Identifier: CC0-1.0
// 生成版: Clang・速度優先（入力検査を除外）。 所有・解放の省略は各APIの前提を参照。
// 生成元: DataStructure/line-container.cpp
// Original Source: https://nyaannyaan.github.io/library/data-structure/line-container.hpp.html
// Original Source: https://github.com/kth-competitive-programming/kactl/blob/main/content/data-structures/LineContainer.h
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/line_container.md
//
// API一覧
// [LineContainer<T,objective>・MinLineContainer<T>・MaxLineContainer<T>]
// add(k,m): y=k*x+mを追加、償却O(log h)。整数は符号付き64bit以下、浮動係数は有限値。
// query(x): 最適値をO(log h)で返す。空は検査時例外、省略時は非空が前提。整数範囲外は常に例外。
// query_optional(x): 空ならnullopt、それ以外はqueryと同じ。xは浮動小数点なら有限値。
// query_line(x): 選択した元の(k,m)をO(log h)で返す。同点の係数は未規定。
// query_line_optional(x): 空ならnullopt、それ以外はquery_lineと同じ。
// query_with_line(x): 1回の探索でResult{最適値,k,m}を返す。queryと同じ前提・O(log h)。
// query_with_line_optional(x): 空ならnullopt、それ以外はquery_with_lineと同じ。
// size()/empty()/clear(): 有効な包絡直線の本数・空判定・全削除、O(1)/O(1)/O(h)。
// begin()/end()/lower_bound(x): 元係数と交点を参照。最小化の傾き順は降順。直接変更は不変条件を壊す。
// insect(x,y): 旧互換の内部交点更新。正しい包絡iterator以外では呼び出さない。
// コピーは独立した包絡。hは有効直線数。浮動小数点版の交点・最適値には丸め誤差がある。
// objectiveはMINIMIZE/MAXIMIZE。MinLineContainer<T>/MaxLineContainer<T>は最小化/最大化の別名。
//
// [Line<T>・補助関数]
// Line<T>のk,m: 元の係数。p: この直線が選ばれる右端(整数は型の範囲に制限)。
// lc_inf<T>(): 整数では最大値、浮動小数点では正の無限大を返す。
// lc_div(a,b): 整数は床除算、浮動小数点は通常除算。0除算・整数結果の範囲外は例外。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <limits>
#include <optional>
#include <set>
#include <stdexcept>
#include <type_traits>
#include <utility>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_LINE_CONTAINER_DEFINED
#define SIRKIT_LINE_CONTAINER_DEFINED

enum Objective { MAXIMIZE = +1, MINIMIZE = -1 };

template <typename T>
struct Line {
  mutable T k, m, p;
  bool operator<(const Line& other) const { return k < other.k; }
  bool operator<(T x) const { return p < x; }
};

template <typename T>
constexpr T lc_inf() {
  if constexpr (std::is_floating_point_v<T>) return std::numeric_limits<T>::infinity();
  else return std::numeric_limits<T>::max();
}

template <typename T>
T lc_div(T a, T b) {
  if (b == 0) throw std::domain_error("lc_div: division by zero");
  if constexpr (std::is_integral_v<T>) {
    static_assert(std::is_signed_v<T>);
    if (a == std::numeric_limits<T>::min() && b == -1)
      throw std::overflow_error("lc_div: quotient overflow");
    return static_cast<T>(a / b - ((a < 0) != (b < 0) && a % b != 0));
  } else return a / b;
}

namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template <typename T, Objective objective>
struct LineComparator {
  using is_transparent = void;
  bool operator()(const Line<T>& a, const Line<T>& b) const {
    if constexpr (objective == MAXIMIZE) return a.k < b.k;
    else return a.k > b.k;
  }
  bool operator()(const Line<T>& a, T x) const { return a < x; }
  bool operator()(T x, const Line<T>& a) const { return x < a.p; }
};
}  // namespace reinsirk::detail

template <typename T, Objective objective>
struct LineContainer : std::multiset<Line<T>, /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::LineComparator<T, objective>> {
  static_assert((std::is_integral_v<T> && std::is_signed_v<T> && sizeof(T) <= 8) ||
                std::is_floating_point_v<T>);
  static_assert(objective == MAXIMIZE || objective == MINIMIZE);
  using super = std::multiset<Line<T>, /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::LineComparator<T, objective>>;
  using super::begin, super::end, super::insert, super::erase;
  using super::empty, super::lower_bound;
  static constexpr T inf = lc_inf<T>();
  struct Result { T value, k, m; };

 private:
  static bool boundary_less(const Line<T>& a, const Line<T>& b) {
    return a.p < b.p;
  }
  static bool dominated_at_boundary(const Line<T>& left, const Line<T>& candidate, const Line<T>& right)
      requires (std::is_integral_v<T> && std::numeric_limits<T>::digits <= 31 && objective == MINIMIZE) {
    // 積はint64_tに収まる。第2比較を厳密にして同点時の保持も維持する。
    using Wide = std::int64_t;
    const Wide boundary = left.p;
    const Wide first = (Wide(candidate.k) - Wide(left.k)) * boundary;
    const Wide second = (Wide(candidate.k) - Wide(right.k)) * (boundary + 1);
    return first >= Wide(left.m) - Wide(candidate.m) && second > Wide(right.m) - Wide(candidate.m);
  }
  template <bool known_order = false>
  static bool intersection(Line<T> const& a, Line<T> const& b) {
    if constexpr (std::is_integral_v<T>) {
      using U = std::make_unsigned_t<T>;
      const bool negative_n = b.m < a.m;
      const bool negative_d = [&] {
        return a.k < b.k;
      }();
      const U n = negative_n ? U(a.m) - U(b.m) : U(b.m) - U(a.m);
      const U d = negative_d ? U(b.k) - U(a.k) : U(a.k) - U(b.k);
      U q = n / d;
      constexpr U top = U(std::numeric_limits<T>::max());
      if (negative_n != negative_d) {
        q = static_cast<U>(q + U(n % d != 0));
        if (q >= top + U{1}) {
          a.p = std::numeric_limits<T>::min();
          return q > top + U{1};
        }
        else a.p = static_cast<T>(-static_cast<T>(q));
      } else a.p = q > top ? inf : static_cast<T>(q);
      return false;
    } else {
      const T n = b.m - a.m, d = a.k - b.k;
      if (std::isfinite(n + d) || (std::isfinite(n) && std::isfinite(d))) a.p = n / d;
      else if (!std::isfinite(n) && !std::isfinite(d))
        a.p = (b.m / 2 - a.m / 2) / (a.k / 2 - b.k / 2);
      else if (!std::isfinite(n)) a.p = ((b.m / 2 - a.m / 2) / d) * 2;
      else a.p = (n / (a.k / 2 - b.k / 2)) / 2;
      return false;
    }
  }
  __attribute__((always_inline))
  static T evaluate(T k, T m, T x) {
    if constexpr (std::is_floating_point_v<T>) return k * x + m;
    else if constexpr (sizeof(T) < 8) {
      const std::int64_t result = std::int64_t(k) * x + m;
      if (result < std::numeric_limits<T>::min() || result > std::numeric_limits<T>::max())
        throw std::overflow_error("LineContainer: value overflow");
      return static_cast<T>(result);
    } else {
#if defined(__SIZEOF_INT128__) && !defined(SIRKIT_LINE_CONTAINER_FORCE_PORTABLE)
      T fast;
      if (!__builtin_mul_overflow(k, x, &fast) && !__builtin_add_overflow(fast, m, &fast))
        [[likely]]
        return fast;
      __extension__ using Wide = __int128;
      const Wide result = Wide(k) * x + m;
      if (result < std::numeric_limits<T>::min() || result > std::numeric_limits<T>::max())
        throw std::overflow_error("LineContainer: value overflow");
      return static_cast<T>(result);
#else
      using U = std::uint64_t;
      // 32bit範囲なら積と和が64bitに収まる。
      constexpr U narrow_offset = U{1} << 31;
      if (((U(k) + narrow_offset) | (U(x) + narrow_offset) |
           (U(m) + narrow_offset)) < (U{1} << 32))
        return k * x + m;
      const U a = k < 0 ? U{0} - U(k) : U(k);
      const U b = x < 0 ? U{0} - U(x) : U(x);
      const U c = m < 0 ? U{0} - U(m) : U(m);
      constexpr U mask = 0xffffffffU;
      const U t0 = (a & mask) * (b & mask);
      const U t1 = (a >> 32) * (b & mask) + (t0 >> 32);
      const U t2 = (a & mask) * (b >> 32) + (t1 & mask);
      U hi = (a >> 32) * (b >> 32) + (t1 >> 32) + (t2 >> 32);
      U lo = (t2 << 32) | (t0 & mask);
      bool negative = (k < 0) != (x < 0);
      if (negative == (m < 0)) {
        const U previous = lo;
        lo += c;
        hi += lo < previous;
      } else if (hi != 0 || lo >= c) {
        const U previous = lo;
        lo -= c;
        hi -= previous < c;
      } else { lo = c - lo; negative = m < 0; }
      const U limit = U(std::numeric_limits<T>::max()) + U(negative);
      if (hi != 0 || lo > limit) throw std::overflow_error("LineContainer: value overflow");
      if (negative && lo == U(std::numeric_limits<T>::max()) + U{1})
        return std::numeric_limits<T>::min();
      return negative ? -static_cast<T>(lo) : static_cast<T>(lo);
#endif
    }
  }
  static void check_x(T x) {
    (void)x;
  }
  __attribute__((always_inline))
  typename super::const_iterator selected(T x) const {
    check_x(x);
    return lower_bound(x);
  }

  bool insect_impl(typename super::iterator x, typename super::iterator y, bool& below) {
    below = false;
    if (y == end()) { x->p = inf; return false; }
    if (x->k == y->k) {
      const bool better = objective == MAXIMIZE ? x->m > y->m : x->m < y->m;
      if constexpr (std::is_integral_v<T>) {
        x->p = better ? inf : std::numeric_limits<T>::min();
        below = !better;
      }
      else x->p = better ? inf : -inf;
    } else below = intersection(*x, *y);
    return !below && !boundary_less(*x, *y);
  }

 public:
  bool insect(typename super::iterator x, typename super::iterator y) {
    bool below;
    return insect_impl(x,y,below);
  }
  void add(T k, T m) {
    add_unchecked(k, m);
  }

 private:
  friend struct LineContainer2D;
  void add_unchecked(T k, T m) {
    if constexpr (std::is_integral_v<T>) {
      // 小さい包絡では、確保前に不要な直線を除く。
      if (super::size() <= 256) {
        // 型・目的ごとに分け、既存の64bit・最大化経路の生成コードを保つ。
        if constexpr (std::numeric_limits<T>::digits <= 31 && objective == MINIMIZE) {
          Line<T> candidate{k, m, inf};
          auto z = lower_bound(candidate);
          if (z != end() && z->k == k) {
            const bool better = objective == MAXIMIZE ? m > z->m : m < z->m;
            if (!better) return;
          }
          auto x = z;
          const bool has_previous = z != begin();
          if (has_previous) --x;
          if (has_previous && z != end() && dominated_at_boundary(*x, candidate, *z)) return;
          if (z != end() && z->k != k && intersection(candidate, *z)) return;
          Line<T> previous{};
          // 下端だけで選ばれる直線と、選択域が型の範囲外の直線を区別する。
          bool previous_below = false;
          if (has_previous) {
            previous = *x;
            previous_below = intersection(previous, candidate);
            // 整数順序では後続を削除できる候補はその後続の旧右端で前任より良い。
            if (!previous_below && previous.p >= candidate.p) return;
          }
          auto y = insert(z, candidate);
          while (z != end() && !boundary_less(*y, *z)) {
            z = erase(z);
            insect(y, z);
          }
          if (has_previous) {
            x->p = previous.p;
          } else x = y;
          while ((y = x) != begin() && !boundary_less(*--x, *y)) insect(x, erase(y));
          // floorが最小値の直線はその点で最適になり得る。真の域外だけ除く。
          if constexpr (std::is_integral_v<T>) {
            const auto first = begin();
            if (first->p == std::numeric_limits<T>::min()) [[unlikely]] {
              const auto next = std::next(first);
              // 末尾の上端は最大値なので、ここでは次の直線が存在する。
              bool first_below;
              insect_impl(first, next, first_below);
              if (first_below) erase(first);
            }
          }
          return;
        } else {
          Line<T> candidate{k, m, inf};
          auto z = lower_bound(candidate);
          if (z != end() && z->k == k) {
            const bool better = objective == MAXIMIZE ? m > z->m : m < z->m;
            if (!better) return;
          } else if (z != end() && intersection<true>(candidate, *z)) return;
          auto x = z;
          const bool has_previous = z != begin();
          Line<T> previous{};
          // 下端だけで選ばれる直線と、選択域が型の範囲外の直線を区別する。
          bool previous_below = false;
          if (has_previous) {
            --x;
            previous = *x;
            previous_below = intersection<true>(previous, candidate);
            // 整数順序では後続を削除できる候補はその後続の旧右端で前任より良い。
            if (!previous_below && previous.p >= candidate.p) return;
          }
          auto y = insert(z, candidate);
          while (z != end() && !boundary_less(*y, *z)) {
            z = erase(z);
            insect(y, z);
          }
          if (has_previous) {
            x->p = previous.p;
          } else x = y;
          while ((y = x) != begin() && !boundary_less(*--x, *y)) insect(x, erase(y));
          // floorが最小値の直線はその点で最適になり得る。真の域外だけ除く。
          if constexpr (std::is_integral_v<T>) {
            const auto first = begin();
            if (first->p == std::numeric_limits<T>::min()) [[unlikely]] {
              const auto next = std::next(first);
              // 末尾の上端は最大値なので、ここでは次の直線が存在する。
              bool first_below;
              insect_impl(first, next, first_below);
              if (first_below) erase(first);
            }
          }
          return;
        }
      }
    }
    auto z = insert({k, m, 0}), y = z++, x = y;
    bool below;
    while (insect_impl(y, z, below)) z = erase(z);
    if constexpr (std::is_integral_v<T>)
      if (below) { erase(y); return; }
    if (x != begin() && insect(--x, y)) insect(x, y = erase(y));
    while ((y = x) != begin() && !boundary_less(*--x, *y)) insect(x, erase(y));
    // floorが最小値の直線はその点で最適になり得る。真の域外だけ除く。
    if constexpr (std::is_integral_v<T>) {
      const auto first = begin();
      if (first->p == std::numeric_limits<T>::min()) [[unlikely]] {
        const auto next = std::next(first);
        bool first_below;
        insect_impl(first, next, first_below);
        if (first_below) erase(first);
      }
    }
  }
 public:
  __attribute__((always_inline))
  T query(T x) const {
    if constexpr (std::is_integral_v<T>) {
      const auto first = begin();
      if (x <= first->p) [[unlikely]] return evaluate(first->k, first->m, x);
      const auto line = lower_bound(x);
      return evaluate(line->k, line->m, x);
    } else {
      const auto line = selected(x);
      return evaluate(line->k, line->m, x);
    }
  }
  std::optional<T> query_optional(T x) const {
    check_x(x);
    if (empty()) return std::nullopt;
    const auto line = lower_bound(x);
    return evaluate(line->k, line->m, x);
  }
  std::pair<T,T> query_line(T x) const {
    const auto line = selected(x);
    return {line->k, line->m};
  }
  std::optional<std::pair<T,T>> query_line_optional(T x) const {
    check_x(x);
    if (empty()) return std::nullopt;
    const auto line = lower_bound(x);
    return std::pair<T,T>{line->k, line->m};
  }
  __attribute__((always_inline))
  Result query_with_line(T x) const {
    const auto line = selected(x);
    return {evaluate(line->k, line->m, x), line->k, line->m};
  }
  __attribute__((always_inline))
  std::optional<Result> query_with_line_optional(T x) const {
    check_x(x);
    if (empty()) return std::nullopt;
    const auto line = lower_bound(x);
    return Result{evaluate(line->k, line->m, x), line->k, line->m};
  }
};
template <typename T> using MinLineContainer = LineContainer<T, MINIMIZE>;
template <typename T> using MaxLineContainer = LineContainer<T, MAXIMIZE>;
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
