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

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#define fi first
#define se second
#define endl '\\n'
#define int int64_t
#define double long double
// template<int D, typename T>struct dense:public
// vector<dense<D-1,T>>{static_assert(D>=1,"dim err");template<typename...
// Args>dense(int n=0,Args...
// args):vector<dense<D-1,T>>(n,dense<D-1,T>(args...)){}}; template<typename
// T>struct dense<1,T>:public vector<T>{dense(int n=0,const T&
// val=T()):vector<T>(n,val){}}; template<typename T> using ordered_set =
// tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// template<typename T> using reverse_queue = priority_queue<T, vector<T>,
// greater<T>>; mt19937_64 rnd(time(0)); const int FIXED_RANDOM = rnd();
// template<typename T> struct custom_hash{T operator()(T x)const{return (T)
// x^FIXED_RANDOM;}}; // check hash in whether int64 or int32 template<typename
// T1, typename T2> using hash_map = gp_hash_table<T1, T2, custom_hash<T1>>;
const int MAXN = (int)3e5 + 2;
const int INF = numeric_limits<int>::max() >> 1;
const int MOD = (int)1000000007;
// Tao luyện kỹ thuật trong 8 năm mày luyện kỹ thuật trong buồng tắm nam
struct lazy_segment_tree {
  using it_type = long long;
  int n;
  vector<it_type> it;
  vector<long long> lz;
  const it_type FAILED_STAGE = -INF;
  void init(int _) {
    n = _;
    it.resize((n << 2) | 1, -INF);
    lz.resize((n << 2) | 1, -INF); // check init lazy
  }
  it_type merge(it_type a, it_type b) { return max(a, b); }
  // it_type build(int u, int l, int r, auto &a){ // 1 indexed
  //   if(l == r) return it[u] = a[l];
  //   return it[u] = merge(build(u << 1, l, ((l + r)>>1), a), build((u << 1)|1,
  //   ((l + r)>>1) + 1, r, a)); // merge stage
  // }
  void lazy(int u, int l, int r) { // lazy
    if (lz[u] == -INF)
      return;
    it[u] = max(it[u], lz[u]);
    if (l != r) {
      lz[u << 1] = max(lz[u << 1], lz[u]);
      lz[(u << 1) | 1] = max(lz[(u << 1) | 1], lz[u]);
    }
    lz[u] = -INF;
  }
  void update(int u, int l, int r, int L, int R, int val) {
    if (R < L)
      return;
    lazy(u, l, r);
    if (l > R || r < L)
      return;
    if (L <= l && r <= R) {
      lz[u] = val; // lazy update
      lazy(u, l, r);
      return;
    }
    update(u << 1, l, ((l + r) >> 1), L, R, val);
    update((u << 1) | 1, ((l + r) >> 1) + 1, r, L, R, val);
    it[u] = merge(it[u << 1], it[(u << 1) | 1]); // merge stage
  }
  it_type get(int u, int l, int r, int L, int R) {
    if (l > R || r < L)
      return FAILED_STAGE; // check for failed stage
    lazy(u, l, r);
    if (L <= l && r <= R)
      return it[u];
    return merge(get(u << 1, l, ((l + r) >> 1), L, R),
                 get((u << 1) | 1, ((l + r) >> 1) + 1, r, L, R)); // merge stage
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("FDT.txt", "r", stdin);
  lazy_segment_tree tr;
  int n, k;
  cin >> n >> k;
  tr.init(300001);
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--) {
    int lf = a[i] - k;
    if (lf < 0)
      lf = 0;
    int rt = a[i] + k;
    if (rt > 300000)
      rt = 300000;
    int d = tr.get(1, 1, 300001, lf + 1, rt + 1);
    if (d < 0)
      d = 0;
    d++;
    tr.update(1, 1, 300001, a[i] + 1, a[i] + 1, d);
  }
  cout << tr.get(1, 1, 300001, 1, 300001);
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#define fi first
#define se second
#define endl '\\n'
#define int int64_t
#define double long double
// template<int D, typename T>struct dense:public
// vector<dense<D-1,T>>{static_assert(D>=1,"dim err");template<typename...
// Args>dense(int n=0,Args...
// args):vector<dense<D-1,T>>(n,dense<D-1,T>(args...)){}}; template<typename
// T>struct dense<1,T>:public vector<T>{dense(int n=0,const T&
// val=T()):vector<T>(n,val){}}; template<typename T> using ordered_set =
// tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// template<typename T> using reverse_queue = priority_queue<T, vector<T>,
// greater<T>>; mt19937_64 rnd(time(0)); const int FIXED_RANDOM = rnd();
// template<typename T> struct custom_hash{T operator()(T x)const{return (T)
// x^FIXED_RANDOM;}}; // check hash in whether int64 or int32 template<typename
// T1, typename T2> using hash_map = gp_hash_table<T1, T2, custom_hash<T1>>;
const int MAXN = (int)3e5 + 2;
const int INF = numeric_limits<int>::max() >> 1;
const int MOD = (int)1000000007;
// Tao luyện kỹ thuật trong 8 năm mày luyện kỹ thuật trong buồng tắm nam
int it[MAXN];
void update(int id, int l, int r, int p, int val) {
  if (l > p || r < p || l > r)
    return;
  if (l == p && r == p) {
    if (it[id] < val)
      it[id] = val;
    return;
  }
  int mid = (l + r) / 2;
  update(id << 1, l, mid, p, val);
  update(id << 1 | 1, mid + 1, r, p, val);
  it[id] = max(it[id << 1], it[id << 1 | 1]);
}
int get(int id, int l, int r, int L, int R) {
  if (l > r || l > R || r < L)
    return -INF;
  if (L <= l && r <= R)
    return it[id];
  int mid = (l + r) / 2;
  return max(get(id << 1, l, mid, L, R), get(id << 1 | 1, mid + 1, r, L, R));
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  freopen("FDT.txt", "r", stdin);
  fill(it, it + MAXN, -INF);
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = n; i >= 1; i--) {
    int lf = a[i] - k;
    if (lf < 0)
      lf = 0;
    int rt = a[i] + k;
    if (rt > n)
      rt = n;
    int d = get(1, 1, n, lf, rt);
    if (d < 0)
      d = 0;
    d++;
    update(1, 1, n, a[i], d);
  }
  cout << get(1, 1, n, 0, n);
}