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

#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;

#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using ml = boost::multiprecision::cpp_int;
using md = boost::multiprecision::cpp_dec_float_100;
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
using mg = vector<vector<long long>>;
using lg = vector<vector<long long>>;
using wlg = vector<vector<pair<long long, long long>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutld cout << fixed << setprecision(10) // cout double
#define output(x)                                                              \\
  do {                                                                         \\
    cout << x << endl;                                                         \\
    exit(0);                                                                   \\
  } while (0)
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...)                                                           \\
  if (ll i = -1)                                                               \\
    for (__VA_ARGS__)                                                          \\
      if (i++, 1)
#define each(i, e, c) fori(i, auto &&e : c) // for each
/***** const value *****/
#define llong_max 9223372036854775807 // 9 * 10^18
#define ldbl_max 1.79769e+308         // 1.7 * 10^308
#define pi 3.1415926535897932         // 3.14 ...
/***** lambda *****/
auto Ceil = [] // if (a % b != 0) return a / b + 1;
    (auto x) { return (ll)ceil(x); };
auto Count = [] // long long count value
    (auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
    (auto c) { return (ll)c - (ll)'0'; };
auto LtoC = [] // number to char
    (auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
    (auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
    (auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
    (auto n) { return (ll)pow(10, n); };
auto Size = [] // long long collection size
    (auto &c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
    (auto b, auto e) { return accumulate(b, e, 0LL); };

/***** operator *****/
template <class T, class S>
pair<T, S> operator+(pair<T, S> l, pair<T, S> r) { // pair<T, S> + pair<T, S>
  return {l.first + r.first, l.second + r.second};
}
template <class T, class S>
pair<T, S> operator-(pair<T, S> l, pair<T, S> r) { // pair<T, S> - pair<T, S>
  return {l.first - r.first, l.second - r.second};
}
/***** template *****/
template <class A, class B, class C> struct triple { // tuple<A, B, C>
  A a = A();
  B b = B();
  C c = C();
};
template <class T>
void MakeVVT(ll ys, ll xs, vvt<T> &v,
             T fill = T()) { // vector<vector<T>> resize + fill
  v.resize(ys);
  rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T>
void MakeVVVT(ll zs, ll ys, ll xs, vvvt<T> &v,
              T fill = T()) { // vector<vector<vector<T>>> resize + fill
  v.resize(zs);
  rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T>
void InputVVT(ll ys, ll xs, vvt<T> &v,
              T fix = T()) { // input vector<vector<T>> (T != struct) + fix
  MakeVVT(ys, xs, v, fix);
  rep(y, 0, ys) rep(x, 0, xs) {
    cin >> v[y][x];
    v[y][x] += fix;
  }
}
template <class T>
void InputVVVT(
    ll zs, ll ys, ll xs, vvvt<T> &v,
    T fix = T()) { // input vector<vector<vector<T>>> (T != struct) + fix
  v.resize(zs);
  rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget {
template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S),
          F (*composition)(F, F), F (*id)()>
struct DS_NyaaLST {
public:
  DS_NyaaLST() : DS_NyaaLST(0) {}
  DS_NyaaLST(int n) : DS_NyaaLST(std::vector<S>(n, e())) {}
  DS_NyaaLST(const std::vector<S> &v) : _n(int(v.size())) {
    log = ceil_pow2(_n);
    size = 1 << log;
    d = std::vector<S>(2 * size, e());
    lz = std::vector<F>(size, id());
    for (int i = 0; i < _n; i++)
      d[size + i] = v[i];
    for (int i = size - 1; i >= 1; i--) {
      update(i);
    }
  }

  void set(int p, S x) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    d[p] = x;
    for (int i = 1; i <= log; i++)
      update(p >> i);
  }

  S get(int p) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    return d[p];
  }

  S prod(long long L, long long R) { // long long -> int 型変換
    int l = (int)L;
    int r = (int)R;
    assert(0 <= l && l <= r && r <= _n);
    if (l == r)
      return e();

    l += size;
    r += size;

    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l)
        push(l >> i);
      if (((r >> i) << i) != r)
        push(r >> i);
    }

    S sml = e(), smr = e();
    while (l < r) {
      if (l & 1)
        sml = op(sml, d[l++]);
      if (r & 1)
        smr = op(d[--r], smr);
      l >>= 1;
      r >>= 1;
    }

    return op(sml, smr);
  }

  S all_prod() { return d[1]; }

  void apply(int p, F f) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    d[p] = mapping(f, d[p]);
    for (int i = 1; i <= log; i++)
      update(p >> i);
  }

  void apply(long long L, long long R, F f) { // long long -> int 型変換
    int l = (int)L;
    int r = (int)R;
    assert(0 <= l && l <= r && r <= _n);
    if (l == r)
      return;

    l += size;
    r += size;

    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l)
        push(l >> i);
      if (((r >> i) << i) != r)
        push((r - 1) >> i);
    }

    {
      int l2 = l, r2 = r;
      while (l < r) {
        if (l & 1)
          all_apply(l++, f);
        if (r & 1)
          all_apply(--r, f);
        l >>= 1;
        r >>= 1;
      }
      l = l2;
      r = r2;
    }

    for (int i = 1; i <= log; i++) {
      if (((l >> i) << i) != l)
        update(l >> i);
      if (((r >> i) << i) != r)
        update((r - 1) >> i);
    }
  }

  template <bool (*g)(S)> int max_right(int l) {
    return max_right(l, [](S x) { return g(x); });
  }

  template <class G> int max_right(int l, G g) {
    assert(0 <= l && l <= _n);
    assert(g(e()));
    if (l == _n)
      return _n;
    l += size;
    for (int i = log; i >= 1; i--)
      push(l >> i);
    S sm = e();
    do {
      while (l % 2 == 0)
        l >>= 1;
      if (!g(op(sm, d[l]))) {
        while (l < size) {
          push(l);
          l = (2 * l);
          if (g(op(sm, d[l]))) {
            sm = op(sm, d[l]);
            l++;
          }
        }
        return l - size;
      }
      sm = op(sm, d[l]);
      l++;
    } while ((l & -l) != l);
    return _n;
  }

  template <bool (*g)(S)> int min_left(int r) {
    return min_left(r, [](S x) { return g(x); });
  }

  template <class G> int min_left(int r, G g) {
    assert(0 <= r && r <= _n);
    assert(g(e()));
    if (r == 0)
      return 0;
    r += size;
    for (int i = log; i >= 1; i--)
      push((r - 1) >> i);
    S sm = e();
    do {
      r--;
      while (r > 1 && (r % 2))
        r >>= 1;
      if (!g(op(d[r], sm))) {
        while (r < size) {
          push(r);
          r = (2 * r + 1);
          if (g(op(d[r], sm))) {
            sm = op(d[r], sm);
            r--;
          }
        }
        return r + 1 - size;
      }
      sm = op(d[r], sm);
    } while ((r & -r) != r);
    return 0;
  }

private:
  int _n, size, log;
  std::vector<S> d;
  std::vector<F> lz;
  void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
  void all_apply(int k, F f) {
    d[k] = mapping(f, d[k]);
    if (k < size)
      lz[k] = composition(f, lz[k]);
  }
  void push(int k) {
    all_apply(2 * k, lz[k]);
    all_apply(2 * k + 1, lz[k]);
    lz[k] = id();
  }
  // @param n `0 <= n`
  // @return minimum non-negative `x` s.t. `n <= 2**x`
  int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n))
      x++;
    return x;
  }
};
} // namespace NyaGadget

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
// using mll = NT_ModLL< 1000000007 >;
// using mll = NT_ModLL< 998244353 >;

