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

#include "bits/stdc++.h"
#include <boost/dynamic_bitset.hpp>
#include <boost/multi_array.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
namespace adaptor = boost::adaptors;
using index_t = std::size_t;

namespace lib {
using graph_t = std::vector<std::set<index_t>>;
template <class T> using weighted_graph_t = std::vector<std::map<index_t, T>>;
template <class T> using pair = std::pair<T, T>;

struct max_t {
  template <class T> constexpr T operator()(T const &lhs, T const &rhs) const {
    return std::max(lhs, rhs);
  }
};
struct min_t {
  template <class T> constexpr T operator()(T const &lhs, T const &rhs) const {
    return std::min(lhs, rhs);
  }
};

constexpr max_t max;
constexpr min_t min;

template <class T> auto order_by(T func) {
  return [=](auto &&lhs, auto &&rhs) { return func(lhs) < func(rhs); };
}

template <class T> constexpr auto max_v = std::numeric_limits<T>::max();
template <class T> constexpr auto min_v = std::numeric_limits<T>::min();

template <class T>
using p_queue = std::priority_queue<T, std::vector<T>, std::greater<>>;

template <class T> constexpr T pow(T val, std::uint64_t p) {
  auto ret = T(1);
  auto s = 1ULL;
  for (auto i = 0; i < 63; ++i, s <<= 1) {
    if (p & s) {
      ret *= val;
    }
    val *= val;
  }
  return ret;
}
} // namespace lib

template <class T, class Op = std::plus<>> class seg_tree {
  std::vector<T> vec;
  T unit;
  std::size_t sz;
  Op op;
  static std::size_t get_size(std::size_t s) {
    std::size_t ret = 1;
    while (ret < s) {
      ret <<= 1;
    }
    return ret;
  }

public:
  seg_tree(std::size_t size, T u = T(), Op f = Op())
      : vec(), unit(u), sz(get_size(size)), op(f) {
    vec.assign(2 * sz, unit);
  }
  template <class Range, class = decltype(std::declval<Range>().size())>
  seg_tree(Range const &rng, T u = T(), Op f = Op())
      : seg_tree(rng.size(), u, f) {
    set(rng);
  }
  template <class Range> void set(Range const &rng) {
    std::size_t i = 0;
    for (auto &&v : rng) {
      vec[i + sz] = v;
      if (++i >= sz) {
        break;
      }
    }
    for (auto s = sz / 2; s > 0; s /= 2) {
      for (index_t i = 0; i < s; ++i) {
        vec[(i + s)] = op(vec[2 * (i + s)], vec[2 * (i + s) + 1]);
      }
    }
  }
  T operator()(std::size_t start, std::size_t end) const {
    T L = unit, R = unit;
    for (std::size_t a = start + sz, b = end + sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1) {
        L = op(L, vec[a++]);
      }
      if (b & 1) {
        R = op(vec[--b], R);
      }
    }
    return op(L, R);
  }
  void update(std::size_t i, T const &v) {
    auto k = i + sz;
    vec[k] = v;
    while (k >>= 1) {
      vec[k] = op(vec[2 * k], vec[2 * k + 1]);
    }
  }
};

template <class T> struct max_value {
  T v;
  max_value(T u = lib::min_v<T>) : v(u) {}
  auto &operator=(T const &u) {
    v = std::max(v, u);
    return *this;
  }
  auto &operator=(max_value const &u) {
    v = std::max(v, *u);
    return *this;
  }
  T operator*() const { return v; }
  void reset() { v = lib::min_v<T>; }
};

