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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll mod = 998244353;
const int N = 500006;
const ll INF = 1e18;

struct seg_tree {

  int sz;
  vector<int> val;

  void init(int x) {
    sz = 1;
    while (sz < x)
      sz *= 2;
    val.resize(sz * 2);
  }

  int merge(int a, int b) { return max(a, b); }

  void build(int node, int l, int r, vector<int> &a) {
    if (r - l == 1) {
      if (l < a.size()) {
        val[node] = a[l];
      }
      return;
    }
    int mid = (l + r) / 2;
    build(node * 2 + 1, l, mid, a);
    build(node * 2 + 2, mid, r, a);
    val[node] = merge(val[node * 2 + 1], val[node * 2 + 2]);
  }

  void build(vector<int> &a) { build(0, 0, sz, a); }

  void upd(int node, int l, int r, int pos, ll x) {
    if (r - l == 1) {
      val[node] = x;
      return;
    }
    int mid = (l + r) / 2;
    if (mid > pos)
      upd(node * 2 + 1, l, mid, pos, x);
    if (mid <= pos)
      upd(node * 2 + 2, mid, r, pos, x);
    val[node] = merge(val[node * 2 + 1], val[node * 2 + 2]);
  }
  void upd(int pos, ll x) { upd(0, 0, sz, pos, x); }

  int ask(int node, int l, int r, int st, int dr) {
    if (dr <= l || st >= r)
      return 0;
    if (l >= st && r <= dr)
      return val[node];
    int mid = (l + r) / 2;

    return max(ask(node * 2 + 1, l, mid, st, dr),
               ask(node * 2 + 2, mid, r, st, dr));
  }
  int ask(int st, int dr) { return ask(0, 0, sz, st, dr); }
};

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

  int n, k;
  cin >> n >> k;
  seg_tree st;
  st.init(N);

  int ans = 0;
  vector<int> dp(N, 0);

  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    int mx = max(st.ask(max(0, x - k), x + 1), st.ask(x, min(N, x + k + 1)));
    dp[i] = mx + 1;
    st.upd(x, dp[i]);
    ans = max(ans, dp[i]);
  }

  cout << ans << '\\n';
  return 0;
}

// 1 1 524288
// 5 5 524292
// 4 4 131072
// 3 3 524290
// 8 8 65536
// 6 6 262146
// 9 9 524296
// 7 7 524294
// 2 2 262144
// 4 4 131072

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll mod = 998244353;
const int N = 300006;
const ll INF = 1e18;

struct seg_tree {
  vector<int> val;
  int B;
  void init(int n) {
    B = 1;
    while (B < n) {
      B *= 2;
      val.resize(B * 2);
    }
  }

  void upd(int node, int l, int r, int pos, int x) {

    if (l + 1 == r) {
      val[node] = max(val[node], x);
      return;
    }

    if (l > pos || r <= pos)
      return;

    int mid = (l + r) / 2;
    upd(node * 2 + 1, l, mid, pos, x);
    upd(node * 2 + 2, mid, r, pos, x);
    val[node] = max(val[node * 2 + 1], val[node * 2 + 2]);
  }

  void upd(int pos, int x) { upd(0, 0, B, pos, x); }

  int ask(int node, int l, int r, int lf, int rg) {
    if (l >= lf && r <= rg) {
      return val[node];
    }
    if (l >= rg || r <= lf)
      return 0;
    int mid = (l + r) / 2;
    return max(ask(node * 2 + 1, l, mid, lf, rg),
               ask(node * 2 + 2, mid, r, lf, rg));
  }
  int ask(int l, int r) { ask(0, 0, B, l, r); }
};

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

  int n, k;
  cin >> n >> k;
  seg_tree st;
  st.init(N);

  int ans = 0;
  vector<int> dp(N, 0);

  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    int mx = max(st.ask(max(1, x - k), x + 1), st.ask(x, min(N, x + k + 1)));
    dp[i] = mx + 1;
    st.upd(x, dp[i]);
    ans = max(ans, dp[i]);
  }

  cout << ans << '\\n';
  return 0;
}