// LST定義（範囲更新、範囲最大値取得）
using TypeLST = long long;
const TypeLST INF = 8e18;
const TypeLST ID = 8e18;
TypeLST op(TypeLST a, TypeLST b) { return std::max(a, b); }
TypeLST e() { return -INF; }
TypeLST mapping(TypeLST f, TypeLST x) { return (f == ID ? x : f); }
TypeLST composition(TypeLST f, TypeLST g) { return (f == ID ? g : f); }
TypeLST id() { return ID; }

int main() {
  ll N, K;
  cin >> N >> K;
  vt<ll> A(N);
  each(i, e, A) cin >> e;
  DS_NyaaLST<TypeLST, op, e, TypeLST, mapping, composition, id> lst(
      vt<TypeLST>(300000 + 2));

  lst.apply(A[0], A[0] + 1, 1);
  rep(i, 1, Size(A)) lst.apply(
      A[i], A[i] + 1,
      lst.prod(max(0LL, A[i] - K), min(300000LL + 1, A[i] + K + 1)) + 1);

  ll ans = 0;
  rep(i, 0, Size(A)) ans = max(ans, lst.prod(A[i], A[i] + 1));
  cout << ans;
  return 0;
}


Error: -6
6e267a69-9522-4221-a6b2-bfc71e0b37a1.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02537/C++/s182087722.cpp:139: S NyaGadget::DS_NyaaLST<S, op, e, F, mapping, composition, id>::prod(long long int, long long int) [with S = long long int; S (* op)(S, S) = op; S (* e)() = e; F = long long int; S (* mapping)(F, S) = mapping; F (* composition)(F, F) = composition; F (* id)() = id]: Assertion `0 <= l && l <= r && r <= _n' failed.


Fixed submission:

#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;

#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using ml = boost::multiprecision::cpp_int;
using md = boost::multiprecision::cpp_dec_float_100;
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
using mg = vector<vector<long long>>;
using lg = vector<vector<long long>>;
using wlg = vector<vector<pair<long long, long long>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutld cout << fixed << setprecision(10) // cout double
#define output(x)                                                              \\
  do {                                                                         \\
    cout << x << endl;                                                         \\
    exit(0);                                                                   \\
  } while (0)
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...)                                                           \\
  if (ll i = -1)                                                               \\
    for (__VA_ARGS__)                                                          \\
      if (i++, 1)
#define each(i, e, c) fori(i, auto &&e : c) // for each
/***** const value *****/
#define llong_max 9223372036854775807 // 9 * 10^18
#define ldbl_max 1.79769e+308         // 1.7 * 10^308
#define pi 3.1415926535897932         // 3.14 ...
/***** lambda *****/
auto Ceil = [] // if (a % b != 0) return a / b + 1;
    (auto x) { return (ll)ceil(x); };
auto Count = [] // long long count value
    (auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
    (auto c) { return (ll)c - (ll)'0'; };
auto LtoC = [] // number to char
    (auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
    (auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
    (auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
    (auto n) { return (ll)pow(10, n); };
auto Size = [] // long long collection size
    (auto &c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
    (auto b, auto e) { return accumulate(b, e, 0LL); };

/***** operator *****/
template <class T, class S>
pair<T, S> operator+(pair<T, S> l, pair<T, S> r) { // pair<T, S> + pair<T, S>
  return {l.first + r.first, l.second + r.second};
}
template <class T, class S>
pair<T, S> operator-(pair<T, S> l, pair<T, S> r) { // pair<T, S> - pair<T, S>
  return {l.first - r.first, l.second - r.second};
}
/***** template *****/
template <class A, class B, class C> struct triple { // tuple<A, B, C>
  A a = A();
  B b = B();
  C c = C();
};
template <class T>
void MakeVVT(ll ys, ll xs, vvt<T> &v,
             T fill = T()) { // vector<vector<T>> resize + fill
  v.resize(ys);
  rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T>
void MakeVVVT(ll zs, ll ys, ll xs, vvvt<T> &v,
              T fill = T()) { // vector<vector<vector<T>>> resize + fill
  v.resize(zs);
  rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T>
void InputVVT(ll ys, ll xs, vvt<T> &v,
              T fix = T()) { // input vector<vector<T>> (T != struct) + fix
  MakeVVT(ys, xs, v, fix);
  rep(y, 0, ys) rep(x, 0, xs) {
    cin >> v[y][x];
    v[y][x] += fix;
  }
}
template <class T>
void InputVVVT(
    ll zs, ll ys, ll xs, vvvt<T> &v,
    T fix = T()) { // input vector<vector<vector<T>>> (T != struct) + fix
  v.resize(zs);
  rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget {
template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S),
          F (*composition)(F, F), F (*id)()>
struct DS_NyaaLST {
public:
  DS_NyaaLST() : DS_NyaaLST(0) {}
  DS_NyaaLST(int n) : DS_NyaaLST(std::vector<S>(n, e())) {}
  DS_NyaaLST(const std::vector<S> &v) : _n(int(v.size())) {
    log = ceil_pow2(_n);
    size = 1 << log;
    d = std::vector<S>(2 * size, e());
    lz = std::vector<F>(size, id());
    for (int i = 0; i < _n; i++)
      d[size + i] = v[i];
    for (int i = size - 1; i >= 1; i--) {
      update(i);
    }
  }

  void set(int p, S x) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    d[p] = x;
    for (int i = 1; i <= log; i++)
      update(p >> i);
  }

  S get(int p) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    return d[p];
  }

  S prod(long long L, long long R) { // long long -> int 型変換
    int l = (int)L;
    int r = (int)R;
    assert(0 <= l && l <= r && r <= _n);
    if (l == r)
      return e();

    l += size;
    r += size;

    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l)
        push(l >> i);
      if (((r >> i) << i) != r)
        push(r >> i);
    }

    S sml = e(), smr = e();
    while (l < r) {
      if (l & 1)
        sml = op(sml, d[l++]);
      if (r & 1)
        smr = op(d[--r], smr);
      l >>= 1;
      r >>= 1;
    }

    return op(sml, smr);
  }

  S all_prod() { return d[1]; }

  void apply(int p, F f) {
    assert(0 <= p && p < _n);
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    d[p] = mapping(f, d[p]);
    for (int i = 1; i <= log; i++)
      update(p >> i);
  }

  void apply(long long L, long long R, F f) { // long long -> int 型変換
    int l = (int)L;
    int r = (int)R;
    assert(0 <= l && l <= r && r <= _n);
    if (l == r)
      return;

    l += size;
    r += size;

    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l)
        push(l >> i);
      if (((r >> i) << i) != r)
        push((r - 1) >> i);
    }

    {
      int l2 = l, r2 = r;
      while (l < r) {
        if (l & 1)
          all_apply(l++, f);
        if (r & 1)
          all_apply(--r, f);
        l >>= 1;
        r >>= 1;
      }
      l = l2;
      r = r2;
    }

    for (int i = 1; i <= log; i++) {
      if (((l >> i) << i) != l)
        update(l >> i);
      if (((r >> i) << i) != r)
        update((r - 1) >> i);
    }
  }

  template <bool (*g)(S)> int max_right(int l) {
    return max_right(l, [](S x) { return g(x); });
  }

  template <class G> int max_right(int l, G g) {
    assert(0 <= l && l <= _n);
    assert(g(e()));
    if (l == _n)
      return _n;
    l += size;
    for (int i = log; i >= 1; i--)
      push(l >> i);
    S sm = e();
    do {
      while (l % 2 == 0)
        l >>= 1;
      if (!g(op(sm, d[l]))) {
        while (l < size) {
          push(l);
          l = (2 * l);
          if (g(op(sm, d[l]))) {
            sm = op(sm, d[l]);
            l++;
          }
        }
        return l - size;
      }
      sm = op(sm, d[l]);
      l++;
    } while ((l & -l) != l);
    return _n;
  }

  template <bool (*g)(S)> int min_left(int r) {
    return min_left(r, [](S x) { return g(x); });
  }

  template <class G> int min_left(int r, G g) {
    assert(0 <= r && r <= _n);
    assert(g(e()));
    if (r == 0)
      return 0;
    r += size;
    for (int i = log; i >= 1; i--)
      push((r - 1) >> i);
    S sm = e();
    do {
      r--;
      while (r > 1 && (r % 2))
        r >>= 1;
      if (!g(op(d[r], sm))) {
        while (r < size) {
          push(r);
          r = (2 * r + 1);
          if (g(op(d[r], sm))) {
            sm = op(d[r], sm);
            r--;
          }
        }
        return r + 1 - size;
      }
      sm = op(d[r], sm);
    } while ((r & -r) != r);
    return 0;
  }

private:
  int _n, size, log;
  std::vector<S> d;
  std::vector<F> lz;
  void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
  void all_apply(int k, F f) {
    d[k] = mapping(f, d[k]);
    if (k < size)
      lz[k] = composition(f, lz[k]);
  }
  void push(int k) {
    all_apply(2 * k, lz[k]);
    all_apply(2 * k + 1, lz[k]);
    lz[k] = id();
  }
  // @param n `0 <= n`
  // @return minimum non-negative `x` s.t. `n <= 2**x`
  int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n))
      x++;
    return x;
  }
};
} // namespace NyaGadget

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
// using mll = NT_ModLL< 1000000007 >;
// using mll = NT_ModLL< 998244353 >;

using TypeLST = long long;
const TypeLST LST_INF = 8e18;
TypeLST op(TypeLST a, TypeLST b) { return std::max(a, b); }
TypeLST e() { return -LST_INF; }
TypeLST mapping(TypeLST f, TypeLST x) { return f + x; }
TypeLST composition(TypeLST f, TypeLST g) { return f + g; }
TypeLST id() { return 0; }

int main() {
  int N, K;
  cin >> N >> K;
  vt<int> A(N);
  each(i, e, A) cin >> e;
  DS_NyaaLST<TypeLST, op, e, TypeLST, mapping, composition, id> lst(N);

  lst.apply(A[0], A[0] + 1, 1);
  rep(i, 1, Size(A))
      lst.apply(A[i], A[i] + 1,
                lst.prod(max(0, A[i] - K), min(300000 + 1, A[i] + K + 1)) + 1);

  ll ans = 0;
  rep(i, 0, Size(A)) ans = max(ans, lst.prod(A[i], A[i] + 1));
  cout << ans;
  return 0;
}