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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const int MX = 3e5 + 5;
const int MOD = 1e9 + 7; // 998244353;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

constexpr int pct(int x) { return __builtin_popcount(x); }  // number of ones
constexpr int bits(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
template <class T> T cdiv(T a, T b) {
  return a / b + !(a < 0 || a % b == 0);
} // division of a by b rounded up, assumes b > 0

int fstTrue(function<bool(int)> f, int lo, int hi) {
  hi++;
  assert(lo <= hi); // assuming f is increasing
  while (lo < hi) { // find first index such that f is true
    int mid = (lo + hi) / 2;
    f(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}

template <class T> void remDup(vector<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), end(v));
}

// DEBUG
void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
  cerr << h;
  if (sizeof...(t))
    cerr << ", ";
  DBG(t...);
}
#define dbg(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [",           \\
      DBG(__VA_ARGS__)

// FILE I/O
void setIn(str S) { freopen(S.c_str(), "r", stdin); }
void setOut(str S) { freopen(S.c_str(), "w", stdout); }
void setIO(str S = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // fast I/O
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(S)) {
    setIn(S + ".in"), setOut(S + ".out");
  } // for USACO
}

template <class T> struct Seg { // comb(ID,b) = b
  const T ID = 0;
  T comb(T a, T b) { return max(a, b); }
  int N;
  vector<T> seg;
  void init(int _N) {
    N = _N;
    seg.assign(2 * N, ID);
  }
  void pull(int p) { seg[p] = comb(seg[2 * p], seg[2 * p + 1]); }
  void upd(int p, T val) { // set val at position p
    p += N, seg[p] = max(seg[p], val);
    for (p /= 2; p; p /= 2)
      pull(p);
  }
  T query(int l, int r) {
    T ra = ID, rb = ID;
    for (l += N, r += N + 1; l < r; l /= 2, r /= 2) {
      if (l & 1)
        ra = comb(ra, seg[l++]);
      if (r & 1)
        rb = comb(seg[--r], rb);
    }
    return comb(ra, rb);
  }
};

int arr[MX];
Seg<ll> ST;

int32_t main() {

  setIO();

  int N, K;
  cin >> N >> K;
  ST.init(MX + 1);

  F0R(i, N) { cin >> arr[i]; }

  int res = 0;
  F0R(i, N) {
    int left = max(0, arr[i] - K), right = min(MX - 1, arr[i] + K);
    int value = ST.query(left, right);
    res = max(res, value + 1);
    ST.upd(arr[i], value + 1);
  }

  cout << res << "\\n";
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<db, db> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;

#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto &a : x)

const int MX = 2e5 + 5;
const int MOD = 1e9 + 7; // 998244353;
const ll INF = 1e18;
const ld PI = acos((ld)-1);

const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

constexpr int pct(int x) { return __builtin_popcount(x); }  // number of ones
constexpr int bits(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
template <class T> T cdiv(T a, T b) {
  return a / b + !(a < 0 || a % b == 0);
} // division of a by b rounded up, assumes b > 0

int fstTrue(function<bool(int)> f, int lo, int hi) {
  hi++;
  assert(lo <= hi); // assuming f is increasing
  while (lo < hi) { // find first index such that f is true
    int mid = (lo + hi) / 2;
    f(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}

template <class T> void remDup(vector<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), end(v));
}

// DEBUG
void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
  cerr << h;
  if (sizeof...(t))
    cerr << ", ";
  DBG(t...);
}
#define dbg(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [",           \\
      DBG(__VA_ARGS__)

// FILE I/O
void setIn(str S) { freopen(S.c_str(), "r", stdin); }
void setOut(str S) { freopen(S.c_str(), "w", stdout); }
void setIO(str S = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0); // fast I/O
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(S)) {
    setIn(S + ".in"), setOut(S + ".out");
  } // for USACO
}

template <class T> struct Seg { // comb(ID,b) = b
  const T ID = 0;
  T comb(T a, T b) { return max(a, b); }
  int N;
  vector<T> seg;
  void init(int _N) {
    N = _N;
    seg.assign(2 * N, ID);
  }
  void pull(int p) { seg[p] = comb(seg[2 * p], seg[2 * p + 1]); }
  void upd(int p, T val) { // set val at position p
    p += N, seg[p] = max(seg[p], val);
    for (p /= 2; p; p /= 2)
      pull(p);
  }
  T query(int l, int r) {
    T ra = ID, rb = ID;
    for (l += N, r += N + 1; l < r; l /= 2, r /= 2) {
      if (l & 1)
        ra = comb(ra, seg[l++]);
      if (r & 1)
        rb = comb(seg[--r], rb);
    }
    return comb(ra, rb);
  }
};

int arr[MX];
Seg<ll> ST;

int32_t main() {

  setIO();

  int N, K;
  cin >> N >> K;
  ST.init(N);

  F0R(i, N) { cin >> arr[i]; }

  int res = 0;
  F0R(i, N) {
    int left = max(0, arr[i] - K), right = min(N - 1, arr[i] + K);
    int value = ST.query(left, right);
    res = max(res, value + 1);
    ST.upd(arr[i], value + 1);
  }

  cout << res << "\\n";
}