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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpll vector<pll>
#define vpii vector<pii>
#define umap unordered_map
#define uset unordered_set
#define PQ priority_queue
#define printa(a, L, R)                                                        \\
  for (int i = L; i < R; i++)                                                  \\
  cout << a[i] << (i == R - 1 ? '\\n' : ' ')
#define printv(a) printa(a, 0, a.size())
#define print2d(a, r, c)                                                       \\
  for (int i = 0; i < r; i++)                                                  \\
    for (int j = 0; j < c; j++)                                                \\
  cout << a[i][j] << (j == c - 1 ? '\\n' : ' ')
#define pb push_back
#define eb emplace_back
#define UB upper_bound
#define LB lower_bound
#define F first
#define S second
#define mem(a, x) memset(a, x, sizeof(a))
#define inf 1e18
#define E 2.71828182845904523536
#define gamma 0.5772156649
#define nl "\\n"
#define lg(r, n) (int)(log2(n) / log2(r))
#define rev(v) reverse(v.begin(), v.end())
#define srt(v) sort(v.begin(), v.end())
#define grtsrt(v) sort(v.begin(), v.end(), greater<ll>())
#define all(v) v.begin(), v.end()
#define mnv(v) *min_element(v.begin(), v.end())
#define mxv(v) *max_element(v.begin(), v.end())
#define toint(a) atoi(a.c_str())
#define BeatMeScanf ios_base::sync_with_stdio(false)
#define one(x) __builtin_popcount(x)
#define Unique(v) v.erase(unique(all(v)), v.end())
#define fout(x) fixed << setprecision(x)

const int mod = 1e9 + 7;
const int N = 3e5 + 9;
const ld eps = 1e-9;
const ld PI = acos(-1.0);
ll gc(ll a, ll b) {
  while (b) {
    ll x = a % b;
    a = b;
    b = x;
  }
  return a;
}
ll lc(ll a, ll b) { return a / gc(a, b) * b; }
ll qpow(ll n, ll k) {
  ll ans = 1;
  assert(k >= 0);
  n %= mod;
  while (k > 0) {
    if (k & 1)
      ans = (ans * n) % mod;
    n = (n * n) % mod;
    k >>= 1;
  }
  return ans % mod;
}

#define MX 300000LL

ll t[1400000];

ll mx(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  return max(mx(v * 2, tl, tm, l, min(r, tm)),
             mx(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(ll v, ll tl, ll tr, ll pos, ll new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    ll tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

void solve() {
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    ll y = mx(1, 0, MX, max(0LL, x - k), min(MX, x + k));
    update(1, 0, MX, x, y + 1);
  }
  cout << t[1] << nl;
}

int main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpll vector<pll>
#define vpii vector<pii>
#define umap unordered_map
#define uset unordered_set
#define PQ priority_queue
#define printa(a, L, R)                                                        \\
  for (int i = L; i < R; i++)                                                  \\
  cout << a[i] << (i == R - 1 ? '\\n' : ' ')
#define printv(a) printa(a, 0, a.size())
#define print2d(a, r, c)                                                       \\
  for (int i = 0; i < r; i++)                                                  \\
    for (int j = 0; j < c; j++)                                                \\
  cout << a[i][j] << (j == c - 1 ? '\\n' : ' ')
#define pb push_back
#define eb emplace_back
#define UB upper_bound
#define LB lower_bound
#define F first
#define S second
#define mem(a, x) memset(a, x, sizeof(a))
#define inf 1e18
#define E 2.71828182845904523536
#define gamma 0.5772156649
#define nl "\\n"
#define lg(r, n) (int)(log2(n) / log2(r))
#define rev(v) reverse(v.begin(), v.end())
#define srt(v) sort(v.begin(), v.end())
#define grtsrt(v) sort(v.begin(), v.end(), greater<ll>())
#define all(v) v.begin(), v.end()
#define mnv(v) *min_element(v.begin(), v.end())
#define mxv(v) *max_element(v.begin(), v.end())
#define toint(a) atoi(a.c_str())
#define BeatMeScanf ios_base::sync_with_stdio(false)
#define one(x) __builtin_popcount(x)
#define Unique(v) v.erase(unique(all(v)), v.end())
#define fout(x) fixed << setprecision(x)

const int mod = 1e9 + 7;
const int N = 3e5 + 9;
const ld eps = 1e-9;
const ld PI = acos(-1.0);
ll gc(ll a, ll b) {
  while (b) {
    ll x = a % b;
    a = b;
    b = x;
  }
  return a;
}
ll lc(ll a, ll b) { return a / gc(a, b) * b; }
ll qpow(ll n, ll k) {
  ll ans = 1;
  assert(k >= 0);
  n %= mod;
  while (k > 0) {
    if (k & 1)
      ans = (ans * n) % mod;
    n = (n * n) % mod;
    k >>= 1;
  }
  return ans % mod;
}

#define MX 300000

ll t[1400000];

ll mx(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(mx(v * 2, tl, tm, l, min(r, tm)),
             mx(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

void solve() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    ll y = mx(1, 1, MX, max(1LL, x - k), x + k);
    update(1, 1, MX, x, y + 1);
  }
  cout << t[1] << nl;
}

int main() {
  solve();
  return 0;
}