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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define _IO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll N = 3e5 + 5, mod = 1e9 + 7, inf = 1e18;
struct node {

  ll l, r;
  ll val;
} tr[N * 4];
ll n, k;

void pushup(ll u) { tr[u].val = max(tr[u * 2].val, tr[u * 2 + 1].val); }

void build(ll u, ll l, ll r) {

  if (l == r) {
    tr[u] = {l, r, 0};
  } else {

    tr[u] = {l, r};
    ll mid = (l + r) / 2;
    build(u * 2, l, mid);
    build(u * 2 + 1, mid + 1, r);
    pushup(u);
  }
}

void modify(ll u, ll x, ll v) {

  if (tr[u].l > x || tr[u].r < x)
    return;
  if (tr[u].l == tr[u].r) {
    tr[u].val = max(tr[u].val, v);
    return;
  } else {

    // ll mid = tr[u].l+tr[u].r>>1;
    ll mid = (tr[u].l + tr[u].r) / 2;
    modify(u * 2, x, v);
    modify(u * 2 + 1, x, v);
    pushup(u);
  }
}

ll query(ll u, ll l, ll r) {

  if (tr[u].l > r || tr[u].r < l)
    return -inf;
  if (tr[u].l >= l && tr[u].r <= r)
    return tr[u].val;
  else {
    ll mid = (tr[u].l + tr[u].r) / 2;
    return max(query(u * 2, l, r), query(u * 2 + 1, l, r));
  }
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  build(1, 0, N);

  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, max(0LL, x - k), min(N, x + k));
    modify(1, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define _IO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const ll N = 3e5 + 5, mod = 1e9 + 7, inf = 1e18;
struct node {
  ll l, r;
  ll val;
} tr[N * 4];
ll n, k;

void pushup(int u) { tr[u].val = max(tr[u << 1].val, tr[u << 1 | 1].val); }

void build(ll u, ll l, ll r) {

  if (l == r) {
    tr[u] = {l, r, 0};
  } else {

    tr[u] = {l, r};
    ll mid = l + r << 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
  }
}

void modify(ll u, ll x, ll v) {

  if (tr[u].l > x || tr[u].r < x)
    return;
  if (tr[u].l == tr[u].r) {
    tr[u].val = max(tr[u].val, v);
  } else {

    ll mid = tr[u].l + tr[u].r >> 1;
    // ll mid = (tr[u].l+tr[u].r)/2;
    modify(u << 1, x, v);
    modify(u << 1 | 1, x, v);
    pushup(u);
  }
}

ll query(ll u, ll l, ll r) {

  if (tr[u].l > r || tr[u].r < l)
    return -inf;
  if (tr[u].l >= l && tr[u].r <= r)
    return tr[u].val;
  else {
    ll mid = (tr[u].l + tr[u].r) / 2;
    ll ans = -inf;
    if (l <= mid)
      ans = max(ans, query(u << 1, l, r));
    if (r > mid)
      ans = max(ans, query(u << 1 | 1, l, r));
    return ans;
  }
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  build(1, 0, n);

  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, max(0LL, x - k), min(N, x + k));
    modify(1, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\\n";

  return 0;
}