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 node {
  int x;

  node() { x = 0; }

  node(int _x) : x(_x) {}

  void combine(node &other) { x = max(x, other.x); }

  void combine(node &a, node &b) {
    *this = a;
    combine(b);
  }
};

int right_half[32];
struct segtree {
  int tree_n = 0;
  vector<node> t;

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

  void init(int n) {
    tree_n = n;
    t.assign(2 * tree_n, node());
  }

  void build(vector<node> v) {
    int n = (int)v.size();
    init(n);
    assert(n <= tree_n);

    for (int i = 0; i < n; i++)
      t[tree_n + i] = v[i];

    for (int i = n - 1; i > 0; i--)
      t[i].combine(t[2 * i], t[2 * i + 1]);
  }

  node query(int a, int b) {
    assert(0 <= a && a <= b && b <= tree_n);
    node ans;

    int r_size = 0;
    for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2) {
      if (a & 1)
        ans.combine(t[a++]);
      if (b & 1)
        right_half[r_size++] = --b;
    }
    for (int i = r_size - 1; i >= 0; i--)
      ans.combine(t[right_half[i]]);

    return ans;
  }

  node query_single(int a) {
    assert(a >= 0 && a < tree_n);
    return t[tree_n + a];
  }

  void join_up(int pos) {
    while (pos > 1) {
      pos /= 2;
      t[pos].combine(t[2 * pos], t[2 * pos + 1]);
    }
  }

  void update(int index, node &a) {
    assert(index >= 0 && index < tree_n);
    int pos = tree_n + index;
    t[pos] = a;
    join_up(pos);
  }
};

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  vecin(n, a);
  segtree T(6e5 + 7);

  for (int i = 0; i < n; i++) {
    int x = T.query(max(a[i] - k, 0), a[i] + k + 1).x;
    node t(x + 1);
    T.update(a[i], t);
  }

  cout << T.query(0, 3e5 + 6).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 node {
// int x;

// node()
//{
// x = 0;
//}

// node(int _x):x(_x){}

// void combine(node& other)
//{
// x = min(x,other.x);
//}

// void combine(node& a, node& b)
//{
//*this = a;
// combine(b);
//}
//};

// int right_half[32];
// struct segtree
//{
// int tree_n = 0;
// vector<node> t;

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

// void init(int n)
//{
// tree_n = n;
// t.assign(2*tree_n,node());
//}

// void build(vector<node> v)
//{
// int n = (int)v.size();
// init(n);
// assert(n <= tree_n);

// for (int i = 0; i < n; i++)
// t[tree_n+i] = v[i];

// for (int i = n-1; i > 0; i--)
// t[i].combine(t[2*i],t[2*i+1]);
//}

// node query(int a, int b)
//{
// assert(0 <= a && a <= b && b <= tree_n);
// node ans;

// int r_size = 0;
// for (a += tree_n, b += tree_n; a < b; a /= 2, b /= 2)
//{
// if (a&1)
// ans.combine(t[a++]);
// if (b&1)
// right_half[r_size++] = --b;
//}
// for (int i = r_size-1; i >= 0; i--)
// ans.combine(t[right_half[i]]);

// return ans;
//}

// node query_single(int a)
//{
// assert(a >= 0 && a < tree_n);
// return t[tree_n+a];
//}

// void join_up(int pos)
//{
// while (pos > 1)
//{
// pos /= 2;
// t[pos].combine(t[2*pos],t[2*pos+1]);
//}
//}

// void update(int index, node& a)
//{
// assert(index >= 0 && index < tree_n);
// int pos = tree_n+index;
// t[pos] = a;
// join_up(pos);
//}
//};

const int N = 3e5 + 30; // limit for array size
int n;                  // array size
int t[2 * N];

void build() { // build the tree
  for (int i = n - 1; i > 0; --i)
    t[i] = max(t[i << 1], t[i << 1 | 1]);
}

void modify(int p, int value) { // set value at position p
  for (t[p + n] = max(value, t[p + n]), p += n; p > 1; p >>= 1)
    t[p >> 1] = max(t[p], t[p ^ 1]);
}

int query(int l, int r) { // sum on interval [l, r)
  int res = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, t[l++]);
    if (r & 1)
      res = max(t[--r], res);
  }
  return res;
}

void solve() {
  int k;
  cin >> n >> k;
  vi a(n);
  vecin(n, a);

  for (int i = 0; i < n; i++) {
    int x = query(max(a[i] - k, 0), a[i] + k + 1);
    // cout << x << " " << a[i] << endl;;
    modify(a[i], x + 1);
  }

  cout << query(0, 3e5 + 7) << endl;
}

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

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