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

#include <bits/stdc++.h>
using namespace std;
#define int long long

#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define umap unordered_map
#define mii map<int, int>
#define pii pair<int, int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define pqmin priority_queue<int, vector<int>, greater<int>>
#define pqmax priority_queue<int>
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define ps(x, y) fixed << setprecision(y) << x
#define out(x) cout << x << endl;
#define print(a, n)                                                            \\
  rep(i, n) cout << a[i] << " ";                                               \\
  cout << endl;
#define clr(a, x) memset(a, x, sizeof a)

const int mod = 1e9 + 7;
const int mod2 = 998244353;
const int N = 3e5;
const int inf = 1e18;

//=====================================================

template <typename... T> void read(T &...args) { ((cin >> args), ...); }
template <typename... T> void write(T &&...args) {
  ((cout << args << " "), ...);
}
template <typename... T> void writeline(T &&...args) {
  ((cout << args << " "), ...);
  cout << endl;
}

int add(int a, int b) {
  int res = (a + b) % mod;
  return (res < 0) ? res + mod : res;
}
int mul(int a, int b) {
  int res = (a * 1LL * b) % mod;
  return (res < 0) ? res + mod : res;
}
string binary(int x) {
  string res = "";
  rep(i, 32) res += (1 << i & x) ? '1' : '0';
  return res;
}
int in(int x, int y, int z) {
  if (y > z)
    swap(y, z);
  return (x >= y && x <= z);
}
struct customCompare {
  bool operator()(pii x, pii y) { return x.first > y.second; }
};

//=====================================================

int tree[10000000];
int a[300005];

void pointUpdate(int i, int j, int k, int l, int val) {
  if (i > l || j < l)
    return;
  if (i == j) {
    tree[k] = a[l] = val;
    return;
  }

  int mid = (i + j) / 2;
  pointUpdate(i, mid, 2 * k, l, val);
  pointUpdate(mid + 1, j, 2 * k + 1, l, val);
  tree[k] = max(tree[2 * k], tree[2 * k + 1]);
}

int query(int i, int j, int l, int r, int k) {
  if (i > r || j < l)
    return 0;
  if (i >= l && j <= r)
    return tree[k];
  int mid = (i + j) / 2;
  return max(query(i, mid, l, r, 2 * k), query(mid + 1, j, l, r, 2 * k + 1));
}

void solve() {
  int n, k;
  cin >> n >> k;
  int b[n];
  rep(i, n) cin >> b[i];
  int mx = *max_element(b, b + n);

  rep(i, n) {
    int l = max(0LL, b[i] - k);
    int r = min(mx, b[i] + k);

    int q = query(0, mx, l, r, 1);
    pointUpdate(0, mx, 1, b[i], q + 1);
  }

  cout << query(0, mx, 1, mx, 1);
}

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

  solve();

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long

#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, a, b) for (int i = a; i < b; i++)
#define fod(i, a, b) for (int i = a; i >= b; i--)
#define umap unordered_map
#define mii map<int, int>
#define pii pair<int, int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define pqmin priority_queue<int, vector<int>, greater<int>>
#define pqmax priority_queue<int>
#define all(v) v.begin(), v.end()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define w(t)                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define ps(x, y) fixed << setprecision(y) << x
#define out(x) cout << x << endl;
#define print(a, n)                                                            \\
  rep(i, n) cout << a[i] << " ";                                               \\
  cout << endl;
#define clr(a, x) memset(a, x, sizeof a)

const int mod = 1e9 + 7;
const int mod2 = 998244353;
const int N = 3e5;
const int inf = 1e18;

//=====================================================

template <typename... T> void read(T &...args) { ((cin >> args), ...); }
template <typename... T> void write(T &&...args) {
  ((cout << args << " "), ...);
}
template <typename... T> void writeline(T &&...args) {
  ((cout << args << " "), ...);
  cout << endl;
}

int add(int a, int b) {
  int res = (a + b) % mod;
  return (res < 0) ? res + mod : res;
}
int mul(int a, int b) {
  int res = (a * 1LL * b) % mod;
  return (res < 0) ? res + mod : res;
}
string binary(int x) {
  string res = "";
  rep(i, 32) res += (1 << i & x) ? '1' : '0';
  return res;
}
int in(int x, int y, int z) {
  if (y > z)
    swap(y, z);
  return (x >= y && x <= z);
}
struct customCompare {
  bool operator()(pii x, pii y) { return x.first > y.second; }
};

//=====================================================

int tree[N + 5];
int a[10 * N + 5];

// update value of element at index i
void pointUpdate(int i, int j, int k, int l, int val) {
  if (i > l || j < l)
    return;
  if (i == j) {
    tree[k] = a[l] = val;
    return;
  }
  if (i >= l && l <= j) {
    tree[k] = max(tree[k], val);
  }

  int mid = (i + j) / 2;
  pointUpdate(i, mid, 2 * k, l, val);
  pointUpdate(mid + 1, j, 2 * k + 1, l, val);
  tree[k] = max(tree[2 * k], tree[2 * k + 1]);
}

int query(int i, int j, int l, int r, int k) {
  if (i > r || j < l)
    return 0;
  if (i >= l && j <= r)
    return tree[k];
  int mid = (i + j) / 2;
  return max(query(i, mid, l, r, 2 * k), query(mid + 1, j, l, r, 2 * k + 1));
}

void solve() {
  int n, k;
  cin >> n >> k;
  int b[n];
  rep(i, n) cin >> b[i];

  int mx = *max_element(b, b + n);
  int ans = 0;

  // int si = 1;
  // while (si < mx) si *= 2;
  // tree.resize(si, 0);

  rep(i, n) {
    int l = max(1LL, b[i] - k);
    int r = min(mx, b[i] + k);

    int q = query(1, mx, l, r, 1);
    pointUpdate(1, mx, 1, b[i], q + 1);

    remax(ans, a[b[i]]);
  }

  cout << ans;
}

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

  solve();

  return 0;
}