競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
#define SKIP_RUNTIME_CHECKS
// Verify: https://judge.yosupo.jp/problem/line_add_get_min
// Submission: https://judge.yosupo.jp/submission/417304
// Library: DataStructure/line-container-2d.cpp
// SPDX-License-Identifier: CC0-1.0
// Original Source: https://nyaannyaan.github.io/library/data-structure/line-container-2d.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_2d.md
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/line_container.md
//
// API一覧
// [LineContainer2D]
// LineContainer2D(inf=2^60): O(1)で空表を作る。infは正のint64_t。infinity(): 設定値をO(1)で返す。
// add(x,y): 有限の点を追加、償却O(log h)。確保失敗時は空に戻して例外を伝える。
// max_ld(a,b)/min_ld(a,b): ax+byの最大/最小をO(log h)で返す。有限係数・非空が前提。
// max_ld/min_ld(整数,整数): 値域がint64_t内の整数型は、型から保証できる有限性・比の検査を省く。
// b/aが浮動型の範囲を超える問い合わせは常にoverflow_error。
// max_ll(a,b)/min_ll(a,b): 浮動計算を±infに制限し、0方向へ切り捨てる。O(log h)。
// max_rounded_ll(a,b)/min_rounded_ll(a,b): 同じ範囲制限後、半整数は0から遠い方へ丸める。
// max_optional(a,b)/min_optional(a,b): 空ならnullopt、それ以外はld版と同じ。
// argmax(a,b)/argmin(a,b): 最適な元の点(x,y)をO(log h)で返す。同点の点は未規定。
// max_with_point(a,b)/min_with_point(a,b): 1回の探索でResult{内積値,x,y}を返す。ld係数・非空、O(log h)。
// argmax_optional(a,b)/argmin_optional(a,b): 空ならnullopt、それ以外はarg版と同じ。
// size()/empty()/clear(): 登録点数(重複込み)・空判定・全削除、O(1)/O(1)/O(h)。
// swap(other): 2コンテナの全状態をO(1)で交換する。コピーは独立、move後の元は空。
// コピーはO(h)、moveはO(1)。コピー・移動・clearでもinfを保持し、swapでは設定も交換する。
// 公開のminlc/maxlc・座標範囲は参照用。外からの変更は不変条件を壊す。
// SIRKIT_LINE_CONTAINER_STANDARD_HINTS: 座標を複製しない標準の探索経路を使う。
// long doubleの丸め誤差があり、ll版も厳密な整数内積ではない。hは包絡に残る点数。
// 既定の丸めモード・型本来の精度が前提。ゼロの符号は未規定。
//
// [同梱: LineContainer<T,objective>]
// 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 = [&] {
#if defined(__GNUC__) && !defined(__clang__)
if constexpr (known_order && sizeof(T) == 8 && objective == MINIMIZE) return false;
#endif
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;
}
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((always_inline))
#endif
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))
#if defined(__clang__)
[[likely]]
#endif
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) {
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (std::is_floating_point_v<T>)
if (!std::isfinite(x)) throw std::invalid_argument("LineContainer: nonfinite x");
#else
(void)x;
#endif
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((always_inline))
#endif
typename super::const_iterator selected(T x) const {
check_x(x);
#ifndef SKIP_RUNTIME_CHECKS
if (empty()) throw std::out_of_range("LineContainer: empty query");
#endif
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) {
#ifndef SKIP_RUNTIME_CHECKS
if constexpr (std::is_floating_point_v<T>)
if (!std::isfinite(k) || !std::isfinite(m))
throw std::invalid_argument("LineContainer: nonfinite coefficient");
#endif
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:
#if defined(__GNUC__) || defined(__clang__)
__attribute__((always_inline))
#endif
T query(T x) const {
if constexpr (std::is_integral_v<T>) {
#ifndef SKIP_RUNTIME_CHECKS
if (empty()) throw std::out_of_range("LineContainer: empty query");
#endif
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};
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((always_inline))
#endif
Result query_with_line(T x) const {
const auto line = selected(x);
return {evaluate(line->k, line->m, x), line->k, line->m};
}
#if defined(__GNUC__) || defined(__clang__)
__attribute__((always_inline))
#endif
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
#ifndef SIRKIT_LINE_CONTAINER_2D_DEFINED
#define SIRKIT_LINE_CONTAINER_2D_DEFINED
struct LineContainer2D {
using ld = long double;
using ll = std::int64_t;
struct Result { ld value, x, y; };
static constexpr ld kInf = static_cast<ld>(std::int64_t{1} << 60);
static constexpr ll INTEGER_LIMIT = 4'100'000'000'000'000'000;
ld Xmax = -std::numeric_limits<ld>::infinity();
ld Xmin = std::numeric_limits<ld>::infinity();
ld Ymax = -std::numeric_limits<ld>::infinity();
ld Ymin = std::numeric_limits<ld>::infinity();
MinLineContainer<ld> minlc;
MaxLineContainer<ld> maxlc;
private:
ll inf_ = kInf;
std::size_t count_ = 0;
// 照会では状態を変えず、包絡が変わった追加後にだけ更新する。
// 算術の代替指定と、処理系に合わせたキャッシュ選択を分ける。
#if !defined(SIRKIT_LINE_CONTAINER_STANDARD_HINTS) && (defined(__clang__) || defined(__GNUC__))
static constexpr bool cache_points = true;
#else
static constexpr bool cache_points = false;
#endif
struct NoCache {};
struct HintEntry {
const Line<ld>* line = nullptr;
const Line<ld>* previous = nullptr;
};
struct CachedHintEntry : HintEntry { Line<ld> cached{}; };
struct AdjacentLines {
const Line<ld>* before = nullptr;
const Line<ld>* after = nullptr;
};
struct SearchHint {
using Entry = std::conditional_t<cache_points, CachedHintEntry, HintEntry>;
Entry positive, negative;
// 0: 別区間、1/2: 全包絡、3: 共通区間、4/5: 境界が+1/-1の2本。
unsigned small = 0;
[[no_unique_address]] std::conditional_t<cache_points, AdjacentLines, NoCache> adjacent{};
[[no_unique_address]] std::conditional_t<cache_points, Line<ld>, NoCache> after_cached{};
} min_hint_, max_hint_;
template <class Hull, class Hint>
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
static void refresh_hint(const Hull& hull, Hint& hint) {
if constexpr (cache_points) {
if (hull.empty()) { hint = {}; return; }
hint.small = 0;
hint.positive.previous = nullptr;
hint.negative.line = nullptr;
hint.negative.previous = nullptr;
hint.adjacent = {};
} else {
hint = {};
if (hull.empty()) return;
}
if (hull.size() <= 2) {
// 2本なら先頭の右端は有限なので、+inf検索が必ず末尾を選ぶ。
const auto first = hull.begin();
hint.positive.line = &*first;
hint.negative.line = hull.size() == 2 ? &*hull.lower_bound(lc_inf<ld>()) : &*first;
if constexpr (cache_points) {
// 1/2本では境界も更新する。3の境界には木の値を使う。
{ hint.positive.cached.k = (*hint.positive.line).k; hint.positive.cached.m = (*hint.positive.line).m; hint.positive.cached.p = (*hint.positive.line).p; }
{ hint.negative.cached.k = (*hint.negative.line).k; hint.negative.cached.m = (*hint.negative.line).m; }
}
hint.small = static_cast<unsigned>(hull.size());
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
if constexpr (cache_points && std::numeric_limits<ld>::radix == 2 &&
std::numeric_limits<ld>::is_iec559 &&
std::is_same_v<Hull, MinLineContainer<ld>>) {
if (hint.small == 2) {
if (std::abs(hint.positive.cached.p) == ld{1})
hint.small = std::signbit(hint.positive.cached.p) ? 5 : 4;
}
}
#endif
return;
}
const auto positive = hull.lower_bound(ld{0.5});
hint.positive.line = &*positive;
if constexpr (cache_points) { hint.positive.cached.k = (*positive).k; hint.positive.cached.m = (*positive).m; }
hint.positive.previous = positive == hull.begin() ? nullptr : &*std::prev(positive);
if (!hint.positive.previous || hint.positive.previous->p < ld{-0.5}) {
hint.small = 3;
if constexpr (cache_points) {
if (positive != hull.begin()) {
const auto prev = std::prev(positive);
hint.adjacent.before = prev == hull.begin() ? nullptr : &*std::prev(prev);
{ hint.negative.cached.k = (*prev).k; hint.negative.cached.m = (*prev).m; }
}
const auto next = std::next(positive);
hint.adjacent.after = next == hull.end() ? nullptr : &*next;
if (hint.adjacent.after) { hint.after_cached.k = (*hint.adjacent.after).k; hint.after_cached.m = (*hint.adjacent.after).m; }
}
return;
}
const auto negative = hull.lower_bound(ld{-0.5});
hint.negative.line = &*negative;
if constexpr (cache_points) { hint.negative.cached.k = (*negative).k; hint.negative.cached.m = (*negative).m; }
hint.negative.previous = negative == hull.begin() ? nullptr : &*std::prev(negative);
}
void refresh_hints() {
refresh_hint(minlc, min_hint_);
refresh_hint(maxlc, max_hint_);
}
template <bool use_shared = false, bool copy = false, class Hull, class Hint>
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
static std::conditional_t<copy, Line<ld>, const Line<ld>&> hinted_line(const Hull& hull, const Hint& hint, ld c) {
if (hint.small == 3) {
const auto& entry = hint.positive;
if constexpr (cache_points) {
// 中央の前後も、同じ境界を再利用して判定する。
if (c <= entry.line->p) {
if (entry.previous && c <= entry.previous->p) {
if (!hint.adjacent.before || hint.adjacent.before->p < c) return hint.negative.cached;
} else return entry.cached;
} else if (hint.adjacent.after && c <= hint.adjacent.after->p) return hint.after_cached;
} else if constexpr (use_shared) {
if ((!entry.previous || entry.previous->p < c) && c <= entry.line->p) return *entry.line;
}
return *hull.lower_bound(c);
}
if (hint.small) {
if constexpr (cache_points)
return hint.positive.cached.p < c ? hint.negative.cached : hint.positive.cached;
else return hint.positive.line->p < c ? *hint.negative.line : *hint.positive.line;
}
const auto& entry = c >= 0 ? hint.positive : hint.negative;
if ((!entry.previous || entry.previous->p < c) && c <= entry.line->p) {
if constexpr (cache_points) return entry.cached;
else return *entry.line;
}
return *hull.lower_bound(c);
}
// 不要な追加を確保前に棄却し、照会キャッシュの更新要否を返す。
template <Objective objective, class Hint>
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
static bool add_hull(LineContainer<ld, objective>& hull, ld k, ld m, const Hint& hint) {
using Hull = LineContainer<ld, objective>;
Line<ld> candidate{k, m, lc_inf<ld>()};
auto z = hull.lower_bound(candidate);
if (z != hull.end() && z->k == k) {
const bool better = objective == MAXIMIZE ? m > z->m : m < z->m;
if (!better) return false;
}
auto x = z;
bool has_previous = z != hull.begin();
Line<ld> previous{};
if (z != hull.end()) {
if (z->k == k) candidate.p = lc_inf<ld>();
else Hull::intersection(candidate, *z);
}
if (has_previous) {
--x;
previous = *x;
Hull::intersection(previous, candidate);
if (z == hull.end() || candidate.p < z->p) {
if (previous.p >= candidate.p) return false;
}
}
// 共通区間の外だけが変わる場合は、既存の点・境界参照を保つ。
bool refresh = true;
ld lower = 0, upper = 0;
if constexpr (cache_points) {
if (hint.small == 3) {
const auto* first = hint.adjacent.before;
const auto* last = hint.adjacent.after ? hint.adjacent.after : hint.positive.line;
lower = first ? first->k : (objective == MAXIMIZE ? -lc_inf<ld>() : lc_inf<ld>());
upper = last->k;
refresh = objective == MAXIMIZE ? lower <= k && k <= upper : upper <= k && k <= lower;
}
} else (void)hint;
const auto mark_removed = [&](const Line<ld>& line) {
if constexpr (cache_points) {
if (!refresh) refresh = objective == MAXIMIZE ? lower <= line.k && line.k <= upper : upper <= line.k && line.k <= lower;
}
};
auto y = hull.insert(z, candidate);
bool removed_successor = false, changed = true;
while (z != hull.end() && !Hull::boundary_less(*y, *z)) {
mark_removed(*z);
z = hull.erase(z);
removed_successor = true;
hull.insect(y, z);
}
if (has_previous) {
x->p = previous.p;
if (!Hull::boundary_less(*x, *y)) {
mark_removed(*y);
hull.insect(x, y = hull.erase(y));
changed = removed_successor;
}
} else x = y;
while ((y = x) != hull.begin() && !Hull::boundary_less(*--x, *y)) {
mark_removed(*y);
hull.insect(x, hull.erase(y));
changed = true;
}
return changed && refresh;
}
static void check_coefficients(ld a, ld b) {
#ifndef SKIP_RUNTIME_CHECKS
if (!std::isfinite(a) || !std::isfinite(b))
throw std::invalid_argument("LineContainer2D: nonfinite coefficient");
#else
(void)a; (void)b;
#endif
}
#if (defined(__GNUC__) || defined(__clang__))
[[gnu::noinline, gnu::cold]]
#endif
static ld recover_line_dot(ld a, ld b, const Line<ld>* line) {
// 巨大な有限積同士の相殺でもinf-infを返さない。
const ld x = line->m, y = line->k;
int ea = 0, eb = 0, ex = 0, ey = 0;
std::frexp(a, &ea); std::frexp(b, &eb);
std::frexp(x, &ex); std::frexp(y, &ey);
const int e1 = std::max(ea, eb), e2 = std::max(ex, ey);
const ld sa = std::ldexp(a, -e1), sb = std::ldexp(b, -e1);
const ld sx = std::ldexp(x, -e2), sy = std::ldexp(y, -e2);
return std::ldexp(sa * sx + sb * sy, e1 + e2);
}
ll to_integer(ld value) const {
// 浮動型がint64_t最大値を2^63へ丸める環境でも、範囲外の整数変換をしない。
if (value >= static_cast<ld>(inf_)) return inf_;
if (value <= -static_cast<ld>(inf_)) return -inf_;
return static_cast<ll>(value);
}
ll to_rounded_integer(ld value) const {
if (value >= static_cast<ld>(inf_)) return inf_;
if (value <= -static_cast<ld>(inf_)) return -inf_;
const ll integral = static_cast<ll>(value);
const ld fraction = value - ld(integral);
return integral + (fraction >= 0.5L) - (fraction <= -0.5L);
}
std::pair<ld, ld> select(ld a, ld b) const {
if (a == 0) {
check_coefficients(a, b);
const auto it = b > 0 ? std::prev(maxlc.end()) : maxlc.begin();
return {it->m, it->k};
}
if (b == 0) {
check_coefficients(a, b);
const auto& line = a > 0 ? *maxlc.lower_bound(ld{0}) : *minlc.lower_bound(ld{0});
return {line.m, line.k};
}
return select_nonzero(a, b);
}
[[noreturn]] static void slope_error(ld a, ld b) {
check_coefficients(a, b);
throw std::overflow_error("LineContainer2D: query slope overflow");
}
std::pair<ld, ld> select_nonzero(ld a, ld b) const {
const ld c = b / a;
#ifndef SKIP_RUNTIME_CHECKS
// 有限aと有限b / aからbの有限性も分かる。bを重ねて検査しない。
if (!std::isfinite(a) || !std::isfinite(c)) [[unlikely]] slope_error(a, b);
#else
if (!std::isfinite(c)) [[unlikely]] slope_error(a, b);
#endif
const auto& line = a > 0 ? hinted_line(maxlc, max_hint_, c) : hinted_line(minlc, min_hint_, c);
return {line.m, line.k};
}
#if (defined(__GNUC__) || defined(__clang__))
[[gnu::always_inline]]
#endif
void check_empty() const {
#ifndef SKIP_RUNTIME_CHECKS
if (empty()) throw std::out_of_range("LineContainer2D: empty query");
#endif
}
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
[[gnu::noinline, gnu::cold]]
static void check_small_ratio(ld a, ld b) {
if (!std::isfinite(b / a)) slope_error(a, b);
}
#endif
template <bool proof, bool negative_a = false, bool optimized_float = false, class Hull, class Hint>
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
static ld nonzero_value(const Hull& hull, const Hint& hint, ld a, ld b) {
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
// 単位境界の比較では除算を省き、小さいaの商溢れだけ別途確認する。
if constexpr (optimized_float && !proof && cache_points &&
std::numeric_limits<ld>::radix == 2 && std::numeric_limits<ld>::is_iec559 &&
std::is_same_v<Hull, MinLineContainer<ld>>) {
if (hint.small >= 4) {
if (std::abs(a) < ld{1}) [[unlikely]] check_small_ratio(a, b);
const ld boundary = hint.small == 4 ? a : -a;
const bool right = negative_a ? b < boundary : b > boundary;
const auto l = right ? hint.negative.cached : hint.positive.cached;
const ld result = a * l.m + b * l.k;
if (std::isnan(result)) [[unlikely]] return recover_line_dot(a, b, &l);
return result;
}
}
#endif
const ld c = b / a;
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
const auto& l = hinted_line<(cache_points || proof), optimized_float && !proof>(hull,hint,c);
#else
const auto& l = hinted_line<(cache_points || proof)>(hull, hint, c);
#endif
const ld result = a * l.m + b * l.k;
// 有限cと有限resultならa, bも有限。例外経路だけ個別検査する。
if constexpr (proof) {
if (std::isnan(result)) [[unlikely]] return recover_line_dot(a, b, &l);
} else if constexpr (cache_points) {
if (!(std::abs(c + result) <= std::numeric_limits<ld>::max())) [[unlikely]]
return checked_result(a, b, c, result, &l);
} else if (!std::isfinite(c + result)) [[unlikely]]
return checked_result(a, b, c, result, &l);
return result;
}
template <bool ratio_is_finite = false, bool optimized_float = false>
#if (defined(__GNUC__) || defined(__clang__))
[[gnu::always_inline]]
#endif
ld value(ld a, ld b) const {
if (a > 0) {
if (b == 0) [[unlikely]] {
if constexpr (!ratio_is_finite) check_coefficients(a, b);
return a * Xmax;
}
return nonzero_value<ratio_is_finite, false, optimized_float>(maxlc, max_hint_, a, b);
}
if (a < 0) {
if (b == 0) [[unlikely]] {
if constexpr (!ratio_is_finite) check_coefficients(a, b);
return a * Xmin;
}
return nonzero_value<ratio_is_finite, true, optimized_float>(minlc, min_hint_, a, b);
}
if constexpr (!ratio_is_finite) check_coefficients(a, b);
return b == 0 ? 0 : b * (b > 0 ? Ymax : Ymin);
}
#if defined(__clang__)
template <bool ratio_is_finite = false, bool optimized_float = false>
#if (defined(__GNUC__) || defined(__clang__))
[[gnu::always_inline]]
#endif
ld minimum_value(ld a, ld b) const {
if (a > 0) {
if (b == 0) [[unlikely]] {
if constexpr (!ratio_is_finite) check_coefficients(a, b);
return a * Xmin;
}
return nonzero_value<ratio_is_finite, false, optimized_float>(minlc, min_hint_, a, b);
}
if (a < 0) {
if (b == 0) [[unlikely]] {
if constexpr (!ratio_is_finite) check_coefficients(a, b);
return a * Xmax;
}
return nonzero_value<ratio_is_finite, true, optimized_float>(maxlc, max_hint_, a, b);
}
if constexpr (!ratio_is_finite) check_coefficients(a, b);
return b == 0 ? -ld{0} : b * (b > 0 ? Ymin : Ymax);
}
#endif
#if (defined(__GNUC__) || defined(__clang__))
[[gnu::noinline, gnu::cold]]
#endif
static ld checked_result(ld a, ld b, ld c, ld result, const Line<ld>* line) {
#ifndef SKIP_RUNTIME_CHECKS
if (!std::isfinite(a) || !std::isfinite(c)) slope_error(a, b);
#else
if (!std::isfinite(c)) slope_error(a, b);
#endif
return std::isnan(result) ? recover_line_dot(a, b, line) : result;
}
template <bool minimum>
ld integer_value(ll a, ll b) const {
#ifndef SKIP_RUNTIME_CHECKS
if (empty()) throw std::out_of_range("LineContainer2D: empty query");
#endif
// int64係数の変換と非零分母との比は必ず有限。重複する浮動検査を省く。
if constexpr (minimum) return -value<true>(-ld(a), -ld(b));
else return value<true>(ld(a), ld(b));
}
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
[[noreturn, gnu::noinline, gnu::cold]]
static void count_error() { throw std::length_error("LineContainer2D: point count overflow"); }
#endif
public:
LineContainer2D() = default;
explicit LineContainer2D(ll inf) : inf_(inf) {
#ifndef SKIP_RUNTIME_CHECKS
if (inf <= 0) throw std::invalid_argument("LineContainer2D: inf must be positive");
#endif
}
ll infinity() const noexcept { return inf_; }
LineContainer2D(const LineContainer2D& other)
: Xmax(other.Xmax), Xmin(other.Xmin), Ymax(other.Ymax), Ymin(other.Ymin),
minlc(other.minlc), maxlc(other.maxlc), inf_(other.inf_), count_(other.count_) { refresh_hints(); }
LineContainer2D& operator = (const LineContainer2D& other) {
if (this != &other) { LineContainer2D copy(other); swap(copy); }
return *this;
}
LineContainer2D(LineContainer2D&& other) noexcept
: Xmax(other.Xmax), Xmin(other.Xmin), Ymax(other.Ymax), Ymin(other.Ymin),
minlc(std::move(other.minlc)), maxlc(std::move(other.maxlc)), inf_(other.inf_), count_(other.count_),
min_hint_(other.min_hint_), max_hint_(other.max_hint_) {
other.clear();
}
LineContainer2D& operator = (LineContainer2D&& other) noexcept {
if (this != &other) {
minlc = std::move(other.minlc); maxlc = std::move(other.maxlc);
Xmax = other.Xmax; Xmin = other.Xmin; Ymax = other.Ymax; Ymin = other.Ymin;
count_ = other.count_;
inf_ = other.inf_;
min_hint_ = other.min_hint_; max_hint_ = other.max_hint_;
other.clear();
}
return *this;
}
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
void add(ld x, ld y) {
check_coefficients(x, y);
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
if (count_ == std::numeric_limits<std::size_t>::max()) [[unlikely]] count_error();
#else
if (count_ == std::numeric_limits<std::size_t>::max())
throw std::length_error("LineContainer2D: point count overflow");
#endif
bool refresh_min, refresh_max;
try {
refresh_min = add_hull(minlc, y, x, min_hint_);
refresh_max = add_hull(maxlc, y, x, max_hint_);
}
catch (...) { clear(); throw; }
if (refresh_min) refresh_hint(minlc, min_hint_);
if (refresh_max) refresh_hint(maxlc, max_hint_);
Xmax = std::max(Xmax, x); Xmin = std::min(Xmin, x);
Ymax = std::max(Ymax, y); Ymin = std::min(Ymin, y);
++count_;
}
template <class A, class B>
requires (std::is_integral_v<A> && std::is_integral_v<B> && std::numeric_limits<A>::digits <= 63 &&
std::numeric_limits<B>::digits <= 63)
ld max_ld(A a, B b) const { return integer_value<false>(a, b); }
template <class A, class B>
requires (std::is_integral_v<A> && std::is_integral_v<B> && std::numeric_limits<A>::digits <= 63 &&
std::numeric_limits<B>::digits <= 63)
ld min_ld(A a, B b) const { return integer_value<true>(a, b); }
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
ld max_ld(ld a, ld b) const {
check_empty();
return value<false, true>(a, b);
}
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
ld min_ld(ld a, ld b) const {
#if defined(__clang__) && defined(SKIP_RUNTIME_CHECKS)
check_empty(); return minimum_value<false, true>(a,b);
#else
return -max_ld(-a,-b);
#endif
}
ll max_ll(ll a, ll b) const { return to_integer(integer_value<false>(a, b)); }
ll min_ll(ll a, ll b) const { return to_integer(integer_value<true>(a, b)); }
ll max_rounded_ll(ll a, ll b) const { return to_rounded_integer(integer_value<false>(a, b)); }
ll min_rounded_ll(ll a, ll b) const { return to_rounded_integer(integer_value<true>(a, b)); }
Result max_with_point(ld a, ld b) const {
check_empty();
const auto [x, y] = select(a, b);
ld result = a * x + b * y;
if (std::isnan(result)) [[unlikely]] {
const Line<ld> line{y, x, 0};
result = recover_line_dot(a, b, &line);
}
return {result, x, y};
}
Result min_with_point(ld a, ld b) const {
const auto result = max_with_point(-a, -b);
return {-result.value, result.x, result.y};
}
std::pair<ld, ld> argmax(ld a, ld b) const { check_empty(); return select(a, b); }
std::pair<ld, ld> argmin(ld a, ld b) const { return argmax(-a, -b); }
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
std::optional<ld> max_optional(ld a, ld b) const {
if (empty()) { check_coefficients(a, b); return std::nullopt; }
return value(a, b);
}
#if (defined(__clang__) || defined(__GNUC__))
[[gnu::always_inline]]
#endif
std::optional<ld> min_optional(ld a, ld b) const {
return max_optional(-a,-b).transform([](ld v){return -v;});
}
std::optional<std::pair<ld, ld>> argmax_optional(ld a, ld b) const {
if (empty()) { check_coefficients(a, b); return std::nullopt; }
return select(a, b);
}
std::optional<std::pair<ld, ld>> argmin_optional(ld a, ld b) const { return argmax_optional(-a, -b); }
void swap(LineContainer2D& other) noexcept {
minlc.swap(other.minlc); maxlc.swap(other.maxlc);
std::swap(Xmax, other.Xmax); std::swap(Xmin, other.Xmin);
std::swap(Ymax, other.Ymax); std::swap(Ymin, other.Ymin);
std::swap(count_, other.count_);
std::swap(inf_, other.inf_);
std::swap(min_hint_, other.min_hint_); std::swap(max_hint_, other.max_hint_);
}
std::size_t size() const noexcept { return count_; }
bool empty() const noexcept { return count_ == 0; }
void clear() noexcept {
minlc.clear(); maxlc.clear(); count_ = 0;
min_hint_ = {}; max_hint_ = {};
Xmax = Ymax = -std::numeric_limits<ld>::infinity();
Xmin = Ymin = std::numeric_limits<ld>::infinity();
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
namespace sirkit {}
using namespace sirkit;
// SPDX-License-Identifier: CC0-1.0
// Verify I/O: Verify/current/common/fast_io.hpp
// SPDX-License-Identifier: CC0-1.0
#include <algorithm>
#include <array>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#ifndef SIRKIT_VERIFY_FAST_IO_DEFINED
#define SIRKIT_VERIFY_FAST_IO_DEFINED
// VerifyInput(file=stdin): 非所有のFILEから読む。別の入力方式と混用しない。
// >> 整数: 64bit以下の十進整数を読む。検査省略時は値域内の正しい入力を前提とする。
// >> char/string: 空白を飛ばし、1文字/空白までの文字列を読む。
// >> 浮動小数点: 十進文字列を変換する。通常のCロケールで使う。
// bool / !: 直前までの読み取りが成功しているか/失敗したかを返す。
class VerifyInput {
static constexpr std::size_t buffer_size = 1 << 17;
std::FILE* file_;
std::array<unsigned char, buffer_size + 1> buffer_;
std::size_t position_ = 0, size_ = 0;
bool good_ = true;
int peek() {
if (position_ == size_) {
size_ = std::fread(buffer_.data(), 1, buffer_size, file_);
buffer_[size_] = 0; // 補充後の末尾は常に数字でない番兵。
position_ = 0;
if (size_ == 0) return EOF;
}
return buffer_[position_];
}
int skip_space() {
int c;
while ((c = peek()) != EOF && c <= ' ') ++position_;
if (c == EOF) good_ = false;
return c;
}
public:
explicit VerifyInput(std::FILE* file = stdin) : file_(file) {}
VerifyInput(const VerifyInput&) = delete;
VerifyInput& operator=(const VerifyInput&) = delete;
explicit operator bool() const noexcept { return good_; }
bool operator!() const noexcept { return !good_; }
template<class T>
requires (std::is_integral_v<T> && sizeof(T) <= sizeof(std::uint64_t)
&& !std::is_same_v<T, char>)
VerifyInput& operator>>(T& result) {
if (!good_) return *this;
int c = skip_space();
if (c == EOF) return *this;
bool negative = false;
if (c == '-' || c == '+') {
negative = c == '-';
++position_;
c = peek();
}
if (c == EOF) {
good_ = false;
return *this;
}
#ifndef SKIP_RUNTIME_CHECKS
if (c < '0' || c > '9') {
good_ = false;
return *this;
}
// 先頭の0を除けば、20桁以下の末尾直前の値はuint64_tに収まる。
while (c == '0') {
++position_;
c = peek();
}
std::uint64_t previous = 0;
unsigned last_digit = 0;
std::size_t digits = 0;
#endif
std::uint64_t value = 0;
#ifdef SKIP_RUNTIME_CHECKS
// バッファ末尾の番兵で内側の範囲判定を省き、数字が境界をまたぐときだけ補充する。
for (;;) {
const unsigned char* first = buffer_.data() + position_;
const unsigned char* cursor = first;
while (static_cast<unsigned>(*cursor - '0') < 10) {
value = value * 10 + static_cast<unsigned>(*cursor - '0');
++cursor;
}
position_ += static_cast<std::size_t>(cursor - first);
if (position_ != size_) break;
c = peek();
if (c == EOF) break;
}
#else
while (c >= '0' && c <= '9') {
previous = value;
last_digit = static_cast<unsigned>(c - '0');
if (digits <= 20) ++digits;
value = value * 10 + static_cast<unsigned>(c - '0');
++position_;
c = peek();
}
#endif
#ifndef SKIP_RUNTIME_CHECKS
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
if ((c != EOF && c > ' ') || digits > 20
|| (digits == 20 && (previous > maximum / 10
|| (previous == maximum / 10 && last_digit > maximum % 10)))) {
good_ = false;
return *this;
}
std::uint64_t limit = static_cast<std::uint64_t>(std::numeric_limits<T>::max());
if constexpr (std::is_signed_v<T>) limit += negative;
else if (negative && value != 0) good_ = false;
if (value > limit) good_ = false;
if (!good_) return *this;
#endif
if constexpr (std::is_signed_v<T>) {
constexpr std::uint64_t minimum_magnitude =
static_cast<std::uint64_t>(std::numeric_limits<T>::max()) + 1;
// -(最小値)は評価せず、絶対値が最大値+1のときだけ最小値を使う。
result = negative
? (value == minimum_magnitude ? std::numeric_limits<T>::min()
: static_cast<T>(-static_cast<T>(value)))
: static_cast<T>(value);
} else result = static_cast<T>(value);
return *this;
}
VerifyInput& operator>>(char& result) {
if (!good_) return *this;
const int c = skip_space();
if (c != EOF) {
result = static_cast<char>(c);
++position_;
}
return *this;
}
VerifyInput& operator>>(std::string& result) {
if (!good_) return *this;
int c = skip_space();
if (c == EOF) return *this;
result.clear();
for (;;) {
const std::size_t first = position_;
while (position_ < size_ && buffer_[position_] > ' ') ++position_;
result.append(reinterpret_cast<const char*>(buffer_.data() + first), position_ - first);
if (position_ != size_ || peek() == EOF) break;
}
return *this;
}
template<class T> requires std::is_floating_point_v<T>
VerifyInput& operator>>(T& result) {
std::string token;
*this >> token;
if (!good_) return *this;
#ifndef SKIP_RUNTIME_CHECKS
char* end = nullptr;
const long double value = std::strtold(token.c_str(), &end);
if (end != token.c_str() + token.size()) good_ = false;
else result = static_cast<T>(value);
#else
result = static_cast<T>(std::strtold(token.c_str(), nullptr));
#endif
return *this;
}
};
// VerifyOutput(file=stdout): 非所有のFILEへ書く。破棄時にflushし、FILEは閉じない。
// << 整数/char/文字列/浮動小数点: 値を出力する。整数は64bit以下に対応する。
// fixed(digits): 浮動小数点を小数点以下digits桁で出力する(digits>=0)。
// flush(): バッファの内容をFILEへ渡す。bool / !で書き込み失敗を確認できる。
class VerifyOutput {
static constexpr std::size_t buffer_size = 1 << 17;
std::FILE* file_;
std::array<char, buffer_size> buffer_;
std::size_t size_ = 0;
int precision_ = 6;
bool fixed_ = false, good_ = true;
void write(std::string_view text) {
while (!text.empty() && good_) {
if (size_ == buffer_.size()) flush();
const auto count = std::min(text.size(), buffer_.size() - size_);
std::memcpy(buffer_.data() + size_, text.data(), count);
size_ += count;
text.remove_prefix(count);
}
}
public:
explicit VerifyOutput(std::FILE* file = stdout) : file_(file) {}
VerifyOutput(const VerifyOutput&) = delete;
VerifyOutput& operator=(const VerifyOutput&) = delete;
~VerifyOutput() { flush(); }
explicit operator bool() const noexcept { return good_; }
bool operator!() const noexcept { return !good_; }
VerifyOutput& fixed(int digits) {
if (digits < 0) throw std::invalid_argument("negative output precision");
fixed_ = true;
precision_ = digits;
return *this;
}
void flush() noexcept {
std::size_t written = 0;
while (written < size_ && good_) {
const auto count = std::fwrite(buffer_.data() + written, 1, size_ - written, file_);
if (count == 0) good_ = false;
written += count;
}
size_ = 0;
}
template<class T>
requires (std::is_integral_v<T> && sizeof(T) <= sizeof(std::uint64_t)
&& !std::is_same_v<T, char>)
VerifyOutput& operator<<(T value) {
if constexpr (std::is_same_v<T, bool>) return *this << static_cast<char>('0' + value);
else {
constexpr std::size_t capacity = std::numeric_limits<std::uint64_t>::digits10 + 3;
if (buffer_.size() - size_ < capacity) flush();
if (!good_) return *this;
const auto end = std::to_chars(buffer_.data() + size_, buffer_.data() + size_ + capacity, value).ptr;
size_ = static_cast<std::size_t>(end - buffer_.data());
return *this;
}
}
VerifyOutput& operator<<(char value) {
if (size_ == buffer_.size()) flush();
if (good_) buffer_[size_++] = value;
return *this;
}
VerifyOutput& operator<<(std::string_view value) { write(value); return *this; }
VerifyOutput& operator<<(const std::string& value) { write(value); return *this; }
VerifyOutput& operator<<(const char* value) { write(value); return *this; }
template<class T> requires std::is_floating_point_v<T>
VerifyOutput& operator<<(T value) {
char local[128];
const char* format = fixed_ ? "%.*Lf" : "%.*Lg";
const int count = std::snprintf(local, sizeof(local), format, precision_,
static_cast<long double>(value));
if (count < 0) good_ = false;
else if (static_cast<std::size_t>(count) < sizeof(local))
write(std::string_view(local, static_cast<std::size_t>(count)));
else {
std::vector<char> text(static_cast<std::size_t>(count) + 1);
std::snprintf(text.data(), text.size(), format, precision_, static_cast<long double>(value));
write(std::string_view(text.data(), static_cast<std::size_t>(count)));
}
return *this;
}
};
inline VerifyInput verify_in;
inline VerifyOutput verify_out;
#endif
#include <cstdint>
int main() {
int n, q;
verify_in >> n >> q;
// |a*x+b|<=2e18なので、既定の2^60より大きい上限を指定する。
LineContainer2D points(std::int64_t{1} << 62);
for (int i = 0; i < n; ++i) {
std::int64_t a, b;
verify_in >> a >> b;
points.add(a, b);
}
for (int i = 0; i < q; ++i) {
int type;
verify_in >> type;
if (type == 0) {
std::int64_t a, b;
verify_in >> a >> b;
points.add(a, b);
} else {
std::int64_t x;
verify_in >> x;
verify_out << points.min_rounded_ll(x, 1) << '\n';
}
}
}