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

/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout << "\\n";
#define ll long long int
#define REP(i, a, n) for (i = a; i <= n; i++)
#define F(i, a, b) for (i = a; i < b; i++)
#define RF(i, b, a) for (i = b; i >= a; i--)
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const ll mod = 1e9 + 7;
const ll MAXN = 300001;
const ll M = 3000000;
ll i, j, mask, test;
vector<ll> tree(M), A(MAXN);
void update(ll node, ll start, ll ends, ll idx, ll val) {
  if (start == ends) {
    // Leaf node
    A[idx] = val;
    tree[node] = val;
  } else {
    int mid = (start + ends) / 2;
    if (start <= idx && idx <= mid) {
      // If idx is in the left child, recurse on the left child
      update(2 * node, start, mid, idx, val);
    } else {
      // if idx is in the right child, recurse on the right child
      update(2 * node + 1, mid + 1, ends, idx, val);
    }
    // Internal node will have the sum of both of its children
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}
ll query(ll node, ll start, ll ends, ll l, ll r) {
  if (r < start || ends < l) {
    // range represented by a node is completely outside the given range
    return 0;
  }
  if (l <= start && ends <= r) {
    // range represented by a node is completely inside the given range
    return tree[node];
  }
  // range represented by a node is partially inside and partially outside the
  // given range
  int mid = (start + ends) / 2;
  int p1 = query(2 * node, start, mid, l, r);
  int p2 = query(2 * node + 1, mid + 1, ends, l, r);
  return max(p1, p2);
}
int main() {
  IOS
      /*#ifndef ONLINE_JUDGE
         freopen("input.txt","r",stdin);
         freopen("output.txt","w",stdout);
      #endif // ONLINE_JUDGE*/
      ll n,
      k;
  cin >> n >> k;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, MAXN - 1, max((ll)0, x - k), min(MAXN - 1, x + k));
    update(1, 0, MAXN - 1, x, p + 1);
  }
  ll maxx = 0;
  F(i, 0, MAXN) { maxx = max(maxx, A[i]); }
  cout << maxx;
  return 0;
}


Error: WA

Fixed submission:

/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout << "\\n";
#define ll long long int
#define REP(i, a, n) for (i = a; i <= n; i++)
#define F(i, a, b) for (i = a; i < b; i++)
#define RF(i, b, a) for (i = b; i >= a; i--)
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const ll mod = 1e9 + 7;
const ll MAXN = 300001;
const ll M = 1000000;
ll i, j, mask, test;
vector<ll> tree(M), A(MAXN);
void update(ll node, ll start, ll ends, ll idx, ll val) {
  if (start == ends) {
    // Leaf node
    A[idx] = val;
    tree[node] = val;
  } else {
    int mid = (start + ends) / 2;
    if (start <= idx and idx <= mid) {
      // If idx is in the left child, recurse on the left child
      update(2 * node, start, mid, idx, val);
    } else {
      // if idx is in the right child, recurse on the right child
      update(2 * node + 1, mid + 1, ends, idx, val);
    }
    // Internal node will have the sum of both of its children
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}
ll query(ll node, ll start, ll ends, ll l, ll r) {
  if (r < start || ends < l) {
    // range represented by a node is completely outside the given range
    return 0;
  }
  if (l <= start && ends <= r) {
    // range represented by a node is completely inside the given range
    return tree[node];
  }
  // range represented by a node is partially inside and partially outside the
  // given range
  int mid = (start + ends) / 2;
  int p1 = query(2 * node, start, mid, l, r);
  int p2 = query(2 * node + 1, mid + 1, ends, l, r);
  return max(p1, p2);
}
int main() {
  IOS
      /*#ifndef ONLINE_JUDGE
         freopen("input.txt","r",stdin);
         freopen("output.txt","w",stdout);
      #endif // ONLINE_JUDGE*/
      ll n,
      k;
  cin >> n >> k;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, MAXN - 1, max((ll)0, x - k), x + k);
    update(1, 0, MAXN - 1, x, p + 1);
  }
  ll maxx = 0;
  F(i, 0, MAXN) { maxx = max(maxx, A[i]); }
  cout << maxx;
  return 0;
}