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

/*
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
    Date:13/06/2020
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define int long long
#define ld long double
#define ff first
#define clr(a, x) memset(a, x, sizeof(a))
#define ss string
#define se second
#define alt(v) v.begin(), v.end()
#define pb emplace_back
#define mp make_pair
#define ipair pair<int, int>
#define fab(i, a, b) for (ll i = (a); i < (b); i++)
#define fba(i, a, b) for (ll i = (b); i >= (a); i--)
#define arr(i, n) fab(i, 0, n)
ll mod = 1000000007;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
const int inf = 1e12;
bool test = 0;

const int N = 3e5 + 5;
int mx[4 * N];

void update(int n, int st, int end, int pos, int val) {
  if (st == end && st == pos) {
    mx[n] = val;
    return;
  }
  int m = (st + end) / 2;
  if (pos <= m)
    update(2 * n, st, m, pos, val);
  if (pos > m)
    update(2 * n + 1, m + 1, end, pos, val);
  mx[n] = max(mx[2 * n], mx[2 * n + 1]);
}
int query(int n, int st, int end, int l, int r) {
  if (l <= st && end <= r)
    return mx[n];
  int m = (st + end) / 2;
  if (r <= m)
    return query(2 * n, st, m, l, r);
  else if (l > m)
    return query(2 * n + 1, m + 1, end, l, r);
  else {
    int p1, p2;
    p1 = query(2 * n, st, m, l, r);
    p2 = query(2 * n + 1, m + 1, end, l, r);
    return max(p1, p2);
  }
}

int st, last;
int n, k, x;
void solve() {
  cin >> n >> k;
  clr(mx, 0);
  st = 0, last = 300000;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    int l = max(st, x - k);
    int r = min(last, x + k);
    int x1 = query(1, st, last, l, r);
    update(1, st, last, x, x1 + 1);
  }
  cout << query(1, st, last, st, last);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // precompute();
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

/*
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
    Date:13/06/2020
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define int long long
#define ld long double
#define ff first
#define clr(a, x) memset(a, x, sizeof(a))
#define ss string
#define se second
#define alt(v) v.begin(), v.end()
#define pb emplace_back
#define mp make_pair
#define ipair pair<int, int>
#define fab(i, a, b) for (ll i = (a); i < (b); i++)
#define fba(i, a, b) for (ll i = (b); i >= (a); i--)
#define arr(i, n) fab(i, 0, n)
ll mod = 1000000007;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
const int inf = 1e12;
bool test = 0;

const int N = 3 * 1000 + 10;
int mx[4 * N];

void update(int n, int st, int end, int pos, int val) {

  if (st > pos or end < pos)
    return;
  if (st == end and st == pos) {
    mx[n] = max(mx[n], val);
    return;
  }
  int mid = (st + end) / 2;
  update(2 * n, st, mid, pos, val);
  update(2 * n + 1, mid + 1, end, pos, val);
  mx[n] = max(mx[2 * n], mx[2 * n + 1]);
}

int query(int n, int st, int end, int l, int r) {
  if (l <= st && end <= r)
    return mx[n];
  int m = (st + end) / 2;
  if (r <= m)
    return query(2 * n, st, m, l, r);
  else if (l > m)
    return query(2 * n + 1, m + 1, end, l, r);
  else {
    int p1, p2;
    p1 = query(2 * n, st, m, l, r);
    p2 = query(2 * n + 1, m + 1, end, l, r);
    return max(p1, p2);
  }
}

int st, last;
int n, k, x;
void solve() {
  cin >> n >> k;
  clr(mx, 0);
  st = 1, last = 300005;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    int l = max(st, x - k);
    int r = min(last, x + k);
    int x1 = query(1, st, last, l, r);
    update(1, st, last, x, x1 + 1);
  }
  cout << query(1, st, last, st, last);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // precompute();
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}