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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;

#define debug(i) cout << "<debug> " << #i << " " << i << " <\\\\debug>" << endl
#define mfor(i, a, b) for (int i = (a); i <= (b); i++)
#define mrep(i, a, b) for (int i = (a); i >= (b); i--)
#define lll __int128
#define Re register
#define mem(a, b) memset(a, (b), sizeof(a))
#define Design ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-5;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

int a[300010];
int b[300010];

struct STtree {
  // 默认min add功能
  // query功能修改push_up cmp
  // update功能修改push_down update
  // greater功能修改greater
#define lc(x) x << 1
#define rc(x) x << 1 | 1
  struct tree {
    int a;
    int l, r;
    tree() : l(0), r(0), a(0) {}
  } node[(int)300010 << 2];

  STtree() {}

  STtree(int l, int r, int *a) { build(1, l, r, a); }

  inline void push_up(int x) {
    // query功能
    node[x].a = max(node[lc(x)].a, node[rc(x)].a);
  }

  inline int cmp(int a, int b) {
    // query功能
    return (a > b ? a : b);
  }

  void build(int x, int l, int r, int *a) {
    node[x].l = l;
    node[x].r = r;
    if (l == r) {
      node[x].a = 0;
      return;
    }
    int mid = (l + r) >> 1;
    build(lc(x), l, mid, a);
    build(rc(x), mid + 1, r, a);
    push_up(x);
  }
  void update(int left, int right, int x, int k) {
    if (left <= node[x].l && node[x].r <= right) {
      node[x].a = k;
      return;
    }
    int mid = (node[x].l + node[x].r) >> 1;
    if (left <= mid)
      update(left, right, lc(x), k);
    if (right > mid)
      update(left, right, rc(x), k);
    push_up(x);
  }
  int query(int left, int right, int x) {
    int res = 0;
    if (left <= node[x].l && node[x].r <= right)
      return node[x].a;
    int mid = (node[x].l + node[x].r) >> 1;
    if (left <= mid)
      res = max(res, query(left, right, lc(x)));
    if (right > mid)
      res = max(res, query(left, right, rc(x)));
    return res;
  }
} st;

int main() {
  Design;
  int n, k;
  cin >> n >> k;
  mfor(i, 1, n) {
    cin >> a[i];
    a[i]++;
  }
  st.build(1, 1, 300001, 0);
  int ans = 0;
  mfor(i, 1, n) {
    int t = st.query(max(a[i] - k, 1), min(a[i] + k, 300001), 1);
    ans = max(ans, t + 1);
    st.update(a[i], a[i], 1, t + 1);
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;

#define debug(i) cout << "<debug> " << #i << " " << i << " <\\\\debug>" << endl
#define mfor(i, a, b) for (int i = (a); i <= (b); i++)
#define mrep(i, a, b) for (int i = (a); i >= (b); i--)
#define lll __int128
#define Re register
#define mem(a, b) memset(a, (b), sizeof(a))
#define Design ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-5;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

int a[300010];
int b[300010];

struct STtree {
  // 默认min add功能
  // query功能修改push_up cmp
  // update功能修改push_down update
  // greater功能修改greater
#define lc(x) x << 1
#define rc(x) x << 1 | 1
  struct tree {
    int a;
    int l, r;
    tree() {}
  } node[(int)300010 << 2];

  STtree() {}

  STtree(int l, int r, int *a) { build(1, l, r, a); }

  inline void push_up(int x) {
    // query功能
    node[x].a = max(node[lc(x)].a, node[rc(x)].a);
  }

  inline int cmp(int a, int b) {
    // query功能
    return (a > b ? a : b);
  }

  void build(int x, int l, int r, int *a) {
    node[x].l = l;
    node[x].r = r;
    if (l == r) {
      node[x].a = 0;
      return;
    }
    int mid = (l + r) >> 1;
    build(lc(x), l, mid, a);
    build(rc(x), mid + 1, r, a);
    push_up(x);
  }
  void update(int left, int right, int x, int k) {
    if (left <= node[x].l && node[x].r <= right) {
      node[x].a = k;
      return;
    }
    int mid = (node[x].l + node[x].r) >> 1;
    if (left <= mid)
      update(left, right, lc(x), k);
    if (right > mid)
      update(left, right, rc(x), k);
    push_up(x);
  }
  int query(int left, int right, int x) {
    int res = 0;
    if (left <= node[x].l && node[x].r <= right)
      return node[x].a;
    int mid = (node[x].l + node[x].r) >> 1;
    if (left <= mid)
      res = max(res, query(left, right, lc(x)));
    if (right > mid)
      res = max(res, query(left, right, rc(x)));
    return res;
  }
} st;

int main() {
  Design;
  int n, k;
  cin >> n >> k;
  mfor(i, 1, n) cin >> a[i], b[i] = a[i];
  sort(a + 1, a + 1 + n);
  int m = unique(a + 1, a + 1 + n) - a;
  st.build(1, 1, m, 0);
  int ans = 0;
  mfor(i, 1, n) {
    int l = lower_bound(a + 1, a + 1 + m, b[i] - k) - a;
    int r = upper_bound(a + 1, a + 1 + m, b[i] + k) - a - 1;
    int t = st.query(l, r, 1);
    ans = max(ans, t + 1);
    int x = lower_bound(a + 1, a + 1 + m, b[i]) - a;
    st.update(x, x, 1, t + 1);
  }
  cout << ans;
}