Problem p02598 (C++) — buggy submission, verdict: Runtime Error

#ifndef __OPTIMIZE_H__
#define __OPTIMIZE_H__

/* updated: 2020-07-22 */

#include <cmath>
#include <cstdint>
#include <stack>
#include <vector>

/* std::not_fn などに相当するものを自作するほうがいいか？ */

/* findAllBoundaries の戻り値 */
template <class Value> struct BoundariesResult {
  std::vector<int64_t> args; // func の引数の列
  std::vector<Value> vals;   // func の戻り値の列
};

template <class Function>
int64_t findBoundary(Function func, int64_t lower, int64_t upper,
                     bool greater = false);
template <class Function>
double findBoundaryFloat(Function func, double lower, double upper,
                         bool greater = false);
template <class Function>
double findOptimumFloat(Function func, double lower, double upper,
                        bool maximize = false);
template <class Function>
auto findAllBoundaries(Function func, int64_t lower, int64_t upper)
    -> BoundariesResult<decltype(func(lower))>;

/*
// 関数 func : int64_t -> bool に対して
// 二分探索で true と false の境界を(1つ)見つける O(log(upper - lower))
// 探索範囲は [lower, upper)
// デフォルトでは func(i - 1) == true かつ func(i) == false となる i を返す
(lower <= i <= upper)
// greater を指定すると真偽を逆にする
*/
template <class Function>
int64_t findBoundary(Function func, int64_t lower, int64_t upper,
                     bool greater) {
  int64_t lo = lower - 1;
  int64_t hi = upper;
  while (hi - lo > 1) {
    int64_t mid = (lo + hi) / 2;
    if (func(mid) == greater) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  return hi;
}

/*
// 関数 func : double -> bool に対して
// 二分探索で true から false に変化する境界を(1つ)見つける
// greater を指定すると真偽を逆にする
*/
template <class Function>
double findBoundaryFloat(Function func, double lower, double upper,
                         bool greater) {
  constexpr double precision = 1e-9;
  const int iterations =
      (int)(std::log2(upper - lower) - std::log2(precision)) + 1;
  double lo = lower;
  double hi = upper;
  for (int i = 0; i < iterations; ++i) {
    double mid = (lo + hi) / 2;
    if (func(mid) == greater) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  return hi;
}

/*
// 関数 func : double -> double に対して
// 三分探索で極小値をとる点を見つける(maximize を指定すると極大値を探索する)
*/
template <class Function>
double findOptimumFloat(Function func, double lower, double upper,
                        bool maximize) {
  constexpr double precision = 1e-9;
  constexpr double ratio = 1.5;
  const int iterations =
      (int)((std::log(upper - lower) - std::log(precision)) / std::log(ratio)) +
      1;

  double lo = lower;
  double hi = upper;
  for (int i = 0; i < iterations; ++i) {
    double mid_lo = (ratio * lo + hi) / (1 + ratio);
    double mid_hi = (lo + ratio * hi) / (1 + ratio);
    double val_lo = func(mid_lo);
    double val_hi = func(mid_hi);
    if ((val_lo > val_hi) == maximize) {
      hi = mid_hi;
    } else {
      lo = mid_lo;
    }
  }
  return hi;
}

/*
// 関数 func : int64_t -> Value(任意) に対して
// 二分探索で値が変化する点をすべて見つける O(min(range, 変化する点数 *
log(range)))
// 探索区間 [lower, upper) 内で同じ値を返す区間が複数登場してはならない
// 戻り値は func(x) が等しい区間の左端の列 (args) とそれに対応する func の値の列
(vals)
// (ただし lower を含み upper を含まない)
// 例えば区間 [0, 6) に対する func の値が { 4, 4, 4, 2, 2, 5 } なら
// (args, vals) = ( { 0, 3, 5 }, { 4, 2, 5 } ) を返す
*/
template <class Function>
auto findAllBoundaries(Function func, int64_t lower, int64_t upper)
    -> BoundariesResult<decltype(func(lower))> {
  using Value = decltype(func(lower)); // 値の型を推論
  struct Mapping {
    int64_t arg;
    Value val;
    Mapping(int64_t arg, Value val) : arg(arg), val(val) {}
  };
  using Range = std::pair<Mapping, Mapping>;

  /* 区間として正当かどうかチェック */
  if (upper <= lower)
    return BoundariesResult<Value>();

  /* 区間 [lower, upper - 1] の両端の値を計算し、戻り値用配列に追加 */
  int64_t lower_arg = lower;
  int64_t upper_arg = upper - 1;
  Value lower_val = func(lower_arg);
  Value upper_val = func(upper_arg);

  std::vector<int64_t> ret_args;
  std::vector<Value> ret_vals;
  ret_args.push_back(lower_arg);
  ret_vals.push_back(lower_val);

  /* 区間全体が同じ値を返すなら終了 */
  if (lower_val == upper_val) {
    BoundariesResult<Value> ret;
    ret.args = std::move(ret_args);
    ret.vals = std::move(ret_vals);
    return ret;
  }

  /* 区間 [lower, upper - 1] の両端の値が異なるなら stack に追加 */
  std::stack<Range> stack;
  stack.push(
      Range(Mapping(lower_arg, lower_val), Mapping(upper_arg, upper_val)));

  /* 両端の値が異なる限り二分探索で境界を探索 */
  while (!stack.empty()) {
    /* 区間 [l_arg, r_arg] に注目 */
    auto pair = stack.top();
    stack.pop();
    Mapping l = pair.first;
    Mapping r = pair.second;
    int64_t l_arg = l.arg;
    int64_t r_arg = r.arg;
    Value l_val = l.val;
    Value r_val = r.val;

    /* 区間幅が 1 なら終了 (r_arg が境界) */
    if (r_arg - l_arg == 1) {
      ret_args.push_back(r_arg);
      ret_vals.push_back(r.val);
      continue;
    }

    /* 区間 [l_arg, m_arg] と [m_arg, r_arg] に分割 */
    int64_t m_arg = (l_arg + r_arg) / 2;
    Value m_val = func(m_arg);

    /* 各区間について、両端の値が異なるなら stack に追加 */
    /* stack から取り出す順序の都合上、区間 [m, r] -> 区間 [l, m] の順で追加 */
    if (m_val != r_val) {
      stack.push(Range(Mapping(m_arg, m_val), Mapping(r_arg, r_val)));
    }

    if (m_val != l_val) {
      stack.push(Range(Mapping(l_arg, l_val), Mapping(m_arg, m_val)));
    }
  }

  BoundariesResult<Value> ret;
  ret.args = std::move(ret_args);
  ret.vals = std::move(ret_vals);
  return ret;
}

#endif
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, a, n) for (LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \\
  {                                                                            \\
    cin.tie(NULL);                                                             \\
    ios::sync_with_stdio(false);                                               \\
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BIT.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "FFT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "Interpolation.h"
#include "KDTree.h"
#include "LIS.h"
#include "LazySegmentTree.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

LL n, k;
LL a[200000];
bool ok(LL x) {
  int cnt = 0;
  REP(i, 0, n) { cnt += (a[i] + x - 1) / x - 1; }
  return cnt <= k;
}

int main() {
  cin >> n >> k;
  REP(i, 0, n) cin >> a[i];
  cout << findBoundary(ok, 1, INF, true) << endl;
  return 0;
}


Error: 0

Fixed submission:

#ifndef __OPTIMIZE_H__
#define __OPTIMIZE_H__

/* updated: 2020-07-22 */

#include <cmath>
#include <cstdint>
#include <stack>
#include <vector>

/* std::not_fn などに相当するものを自作するほうがいいか？ */

/* findAllBoundaries の戻り値 */
template <class Value> struct BoundariesResult {
  std::vector<int64_t> args; // func の引数の列
  std::vector<Value> vals;   // func の戻り値の列
};

template <class Function>
int64_t findBoundary(Function func, int64_t lower, int64_t upper,
                     bool greater = false);
template <class Function>
double findBoundaryFloat(Function func, double lower, double upper,
                         bool greater = false);
template <class Function>
double findOptimumFloat(Function func, double lower, double upper,
                        bool maximize = false);
template <class Function>
auto findAllBoundaries(Function func, int64_t lower, int64_t upper)
    -> BoundariesResult<decltype(func(lower))>;

/*
// 関数 func : int64_t -> bool に対して
// 二分探索で true と false の境界を(1つ)見つける O(log(upper - lower))
// 探索範囲は [lower, upper)
// デフォルトでは func(i - 1) == true かつ func(i) == false となる i を返す
(lower <= i <= upper)
// greater を指定すると真偽を逆にする
*/
template <class Function>
int64_t findBoundary(Function func, int64_t lower, int64_t upper,
                     bool greater) {
  int64_t lo = lower - 1;
  int64_t hi = upper;
  while (hi - lo > 1) {
    int64_t mid = (lo + hi) / 2;
    if (func(mid) == greater) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  return hi;
}

/*
// 関数 func : double -> bool に対して
// 二分探索で true から false に変化する境界を(1つ)見つける
// greater を指定すると真偽を逆にする
*/
template <class Function>
double findBoundaryFloat(Function func, double lower, double upper,
                         bool greater) {
  constexpr double precision = 1e-9;
  const int iterations =
      (int)(std::log2(upper - lower) - std::log2(precision)) + 1;
  double lo = lower;
  double hi = upper;
  for (int i = 0; i < iterations; ++i) {
    double mid = (lo + hi) / 2;
    if (func(mid) == greater) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  return hi;
}

/*
// 関数 func : double -> double に対して
// 三分探索で極小値をとる点を見つける(maximize を指定すると極大値を探索する)
*/
template <class Function>
double findOptimumFloat(Function func, double lower, double upper,
                        bool maximize) {
  constexpr double precision = 1e-9;
  constexpr double ratio = 1.5;
  const int iterations =
      (int)((std::log(upper - lower) - std::log(precision)) / std::log(ratio)) +
      1;

  double lo = lower;
  double hi = upper;
  for (int i = 0; i < iterations; ++i) {
    double mid_lo = (ratio * lo + hi) / (1 + ratio);
    double mid_hi = (lo + ratio * hi) / (1 + ratio);
    double val_lo = func(mid_lo);
    double val_hi = func(mid_hi);
    if ((val_lo > val_hi) == maximize) {
      hi = mid_hi;
    } else {
      lo = mid_lo;
    }
  }
  return hi;
}

/*
// 関数 func : int64_t -> Value(任意) に対して
// 二分探索で値が変化する点をすべて見つける O(min(range, 変化する点数 *
log(range)))
// 探索区間 [lower, upper) 内で同じ値を返す区間が複数登場してはならない
// 戻り値は func(x) が等しい区間の左端の列 (args) とそれに対応する func の値の列
(vals)
// (ただし lower を含み upper を含まない)
// 例えば区間 [0, 6) に対する func の値が { 4, 4, 4, 2, 2, 5 } なら
// (args, vals) = ( { 0, 3, 5 }, { 4, 2, 5 } ) を返す
*/
template <class Function>
auto findAllBoundaries(Function func, int64_t lower, int64_t upper)
    -> BoundariesResult<decltype(func(lower))> {
  using Value = decltype(func(lower)); // 値の型を推論
  struct Mapping {
    int64_t arg;
    Value val;
    Mapping(int64_t arg, Value val) : arg(arg), val(val) {}
  };
  using Range = std::pair<Mapping, Mapping>;

  /* 区間として正当かどうかチェック */
  if (upper <= lower)
    return BoundariesResult<Value>();

  /* 区間 [lower, upper - 1] の両端の値を計算し、戻り値用配列に追加 */
  int64_t lower_arg = lower;
  int64_t upper_arg = upper - 1;
  Value lower_val = func(lower_arg);
  Value upper_val = func(upper_arg);

  std::vector<int64_t> ret_args;
  std::vector<Value> ret_vals;
  ret_args.push_back(lower_arg);
  ret_vals.push_back(lower_val);

  /* 区間全体が同じ値を返すなら終了 */
  if (lower_val == upper_val) {
    BoundariesResult<Value> ret;
    ret.args = std::move(ret_args);
    ret.vals = std::move(ret_vals);
    return ret;
  }

  /* 区間 [lower, upper - 1] の両端の値が異なるなら stack に追加 */
  std::stack<Range> stack;
  stack.push(
      Range(Mapping(lower_arg, lower_val), Mapping(upper_arg, upper_val)));

  /* 両端の値が異なる限り二分探索で境界を探索 */
  while (!stack.empty()) {
    /* 区間 [l_arg, r_arg] に注目 */
    auto pair = stack.top();
    stack.pop();
    Mapping l = pair.first;
    Mapping r = pair.second;
    int64_t l_arg = l.arg;
    int64_t r_arg = r.arg;
    Value l_val = l.val;
    Value r_val = r.val;

    /* 区間幅が 1 なら終了 (r_arg が境界) */
    if (r_arg - l_arg == 1) {
      ret_args.push_back(r_arg);
      ret_vals.push_back(r.val);
      continue;
    }

    /* 区間 [l_arg, m_arg] と [m_arg, r_arg] に分割 */
    int64_t m_arg = (l_arg + r_arg) / 2;
    Value m_val = func(m_arg);

    /* 各区間について、両端の値が異なるなら stack に追加 */
    /* stack から取り出す順序の都合上、区間 [m, r] -> 区間 [l, m] の順で追加 */
    if (m_val != r_val) {
      stack.push(Range(Mapping(m_arg, m_val), Mapping(r_arg, r_val)));
    }

    if (m_val != l_val) {
      stack.push(Range(Mapping(l_arg, l_val), Mapping(m_arg, m_val)));
    }
  }

  BoundariesResult<Value> ret;
  ret.args = std::move(ret_args);
  ret.vals = std::move(ret_vals);
  return ret;
}

#endif
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T>>;
#define REP(i, a, n) for (LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for (LL i = (LL)(n)-1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP                                                                \\
  {                                                                            \\
    cin.tie(NULL);                                                             \\
    ios::sync_with_stdio(false);                                               \\
  }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream &operator>>(istream &ist, pair<T, U> &right) {
  return ist >> right.first >> right.second;
}
template <class T, class U>
ostream &operator<<(ostream &ost, const pair<T, U> &right) {
  return ost << right.first << ' ' << right.second;
}
template <class T, class TCompatible, size_t N>
void Fill(T (&dest)[N], const TCompatible &val) {
  fill(dest, dest + N, val);
}
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T (&dest)[M][N], const TCompatible &val) {
  for (int i = 0; i < M; ++i)
    Fill(dest[i], val);
}
template <class T> T Next() {
  T buf;
  cin >> buf;
  return buf;
}
istream &Ignore(istream &ist) {
  string s;
  ist >> s;
  return ist;
}
bool Inside(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BIT.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "FFT.h"
#include "Factorization.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "Interpolation.h"
#include "KDTree.h"
#include "LIS.h"
#include "LazySegmentTree.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream &operator>>(istream &ist, __int128 &val) {
  LL tmp;
  ist >> tmp;
  val = tmp;
  return ist;
}
ostream &operator<<(ostream &ost, __int128 val) {
  LL tmp = val;
  ost << tmp;
  return ost;
}
#endif

LL n, k;
LL a[200000];
bool ok(LL x) {
  int cnt = 0;
  REP(i, 0, n) { cnt += (a[i] + x - 1) / x - 1; }
  return cnt <= k;
}

int main() {
  cin >> n >> k;
  REP(i, 0, n) cin >> a[i];
  cout << findBoundary(ok, 0, INF, true) << endl;
  return 0;
}