Problem p02537 (C++)

Buggy submission (status: Runtime Error):
// One Batch Two Batch Penny and Dime

#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;
#define ll long long
#define hii cout << "hii" << endl
#define int long long
#define endl '\n'
#define all(s) s.begin(), s.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 1e5 + 5;
const int MAXN = 1e6 + 5;
const int M = 1e6 + 5;
const int mod = 998244353;
const int MOD = 998244353;
const int INF = 1e18 + 8;
const int LG = 21;

int arr[N];

class segtree {
public:
  vector<int> v;
  int N;

  segtree(int n) {
    v.assign(4 * n + 5, 0);
    N = n;
  }

  void buildtree(int s, int e, int treenode) {
    if (s == e) {
      v[treenode] = 0;
      return;
    }
    int mid = s + e >> 1;
    buildtree(s, mid, 2 * treenode + 1);
    buildtree(mid + 1, e, 2 * treenode + 2);
    v[treenode] = max(v[2 * treenode + 1], v[2 * treenode + 2]);
  }

  void updatetree(int idx, int val, int s, int e, int treenode) {
    if (s == e) {
      v[treenode] = val;
      return;
    }
    int mid = s + e >> 1;
    if (idx <= mid)
      updatetree(idx, val, s, mid, 2 * treenode + 1);
    else
      updatetree(idx, val, mid + 1, e, 2 * treenode + 2);
    v[treenode] = max(v[2 * treenode + 1], v[2 * treenode + 2]);
  }

  int querytree(int l, int r, int s, int e, int treenode) {
    if (s >= l and e <= r) {
      return v[treenode];
    }
    if (s > r or l > e)
      return 0;
    int mid = s + e >> 1;
    int left = querytree(l, r, s, mid, 2 * treenode + 1);
    int right = querytree(l, r, mid + 1, e, 2 * treenode + 2);
    return max(left, right);
  }

  void update(int idx, int val) { updatetree(idx, val, 0, N - 1, 0); }

  int query(int l, int r) {
    if (l > r)
      return 0;
    return querytree(l, r, 0, N - 1, 0);
  }

  void build() { buildtree(0, N - 1, 0); }
};

void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int ret = 0;
  segtree stree(300005);
  for (int i = 0; i < n; i++) {
    int l = max(0LL, arr[i] - k);
    int r = min(arr[i] + k, 300000LL);
    int temp = 1 + stree.query(l, r);
    ret = max(ret, temp);
    stree.update(arr[i], temp);
  }
  cout << ret << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
// One Batch Two Batch Penny and Dime

#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;
#define ll long long
#define hii cout << "hii" << endl
#define int long long
#define endl '\n'
#define all(s) s.begin(), s.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 5e5 + 5;
const int MAXN = 1e6 + 5;
const int M = 1e6 + 5;
const int mod = 998244353;
const int MOD = 998244353;
const int INF = 1e18 + 8;
const int LG = 21;

int arr[N];

class segtree {
public:
  vector<int> v;
  int N;

  segtree(int n) {
    v.assign(4 * n + 5, 0);
    N = n;
  }

  void buildtree(int s, int e, int treenode) {
    if (s == e) {
      v[treenode] = 0;
      return;
    }
    int mid = s + e >> 1;
    buildtree(s, mid, 2 * treenode + 1);
    buildtree(mid + 1, e, 2 * treenode + 2);
    v[treenode] = max(v[2 * treenode + 1], v[2 * treenode + 2]);
  }

  void updatetree(int idx, int val, int s, int e, int treenode) {
    if (s == e) {
      v[treenode] = val;
      return;
    }
    int mid = s + e >> 1;
    if (idx <= mid)
      updatetree(idx, val, s, mid, 2 * treenode + 1);
    else
      updatetree(idx, val, mid + 1, e, 2 * treenode + 2);
    v[treenode] = max(v[2 * treenode + 1], v[2 * treenode + 2]);
  }

  int querytree(int l, int r, int s, int e, int treenode) {
    if (s >= l and e <= r) {
      return v[treenode];
    }
    if (s > r or l > e)
      return 0;
    int mid = s + e >> 1;
    int left = querytree(l, r, s, mid, 2 * treenode + 1);
    int right = querytree(l, r, mid + 1, e, 2 * treenode + 2);
    return max(left, right);
  }

  void update(int idx, int val) { updatetree(idx, val, 0, N - 1, 0); }

  int query(int l, int r) {
    if (l > r)
      return 0;
    return querytree(l, r, 0, N - 1, 0);
  }

  void build() { buildtree(0, N - 1, 0); }
};

void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int ret = 0;
  segtree stree(300005);
  for (int i = 0; i < n; i++) {
    int l = max(0LL, arr[i] - k);
    int r = min(arr[i] + k, 300000LL);
    int temp = 1 + stree.query(l, r);
    ret = max(ret, temp);
    stree.update(arr[i], temp);
  }
  cout << ret << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}