void Main() {
  int N, K;
  std::cin >> N >> K;
  const int MAX = 300000;
  seg_tree<int, lib::max_t> tree(MAX + 1);
  max_value<int> max;
  for (auto _ : boost::irange(N)) {
    int p;
    std::cin >> p;
    auto left = std::max(0, p - K);
    auto right = std::min(MAX + 1, p + K + 1);
    auto v = tree(left, right);
    max = v + 1;
    tree.update(p, v + 1);
  }
  std::cout << *max << std::endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <boost/dynamic_bitset.hpp>
#include <boost/multi_array.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
namespace adaptor = boost::adaptors;
using index_t = std::size_t;

namespace lib {
using graph_t = std::vector<std::set<index_t>>;
template <class T> using weighted_graph_t = std::vector<std::map<index_t, T>>;
template <class T> using pair = std::pair<T, T>;

struct max_t {
  template <class T> constexpr T operator()(T const &lhs, T const &rhs) const {
    return std::max(lhs, rhs);
  }
};
struct min_t {
  template <class T> constexpr T operator()(T const &lhs, T const &rhs) const {
    return std::min(lhs, rhs);
  }
};

constexpr max_t max;
constexpr min_t min;

template <class T> auto order_by(T func) {
  return [=](auto &&lhs, auto &&rhs) { return func(lhs) < func(rhs); };
}

template <class T> constexpr auto max_v = std::numeric_limits<T>::max();
template <class T> constexpr auto min_v = std::numeric_limits<T>::min();

template <class T>
using p_queue = std::priority_queue<T, std::vector<T>, std::greater<>>;

template <class T> constexpr T pow(T val, std::uint64_t p) {
  auto ret = T(1);
  auto s = 1ULL;
  for (auto i = 0; i < 63; ++i, s <<= 1) {
    if (p & s) {
      ret *= val;
    }
    val *= val;
  }
  return ret;
}
} // namespace lib

template <class T, class Op = std::plus<>> class seg_tree {
  std::vector<T> vec;
  T unit;
  std::size_t sz;
  Op op;
  static std::size_t get_size(std::size_t s) {
    std::size_t ret = 1;
    while (ret < s) {
      ret <<= 1;
    }
    return ret;
  }

public:
  seg_tree(std::size_t size, T u = T(), Op f = Op())
      : vec(), unit(u), sz(get_size(size)), op(f) {
    vec.assign(2 * sz, unit);
  }
  template <class Range, class = decltype(std::declval<Range>().size())>
  seg_tree(Range const &rng, T u = T(), Op f = Op())
      : seg_tree(rng.size(), u, f) {
    set(rng);
  }
  template <class Range> void set(Range const &rng) {
    std::size_t i = 0;
    for (auto &&v : rng) {
      vec[i + sz] = v;
      if (++i >= sz) {
        break;
      }
    }
    for (auto s = sz / 2; s > 0; s /= 2) {
      for (index_t i = 0; i < s; ++i) {
        vec[(i + s)] = op(vec[2 * (i + s)], vec[2 * (i + s) + 1]);
      }
    }
  }
  T operator()(std::size_t start, std::size_t end) const {
    T L = unit, R = unit;
    for (std::size_t a = start + sz, b = end + sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1) {
        L = op(L, vec[a++]);
      }
      if (b & 1) {
        R = op(vec[--b], R);
      }
    }
    return op(L, R);
  }
  void update(std::size_t i, T const &v) {
    auto k = i + sz;
    vec[k] = v;
    while (k >>= 1) {
      vec[k] = op(vec[2 * k], vec[2 * k + 1]);
    }
  }
};

template <class T> struct max_value {
  T v;
  max_value(T u = lib::min_v<T>) : v(u) {}
  auto &operator=(T const &u) {
    v = std::max(v, u);
    return *this;
  }
  auto &operator=(max_value const &u) {
    v = std::max(v, *u);
    return *this;
  }
  T operator*() const { return v; }
  void reset() { v = lib::min_v<T>; }
};

void Main() {
  int N, K;
  std::cin >> N >> K;
  seg_tree<int, lib::max_t> tree(N);
  max_value<int> max;
  for (auto _ : boost::irange(N)) {
    int p;
    std::cin >> p;
    auto left = std::max(0, p - K);
    auto right = std::min(N, p + K + 1);
    auto v = tree(left, right);
    max = v + 1;
    tree.update(p, v + 1);
  }
  std::cout << *max << std::endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}