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

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

// 引用元：https://drken1215.hatenablog.com/entry/2020/09/27/080300
template <class Monoid> struct SegmentTree {
  using Func = function<Monoid(Monoid, Monoid)>;
  const Func F;
  const Monoid UNITY;
  int SIZE_R;
  vector<Monoid> dat;

  SegmentTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
    init(n);
  }
  void init(int n) {
    SIZE_R = 1;
    while (SIZE_R < n)
      SIZE_R *= 2;
    dat.assign(SIZE_R * 2, UNITY);
  }

  /* set, a is 0-indexed */
  void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
  void build() {
    for (int k = SIZE_R - 1; k > 0; --k)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* update a, a is 0-indexed */
  void update(int a, const Monoid &v) {
    // まず最下段の、元の配列に相当する箇所を更新
    int k = a + SIZE_R;
    dat[k] = v;
    // 登りながら更新
    while (k >>= 1)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* query [a, b), a and b are 0-indexed */
  Monoid query(int a, int b) {
    Monoid vleft = UNITY, vright = UNITY;
    for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
         left >>= 1, right >>= 1) {
      if (left & 1)
        vleft = F(vleft, dat[left++]);
      if (right & 1)
        vright = F(dat[--right], vright);
    }
    return F(vleft, vright);
  }
  inline Monoid operator[](int a) { return dat[a + SIZE_R]; }

  /* debug */
  void print() {
    for (int i = 0; i < SIZE_R; ++i) {
      cout << (*this)[i];
      if (i != SIZE_R - 1)
        cout << ",";
    }
    cout << endl;
  }
};

LL N, K;
LL A[300000];

LL ans;

void solve() {
  // ラストがiで終わるような数列の最大の長さをdp[i]とする
  const LL MAX_A =　300000;
  auto func = [&](LL a, LL b) { return max(a, b); };
  SegmentTree<LL> st(MAX_A + 1, func, 0);
  for (int i = 0; i < N; i++) {
    LL left = max(0LL, A[i] - K);
    LL right = min(MAX_A, A[i] + K);
    LL cand = st.query(left, right + 1);
    st.update(A[i], cand + 1);
  }
  ans = st.query(1, MAX_A + 1);
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

// 引用元：https://drken1215.hatenablog.com/entry/2020/09/27/080300
template <class Monoid> struct SegmentTree {
  using Func = function<Monoid(Monoid, Monoid)>;
  const Func F;
  const Monoid UNITY;
  int SIZE_R;
  vector<Monoid> dat;

  SegmentTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
    init(n);
  }
  void init(int n) {
    SIZE_R = 1;
    while (SIZE_R < n)
      SIZE_R *= 2;
    dat.assign(SIZE_R * 2, UNITY);
  }

  /* set, a is 0-indexed */
  void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
  void build() {
    for (int k = SIZE_R - 1; k > 0; --k)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* update a, a is 0-indexed */
  void update(int a, const Monoid &v) {
    // まず最下段の、元の配列に相当する箇所を更新
    int k = a + SIZE_R;
    dat[k] = v;
    // 登りながら更新
    while (k >>= 1)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* query [a, b), a and b are 0-indexed */
  Monoid query(int a, int b) {
    Monoid vleft = UNITY, vright = UNITY;
    for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
         left >>= 1, right >>= 1) {
      if (left & 1)
        vleft = F(vleft, dat[left++]);
      if (right & 1)
        vright = F(dat[--right], vright);
    }
    return F(vleft, vright);
  }
  inline Monoid operator[](int a) { return dat[a + SIZE_R]; }

  /* debug */
  void print() {
    for (int i = 0; i < SIZE_R; ++i) {
      cout << (*this)[i];
      if (i != SIZE_R - 1)
        cout << ",";
    }
    cout << endl;
  }
};

LL N, K;
LL A[300000];

LL ans;

void solve() {
  // ラストがiで終わるような数列の最大の長さをdp[i]とする
  auto func = [&](LL a, LL b) { return max(a, b); };
  SegmentTree<LL> st(N + 2, func, 0);
  for (int i = 0; i < N; i++) {
    LL left = max(0LL, A[i] - K);
    LL right = min(N, A[i] + K);
    LL cand = st.query(left, right + 1);
    st.update(A[i], cand + 1);
  }
  ans = st.query(1, N + 1);
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  solve();

  cout << ans << endl;
  return 0;
}