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

// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include "bits/stdc++.h"

using namespace std;

#define PI 3.1415926535897932384626433832795l

#define pb push_back
#define mp make_pair
#define lb lower_bound // First element which is not less than val
#define ub upper_bound // First element greater than val
#define f first
#define s second

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define endl '\\n'
#define re return
#define repz(i, a) for (ll i = 0; i < (a); i++)
#define vecin(n, v)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> v[i];
#define full(v) v.begin(), v.end()

struct S {
  int x;

  // This should be the identity element
  S() { x = 0; }

  S(int _x) : x(_x) {}
};

S op(S &a, S &b) { return {max(a.x, b.x)}; }

// Predicate for binary search. Must be true for identity.
bool f(S /*x*/) {
  // Dummy for binary search
  return true;
}

struct segtree {
  int _n;
  vector<S> t;
  int size;
  int log;

  segtree() : segtree(0) {}

  segtree(int n = -1) {
    if (n >= 0)
      init(n);
  }

  void init(int n) {
    _n = n;
    log = 0;
    while ((1U << log) < (unsigned int)(n))
      log++;
    size = 1 << log;
    t.assign(2 * size, S());
  }

  void build(vector<S> v) {
    init((int)v.size());
    assert(_n != 0);

    for (int i = 0; i < _n; i++)
      t[size + i] = v[i];

    for (int i = size - 1; i > 0; i--)
      t[i] = op(t[2 * i], t[2 * i + 1]);
  }

  void set(int p, S x) {
    assert(0 <= p && p < _n);
    p += size;
    t[p] = x;
    for (int i = 1; i <= log; i++)
      t[p >> i] = op(t[2 * (p >> i)], t[2 * (p >> i) + 1]);
  }

  S get(int p) {
    assert(0 <= p && p < _n);
    return t[p + size];
  }

  // Find result for range [l,r)
  S query(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    S ansl, ansr;
    l += size;
    r += size;
    while (l < r) {
      if (l & 1)
        ansl = op(ansl, t[l++]);
      if (r & 1)
        ansr = op(t[--r], ansr);
      l >>= 1;
      r >>= 1;
    }
    return op(ansl, ansr);
  }

  /* Binary Search */
  // Find the maximum value of r such that [l,r) is true
  int max_right(int l) {
    assert(0 <= l && l <= _n);

    if (l == _n)
      return _n;
    l += size;
    S sm;
    do {
      while (l % 2 == 0)
        l >>= 1;
      if (!f(op(sm, t[l]))) {
        while (l < size) {
          l *= 2;
          if (f(op(sm, t[l]))) {
            sm = op(sm, t[l]);
            l++;
          }
        }
        return l - size;
      }
      sm = op(sm, t[l]);
      l++;
    } while ((l & -l) != l);
    return _n;
  }

  // This is the minimum l such that [l,r) is true
  int min_left(int r) {
    assert(0 <= r && r <= _n);
    if (r == 0)
      return 0;
    r += size;
    S sm;
    do {
      r--;
      while (r > 1 && (r % 2))
        r >>= 1;
      if (!f(op(t[r], sm))) {
        while (r < size) {
          r = (2 * r + 1);
          if (f(op(t[r], sm))) {
            sm = op(t[r], sm);
            r--;
          }
        }
        return r + 1 - size;
      }
      sm = op(t[r], sm);
    } while ((r & -r) != r);
    return 0;
  }
};

const int N = 6e5 + 57;

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  vecin(n, a);
  segtree T(N);
  repz(i, n) {
    S s = T.query(max(a[i] - k, 0), a[i] + k + 1);
    T.set(a[i], {s.x + 1});
  }
  cout << T.t[1].x << endl;
  ;
}

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

  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Error: 0

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include "bits/stdc++.h"

using namespace std;

#define PI 3.1415926535897932384626433832795l

#define pb push_back
#define mp make_pair
#define lb lower_bound // First element which is not less than val
#define ub upper_bound // First element greater than val
#define f first
#define s second

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define endl '\\n'
#define re return
#define repz(i, a) for (ll i = 0; i < (a); i++)
#define vecin(n, v)                                                            \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> v[i];
#define full(v) v.begin(), v.end()

struct S {
  int x;

  // This should be the identity element
  S() { x = 0; }

  S(int _x) : x(_x) {}
};

S op(S &a, S &b) { return {max(a.x, b.x)}; }

// Predicate for binary search. Must be true for identity.
bool f(S /*x*/) {
  // Dummy for binary search
  return true;
}

struct segtree {
  int _n;
  vector<S> t;
  int size;
  int log;

  segtree() : segtree(0) {}

  segtree(int n = -1) {
    if (n >= 0)
      init(n);
  }

  void init(int n) {
    _n = n;
    log = 0;
    while ((1U << log) < (unsigned int)(n))
      log++;
    size = 1 << log;
    t.assign(2 * _n, S());
  }

  void build(vector<S> v) {
    init((int)v.size());
    assert(_n != 0);

    for (int i = 0; i < _n; i++)
      t[size + i] = v[i];

    for (int i = size - 1; i > 0; i--)
      t[i] = op(t[2 * i], t[2 * i + 1]);
  }

  void set(int p, S x) {
    assert(0 <= p && p < _n);
    p += size;
    t[p] = x;
    for (int i = 1; i <= log; i++)
      t[p >> i] = op(t[2 * (p >> i)], t[2 * (p >> i) + 1]);
  }

  S get(int p) {
    assert(0 <= p && p < _n);
    return t[p + size];
  }

  // Find result for range [l,r)
  S query(int l, int r) {
    assert(0 <= l && l <= r && r <= _n);
    S ansl, ansr;
    l += size;
    r += size;
    while (l < r) {
      if (l & 1)
        ansl = op(ansl, t[l++]);
      if (r & 1)
        ansr = op(t[--r], ansr);
      l >>= 1;
      r >>= 1;
    }
    return op(ansl, ansr);
  }

  /* Binary Search */
  // Find the maximum value of r such that [l,r) is true
  int max_right(int l) {
    assert(0 <= l && l <= _n);

    if (l == _n)
      return _n;
    l += size;
    S sm;
    do {
      while (l % 2 == 0)
        l >>= 1;
      if (!f(op(sm, t[l]))) {
        while (l < size) {
          l *= 2;
          if (f(op(sm, t[l]))) {
            sm = op(sm, t[l]);
            l++;
          }
        }
        return l - size;
      }
      sm = op(sm, t[l]);
      l++;
    } while ((l & -l) != l);
    return _n;
  }

  // This is the minimum l such that [l,r) is true
  int min_left(int r) {
    assert(0 <= r && r <= _n);
    if (r == 0)
      return 0;
    r += size;
    S sm;
    do {
      r--;
      while (r > 1 && (r % 2))
        r >>= 1;
      if (!f(op(t[r], sm))) {
        while (r < size) {
          r = (2 * r + 1);
          if (f(op(t[r], sm))) {
            sm = op(t[r], sm);
            r--;
          }
        }
        return r + 1 - size;
      }
      sm = op(t[r], sm);
    } while ((r & -r) != r);
    return 0;
  }
};

const int N = 6e5 + 57;

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  vecin(n, a);
  segtree T(N);
  repz(i, n) {
    S s = T.query(max(a[i] - k, 0), a[i] + k + 1);
    T.set(a[i], {s.x + 1});
  }
  cout << T.t[1].x << endl;
  ;
}

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

  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}