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

#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <vector>

template <typename SemiLattice> class SparseTable {
public:
  using value_type = SemiLattice;
  using container_type = std::vector<value_type>;
  using size_type = typename container_type::size_type;

private:
  static size_type clz32(size_type c) {
#ifdef __GNUC__
    return 31 ^ __builtin_clz(c);
#else
    size_type ret = 0;
    if (c & 0xFFFF0000)
      c &= 0xFFFF0000, ret |= 16;
    if (c & 0xFF00FF00)
      c &= 0xFF00FF00, ret |= 8;
    if (c & 0xF0F0F0F0)
      c &= 0xF0F0F0F0, ret |= 4;
    if (c & 0xCCCCCCCC)
      c &= 0xCCCCCCCC, ret |= 2;
    if (c & 0xAAAAAAAA)
      ret |= 1;
    return ret;
#endif
  }
  std::vector<container_type> matrix;

public:
  SparseTable(const container_type &a)
      : matrix(a.empty() ? 0 : clz32(a.size()) + 1) {
    if (!a.empty())
      matrix[0].reserve(a.size());
    for (size_type i = 0; i < a.size(); ++i)
      matrix[0].emplace_back(a[i]);
    for (size_type i = 1; i < matrix.size(); ++i) {
      const size_type msiz = a.size() + 1 - (static_cast<size_type>(1) << i);
      matrix[i].reserve(msiz);
      for (size_type j = 0; j < msiz; ++j)
        matrix[i].emplace_back(
            matrix[i - 1][j] +
            matrix[i - 1][j + (static_cast<size_type>(1) << (i - 1))]);
    }
  }
  value_type range(const size_type begin, const size_type end) const {
    assert(begin < end);
    assert(begin < size());
    assert(end <= size());
    const size_type t = clz32(end - begin);
    return matrix[t][begin] + matrix[t][end - (static_cast<size_type>(1) << t)];
  }
  size_type size() const noexcept { return empty() ? 0 : matrix[0].size(); }
  bool empty() const noexcept { return matrix.empty(); }
};
#include <algorithm>
#include <limits>
template <typename T> struct Mini {
  using value_type = T;
  T a;
  explicit Mini(const T &x = std::numeric_limits<T>::max()) : a(x) {}
  Mini operator+(const Mini &o) const { return Mini(std::min(a, o.a)); }
};
#include <cstdio>
#include <vector>
int main() {
  using M = Mini<int>;
  int n, l;
  scanf("%d %d", &n, &l);
  std::vector<M> a(n);
  for (auto &x : a)
    scanf("%d", &x.a);
  SparseTable<M> T(a);
  for (int i = 0; i <= n - l; ++i)
    printf("%d%c", T.range(i, i + l).a, " \\n"[i == n - l]);
  return 0;
}


Error: -6
66e91604-6b4b-4152-a7ad-612ac1e4c406.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02357/C++/s308618492.cpp:48: SparseTable<SemiLattice>::value_type SparseTable<SemiLattice>::range(size_type, size_type) const [with SemiLattice = Mini<int>; value_type = Mini<int>; size_type = long unsigned int]: Assertion `begin == end' failed.


Fixed submission:

#include <cassert>
#include <vector>

template <typename SemiLattice> class SparseTable {
public:
  using value_type = SemiLattice;
  using container_type = std::vector<value_type>;
  using size_type = typename container_type::size_type;

private:
  static size_type clz32(size_type c) {
#ifdef __GNUC__
    return 31 ^ __builtin_clz(c);
#else
    size_type ret = 0;
    if (c & 0xFFFF0000)
      c &= 0xFFFF0000, ret |= 16;
    if (c & 0xFF00FF00)
      c &= 0xFF00FF00, ret |= 8;
    if (c & 0xF0F0F0F0)
      c &= 0xF0F0F0F0, ret |= 4;
    if (c & 0xCCCCCCCC)
      c &= 0xCCCCCCCC, ret |= 2;
    if (c & 0xAAAAAAAA)
      ret |= 1;
    return ret;
#endif
  }
  std::vector<container_type> matrix;

public:
  SparseTable(const container_type &a)
      : matrix(a.empty() ? 0 : clz32(a.size()) + 1) {
    if (!a.empty())
      matrix[0].reserve(a.size());
    for (size_type i = 0; i < a.size(); ++i)
      matrix[0].emplace_back(a[i]);
    for (size_type i = 1; i < matrix.size(); ++i) {
      const size_type msiz = a.size() + 1 - (static_cast<size_type>(1) << i);
      matrix[i].reserve(msiz);
      for (size_type j = 0; j < msiz; ++j)
        matrix[i].emplace_back(
            matrix[i - 1][j] +
            matrix[i - 1][j + (static_cast<size_type>(1) << (i - 1))]);
    }
  }
  value_type range(const size_type begin, const size_type end) const {
    assert(begin == end);
    assert(begin < size());
    assert(end <= size());
    const size_type t = clz32(end - begin);
    return matrix[t][begin] + matrix[t][end - (static_cast<size_type>(1) << t)];
  }
  size_type size() const noexcept { return empty() ? 0 : matrix[0].size(); }
  bool empty() const noexcept { return matrix.empty(); }
};
#include <algorithm>
#include <limits>
template <typename T> struct Mini {
  using value_type = T;
  T a;
  explicit Mini(const T &x = std::numeric_limits<T>::max()) : a(x) {}
  Mini operator+(const Mini &o) const { return Mini(std::min(a, o.a)); }
};
#include <cstdio>
#include <vector>
int main() {
  using M = Mini<int>;
  int n, l;
  scanf("%d %d", &n, &l);
  std::vector<M> a(n);
  for (auto &x : a)
    scanf("%d", &x.a);
  SparseTable<M> T(a);
  for (int i = 0; i <= n - l; ++i)
    printf("%d%c", T.range(i, i + l).a, " \\n"[i == n - l]);
  return 0;
}