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

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

template <typename Monoid> class DisjointSparseTable {
  using T = typename Monoid::value_type;
  Monoid m;
  vector<int> logTable;
  vector<vector<T>> table;

public:
  template <typename InputIterator>
  DisjointSparseTable(InputIterator first, InputIterator last) {
    int n = distance(first, last);
    int size = 1;
    while (size < n)
      size *= 2;
    logTable.resize(size + 1);
    for (int i = 2; i <= size; i++)
      logTable[i] = logTable[i >> 1] + 1;
    int k = logTable[size];
    table.resize(k + 2, vector<T>(size, m.id()));
    copy(first, last, table[1].begin());
    for (int h = 2, range; (range = 1 << h) <= size; h++) {
      int half = range >> 1;
      for (int i = half; i < size; i += range) {
        table[h][i - 1] = table[1][i - 1];
        for (int j = i - 2; j >= i - half; j--)
          table[h][j] = m(table[1][j], table[h][j + 1]);
        table[h][i] = table[1][i];
        for (int j = i + 1; j < i + half; j++)
          table[h][j] = m(table[h][j - 1], table[1][j]);
      }
    }
  }

  T fold(int l, int r) { // [l, r)
    if (l == r)
      return m.id();
    if (l == --r)
      return table[1][l];
    int h = logTable[l ^ r] + 1;
    return m(table[h][l], table[h][r]);
  }
};

template <typename T, T upperInf = numeric_limits<T>::max()> struct minMonoid {
  using value_type = T;
  value_type id() { return upperInf; }
  value_type operator()(const value_type &a, const value_type &b) {
    return a < b ? a : b;
  }
};

int a[1000000];

main {
  int N, L;
  scanf("%lld %lld", &N, &L);
  rep(i, N) scanf("%lld", &a[i]);
  DisjointSparseTable<minMonoid<int>> dst(a, a + N);
  rep(i, N - L + 1) printf("%d%c", dst.fold(i, i + L), " \\n"[i == N - L]);
}


Error: 0

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

template <typename Monoid> class DisjointSparseTable {
  using T = typename Monoid::value_type;
  Monoid m;
  vector<int> logTable;
  vector<vector<T>> table;

public:
  template <typename InputIterator>
  DisjointSparseTable(InputIterator first, InputIterator last) {
    int n = distance(first, last);
    int size = 1;
    while (size < n)
      size *= 2;
    logTable.resize(size + 1);
    for (int i = 2; i <= size; i++)
      logTable[i] = logTable[i >> 1] + 1;
    int k = logTable[size];
    table.resize(k + 1, vector<T>(size, m.id()));
    copy(first, last, table[1].begin());
    for (int h = 2, range; (range = 1 << h) <= size; h++) {
      int half = range >> 1;
      for (int i = half; i < size; i += range) {
        table[h][i - 1] = table[1][i - 1];
        for (int j = i - 2; j >= i - half; j--)
          table[h][j] = m(table[1][j], table[h][j + 1]);
        table[h][i] = table[1][i];
        for (int j = i + 1; j < i + half; j++)
          table[h][j] = m(table[h][j - 1], table[1][j]);
      }
    }
  }

  T fold(int l, int r) { // [l, r)
    if (l == r)
      return m.id();
    if (l == --r)
      return table[1][l];
    int h = logTable[l ^ r] + 1;
    return m(table[h][l], table[h][r]);
  }
};

template <typename T, T upperInf = numeric_limits<T>::max()> struct minMonoid {
  using value_type = T;
  value_type id() { return upperInf; }
  value_type operator()(const value_type &a, const value_type &b) {
    return a < b ? a : b;
  }
};

int a[1000000];

main {
  int N, L;
  scanf("%lld %lld", &N, &L);
  rep(i, N) scanf("%lld", &a[i]);
  DisjointSparseTable<minMonoid<int>> dst(a, a + N);
  rep(i, N - L + 1) printf("%d%c", dst.fold(i, i + L), " \\n"[i == N - L]);
}