alexjercan/bugnet huggingface dataset:
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, k;

struct ST {
  int st[4 * N];

  ST() { memset(st, 0, sizeof st); }

  void update(int v, int tl, int tr, int idx, int val) {
    if (tl == idx && tl == tr) {
      st[v] = max(st[v], val);
      return;
    }
    int tm = (tl + tr) / 2;
    if (idx <= tm) {
      update(v * 2, tl, tm, idx, val);
    } else {
      update(v * 2 + 1, tm + 1, tr, idx, val);
    }
    st[v] = max(st[v * 2], st[v * 2 + 1]);
  }

  int query(int v, int tl, int tr, int l, int r) {
    if (l > r) {
      return 0;
    }
    if (tl == l && tr == r) {
      return st[v];
    }
    int tm = (tl + tr) / 2;
    int ql = query(v * 2, tl, tm, l, min(tm, r));
    int qr = query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
    return max(ql, qr);
  }

  int mx() { return st[1]; }
};

ST st;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  int a;
  for (int i = 1; i <= n; i++) {
    cin >> a;

    int li = max(a - k, 0);
    int ri = min(a + k, N - 1);
    int best = st.query(1, 0, N - 1, li, ri) + 1;
    st.update(1, 0, N - 1, a, best);
  }

  cout << st.mx() << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, k;

struct ST {
  int st[4 * N];

  void update(int v, int tl, int tr, int idx, int val) {
    if (tl == idx && tl == tr) {
      st[v] = max(st[v], val);
      return;
    }
    int tm = (tl + tr) / 2;
    if (idx <= tm) {
      update(v * 2, tl, tm, idx, val);
    } else {
      update(v * 2 + 1, tm + 1, tr, idx, val);
    }
    st[v] = max(st[v * 2], st[v * 2 + 1]);
  }

  int query(int v, int tl, int tr, int l, int r) {
    if (l > r) {
      return 0;
    }
    if (tl == l && tr == r) {
      return st[v];
    }
    int tm = (tl + tr) / 2;
    int ql = query(v * 2, tl, tm, l, min(tm, r));
    int qr = query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
    return max(ql, qr);
  }

  int mx() { return st[1]; }
};

ST st;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> k;

  int a;
  for (int i = 1; i <= n; i++) {
    cin >> a;

    int li = max(a - k, 1);
    int ri = min(a + k, N - 1);
    int best = st.query(1, 1, N - 1, li, ri) + 1;
    st.update(1, 1, N - 1, a, best);
  }

  cout << st.mx() << '\\n';

  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

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

int n, k, result, a;
int t[1300005];

void update(int idx, int l, int r, int aim, int val) {
  if (l == aim and aim == r) {
    t[idx] = val; // 这里是否可以直接赋值???
    return;
  }
  int mid = (l + r) >> 1;
  if (aim <= mid) {
    update(idx << 1, l, mid, aim, val);
  } else {
    update(idx << 1 | 1, mid + 1, r, aim, val);
  }
  t[idx] = max(t[idx << 1], t[idx << 1 | 1]);
  return;
}

int query(int idx, int l, int r, int aiml, int aimr) {
  if (aiml <= l and r <= aimr) {
    return t[idx];
  }
  int mid = (l + r) >> 1, ansl = 0, ansr = 0;
  if (aiml <= mid) {
    ansl = query(idx << 1, l, mid, aiml, aimr);
  }
  if (mid < aimr) {
    ansr = query(idx << 1 | 1, mid + 1, r, aiml, aimr);
  }
  return max(ansl, ansr);
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    result = query(1, 0, 300000, max(0, a - k), min(300000, a + k)) + 1;
    update(1, 0, 300000, a, result);
  }
  cout << query(1, 0, 300000, 0, 300000) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int n, k, result, a;
int t[1300005];

void update(int idx, int l, int r, int aim, int val) {
  if (l == aim and aim == r) {
    t[idx] = val; // 这里是否可以直接赋值???
    return;
  }
  int mid = (l + r) >> 1;
  if (aim <= mid) {
    update(idx << 1, l, mid, aim, val);
  } else {
    update(idx << 1 | 1, mid + 1, r, aim, val);
  }
  t[idx] = max(t[idx << 1], t[idx << 1 | 1]);
  return;
}

int query(int idx, int l, int r, int aiml, int aimr) {
  if (aiml <= l and r <= aimr) {
    return t[idx];
  }
  int mid = (l + r) >> 1, ansl = 0, ansr = 0;
  if (aiml <= mid) {
    ansl = query(idx << 1, l, mid, aiml, aimr);
  }
  if (mid < aimr) {
    ansr = query(idx << 1 | 1, mid + 1, r, aiml, aimr);
  }
  return max(ansl, ansr);
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    result = query(1, 1, 300000, max(1, a - k), min(300000, a + k)) + 1;
    update(1, 1, 300000, a, result);
  }
  cout << result << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int, int> pp;
bool com(pp x, pp y) { return x.ff < y.ff; }
int N = 3e5 + 9, k;
void input_() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}
std::vector<int> dp(N, -1);
int f(int i, int n, int k, int *a) {
  if (i == n - 1)
    return 1;
  if (dp[i] != -1)
    return dp[i];
  int x = -1, y = -1;
  for (int j = i + 1; j < n; j++) {
    if (a[j] - a[i] <= k && a[j] >= a[i]) {
      x = j;
      // cout<<a[j]-a[i]<<" "<<i<<"\\n";
      break;
    }
  }
  for (int j = i + 1; j < n; j++) {
    if (a[i] - a[j] <= k && a[j] < a[i]) {
      y = j;
      break;
    }
  }
  if (x == -1 && y == -1)
    dp[i] = 1;
  else if (x == -1)
    dp[i] = 1 + f(y, n, k, a);
  else if (y == -1)
    dp[i] = 1 + f(x, n, k, a);
  else
    dp[i] = max(1 + f(x, n, k, a), 1 + f(y, n, k, a));
  // cout<<dp[i]<<" "<<i<<"\\n";
  return dp[i];
}
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (dp[i] == -1)
      ans = max(f(i, n, k, a), ans);
  }
  cout << ans;
}
int main() {
  input_();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int, int> pp;
bool com(pp x, pp y) { return x.ff < y.ff; }
int N = 3e5 + 9, k;
void input_() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
}
std::vector<int> dp(N, -1);
int f(int i, int n, int k, int *a) {
  if (i == n - 1)
    return 1;
  if (dp[i] != -1)
    return dp[i];
  for (int j = i + 1; j < n; j++) {
    if (abs(a[i] - a[j]) <= k) {
      dp[i] = max(dp[i], 1 + f(j, n, k, a));
    }
  }
  return dp[i];
}
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int ans = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (dp[i] == -1)
      ans = max(f(i, n, k, a), ans);
  }
  cout << ans;
}
int main() {
  input_();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
int t[maxn << 2];
void update(int x, int nv, int l, int r, int num) {
  if (l == r) {
    t[num] = nv;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(x, nv, l, mid, num << 1);
  else
    update(x, nv, mid + 1, r, num << 1 | 1);
  t[num] = max(t[num << 1], t[num << 1 | 1]);
}
int query(int x, int y, int l, int r, int num) {
  if (l > y || r < x)
    return 0;
  if (l >= x && r <= y)
    return t[num];
  int mid = (l + r) >> 1;
  return max(query(x, y, l, mid, num << 1),
             query(x, y, mid + 1, r, num << 1 | 1));
}
int n, k, ans;
int main() {
  cin >> n >> k;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    int p = query(max(0, x - k), x + k, 0, maxn - 1, 1);
    ans = max(ans, 1 + p);
    update(x, 1 + p, 0, maxn - 1, 1);
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
const int maxn = 1e7;
struct point {
  int l, r, sum;
} t[maxn];
int n, k, sum, ans;
void make(int left, int right, int num) {
  t[num].l = left;
  t[num].r = right;
  if (left == right)
    return;
  else {
    make(left, (left + right) / 2, num * 2);
    make((right + left) / 2 + 1, right, num * 2 + 1);
    t[num].sum = t[num * 2].sum + t[num * 2 + 1].sum;
  }
}
void find(int left, int right, int num) {
  if (t[num].l == t[num].r)
    return;
  if (!(left <= t[num].l && right >= t[num].r)) {
    if (right <= (t[num].l + t[num].r) / 2)
      find(left, right, num * 2);
    else if (left >= (t[num].l + t[num].r) / 2 + 1)
      find(left, right, num * 2 + 1);
    else {
      find(left, right, num * 2);
      find(left, right, num * 2 + 1);
    }
  } else {
    sum = max(sum, t[num].sum);
  }
}
void add(int x, int y, int num) {
  if (t[num].l == t[num].r) {
    t[num].sum++;
    return;
  }
  if (y <= (t[num].l + t[num].r) / 2)
    add(x, y, num * 2);
  else if (x >= (t[num].l + t[num].r) / 2)
    add(x, y, num * 2 + 1);
  else {
    add(x, y, num * 2);
    add(x, y, num * 2 + 1);
  }
  t[num].sum = max(t[num * 2].sum, t[num * 2 + 1].sum);
}
int main() {
  cin >> n >> k;
  make(0, 3 * n, 1);
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    add(x, x + 2 * k, 1);
    sum = 0;
    find(x, x + 2 * k, 1);
    ans = max(sum, ans);
  }
  cout << ans;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

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

struct RMst { // Range max segtree
  vector<int> d;
  int zero;
  RMst(int sz, int num) {
    int x = 1;
    while (x < sz)
      x *= 2;
    zero = x;
    d.resize(x * 2, num);
  }
  void setnum(int di, int num) {
    di += zero;
    d[di] = num;
    di /= 2;
    while (di >= 1 && d[di] < num) {
      d[di] = num;
      di /= 2;
    }
    return;
  }
  int ranmax(int l, int r) { // 0-index,[l,r)
    if (l >= r)
      return -1;
    int x = -1001001001;
    l += zero;
    r += zero;
    while (1) {
      if (l % 2 == 1) {
        x = max(x, d[l]);
        ++l;
      }
      if (r % 2 == 1) {
        x = max(x, d[r - 1]);
        --r;
      }
      if (l == r)
        break;
      l /= 2;
      r /= 2;
    }
    return x;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  RMst dp(300005, 0);
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    int l = max(0, a - k);
    int r = min(300003, a + k + 1);
    dp.setnum(a, dp.ranmax(l, r) + 1);
  }
  cout << dp.ranmax(0, 300005) << endl;
}

Error: WA

Fixed submission:

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

struct RMst { // Range max segtree
  vector<int> d;
  int zero;
  RMst(int sz, int num) {
    int x = 1;
    while (x < sz)
      x *= 2;
    zero = x;
    d.resize(sz * 2, num);
  }
  void setnum(int di, int num) {
    di += zero;
    d[di] = num;
    di /= 2;
    while (di >= 1 && d[di] < num) {
      d[di] = max(d[di * 2], d[di * 2 + 1]);
      di /= 2;
    }
    return;
  }
  int ranmax(int l, int r) { // 0-index,[l,r)
    if (l >= r)
      return -1;
    int x = -1001001001;
    l += zero;
    r += zero;
    while (1) {
      if (l % 2 == 1) {
        x = max(x, d[l]);
        ++l;
      }
      if (r % 2 == 1) {
        x = max(x, d[r - 1]);
        --r;
      }
      if (l == r)
        break;
      l /= 2;
      r /= 2;
    }
    return x;
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  RMst dp(300005, 0);
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    int l = max(0, a - k);
    int r = min(300003, a + k + 1);
    dp.setnum(a, dp.ranmax(l, r) + 1);
  }
  cout << dp.ranmax(0, 300005) << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const ll MOD = 1e9 + 7;
const int MAX = 1e5 + 10;
const ll INF = (1LL << 31) - 1;
const double PI = acos(-1);

const int SEGLEN = 1 << 19;
vector<int> segtree(2 * SEGLEN);

void update(int ind, int x) {
  ind += SEGLEN;
  segtree[ind] = x;
  while (1) {
    ind /= 2;
    if (ind == 0)
      break;
    segtree[ind] = max(segtree[2 * ind], segtree[2 * ind + 1]);
  }
}

int get_max(int l, int r) {
  l += SEGLEN;
  r += SEGLEN;
  int ans = 0;
  while (l < r) {
    if (l % 2 == 1) {
      ans = max(ans, segtree[l]);
      ++l;
    }
    l /= 2;
    if (r % 2 == 1) {
      ans = max(ans, segtree[r - 1]);
      --r;
    }
    r /= 2;
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    int s = max(0, a - k), t = min(SEGLEN, a + k + 1);
    update(a, get_max(s, t) + 1);
  }
  int ans = 1;
  rep(i, SEGLEN) ans = max(ans, segtree[i + SEGLEN]);
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const ll MOD = 1e9 + 7;
const int MAX = 1e5 + 10;
const ll INF = (1LL << 31) - 1;
const double PI = acos(-1);

const int SEGLEN = 1 << 18;
vector<int> segtree(2 * SEGLEN);

void update(int ind, int x) {
  ind += SEGLEN;
  segtree[ind] = x;
  while (1) {
    ind /= 2;
    if (ind == 0)
      break;
    segtree[ind] = max(segtree[2 * ind], segtree[2 * ind + 1]);
  }
}

int get_max(int l, int r) {
  l += SEGLEN;
  r += SEGLEN;
  int ans = 0;
  while (l < r) {
    if (l % 2 == 1) {
      ans = max(ans, segtree[l]);
      ++l;
    }
    l /= 2;
    if (r % 2 == 1) {
      ans = max(ans, segtree[r - 1]);
      --r;
    }
    r /= 2;
  }

  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    int s = max(0, a - k), t = min(SEGLEN, a + k + 1);
    update(a, get_max(s, t) + 1);
  }
  int ans = 1;
  rep(i, SEGLEN) ans = max(ans, segtree[i + SEGLEN]);
  cout << ans << endl;

  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const int maxn = 3e6 + 7;
struct node {
  int l, r, s;
} pre[maxn];
int n, k, mx, a[maxn];
void build(int k, int l, int r);
void update(int v, int k, int l, int r);
int query(int k, int l, int r);
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    mx = max(mx, a[i]);
  }
  build(1, 0, mx);
  int now = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    now = query(1, max(0, a[i] - k), min(mx, a[i] + k)) + 1;
    ans = max(ans, now);
    update(now, 1, a[i], a[i]);
  }
  printf("%d\\n", ans);
}

void build(int k, int l, int r) {
  pre[k].l = l, pre[k].r = r, pre[k].s = 0;
  if (l >= r)
    return;
  build(k * 2, l, (l + r) / 2);
  build(k * 2 + 1, (l + r) / 2 + 1, r);
}

void update(int v, int k, int l, int r) {
  if (l <= pre[k].l && pre[k].r <= r) {
    pre[k].s = v;
    return;
  }
  int mid = (pre[k].l + pre[k].r) / 2;
  if (l <= mid)
    update(v, k * 2, l, r);
  else
    update(v, k * 2 + 1, l, r);
  pre[k].s = max(pre[k * 2].s, pre[k * 2 + 1].s);
}

int query(int k, int l, int r) {
  if (l <= pre[k].l && pre[k].r <= r) {
    return pre[k].s;
  }
  int mid = (pre[k].l + pre[k].r) / 2;
  int ans1 = 0, ans2 = 0;
  if (l <= mid)
    ans1 = query(k * 2, l, r);
  if (mid + 1 <= r)
    ans2 = query(k * 2 + 1, l, r);
  return max(ans1, ans2);
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int maxn = 3e6 + 7;
struct node {
  int l, r, s;
} pre[maxn];
int n, k, mx, a[maxn];
void build(int k, int l, int r);
void update(int k, int l, int r);
int query(int k, int l, int r);
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    mx = max(mx, a[i]);
  }
  build(1, 1, mx);
  int now = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    now = query(1, max(0, a[i] - k), min(mx, a[i] + k)) + 1;
    ans = max(ans, now);
    update(1, now, now);
  }
  printf("%d\\n", ans);
}

void build(int k, int l, int r) {
  pre[k].l = l, pre[k].r = r, pre[k].s = 0;
  if (l >= r)
    return;
  build(k * 2, l, (l + r) / 2);
  build(k * 2 + 1, (l + r) / 2 + 1, r);
}

void update(int k, int l, int r) {
  if (l <= pre[k].l && pre[k].r <= r) {
    pre[k].s = l;
    return;
  }
  int mid = (pre[k].l + pre[k].r) / 2;
  if (l <= mid)
    update(k * 2, l, r);
  if (mid + 1 <= r)
    update(k * 2 + 1, l, r);
  pre[k].s = max(pre[k * 2].s, pre[k * 2 + 1].s);
}

int query(int k, int l, int r) {
  if (pre[k].l <= l && pre[k].r <= r) {
    return pre[k].s;
  }
  int mid = (pre[k].l + pre[k].r) / 2;
  int ans1 = 0, ans2 = 0;
  if (l <= mid)
    ans1 = query(k * 2, l, r);
  if (mid + 1 <= r)
    ans2 = query(k * 2 + 1, l, r);
  return max(ans1, ans2);
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

vector<int> arr, st;
int query(int node, int l, int r, int ll, int rl) {

  if (l <= ll && r >= rl)
    return st[node];
  if (r < ll || l > rl)
    return 0;
  int mid = ll + (rl - ll) / 2;
  int left = query(2 * node + 1, l, r, ll, mid);
  int right = query(2 * node + 2, l, r, mid + 1, rl);
  return max(left, right);
}
int update(int node, int i, int v, int ll, int rl) {
  if (i < ll || i > rl)
    return st[node];
  if (ll == rl) {
    st[node] = v;
    return st[node];
  }
  int mid = ll + (rl - ll) / 2;
  if (ll <= i && i <= mid)
    int left = update(2 * node + 1, i, v, ll, mid);
  else
    int right = update(2 * node + 2, i, v, mid + 1, rl);
  st[node] = max(st[2 * node + 1], st[2 * node + 2]);
  return st[node];
}

int main() {
  int n, k;
  cin >> n >> k;
  arr.resize(n);
  int size = 3e5;
  st.assign(1e8, 0);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    int l = arr[i] - k;
    int r = arr[i] + k;
    int v = query(0, l, r, 0, size);
    update(0, arr[i], v + 1, 0, size);
  }
  // for(auto x:st)cout<<x;
  cout << query(0, 0, size, 0, size) << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> arr, st;
int query(int node, int l, int r, int ll, int rl) {

  if (l <= ll && r >= rl)
    return st[node];
  if (r < ll || l > rl)
    return 0;
  int mid = ll + (rl - ll) / 2;
  int left = query(2 * node + 1, l, r, ll, mid);
  int right = query(2 * node + 2, l, r, mid + 1, rl);
  return max(left, right);
}
int update(int node, int i, int v, int ll, int rl) {
  if (i < ll || i > rl)
    return st[node];
  if (ll == rl) {
    st[node] = v;
    return st[node];
  }
  int mid = ll + (rl - ll) / 2;
  if (ll <= i && i <= mid)
    int left = update(2 * node + 1, i, v, ll, mid);
  else
    int right = update(2 * node + 2, i, v, mid + 1, rl);
  st[node] = max(st[2 * node + 1], st[2 * node + 2]);
  return st[node];
}

int main() {
  int n, k;
  cin >> n >> k;
  arr.resize(n);
  int size = 1;
  while (size < 3e5)
    size *= 2;
  st.assign(2 * size, 0);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    int l = arr[i] - k;
    int r = arr[i] + k;
    int v = query(0, l, r, 0, 2 * size - 1);
    update(0, arr[i], v + 1, 0, 2 * size - 1);
  }
  // for(auto x:st)cout<<x;
  cout << query(0, 0, 2 * size - 1, 0, 2 * size - 1) << '\\n';
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 600005;
const int mod = 1000000007;

int N, K;

struct SegmentTree {
private:
  int n;
  vector<int> node;

public:
  SegmentTree(vector<int> v) {
    int sz = v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1, 0);
    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
  }

  void update(int x, int val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = max(node[2 * x + 1], node[2 * x + 2]);
    }
  }

  int getmax(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return node[k];

    int vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
int main() {
  int N, K;
  cin >> N >> K;
  SegmentTree seg(vector<int>(INF, 0));
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    int l = max(0, a - K);
    int r = min(INF, a + K + 1);
    int v = seg.getmax(l, r); // cout<<l<<" "<<r<<endl;
    seg.update(a, v + 1);     // cout<<v<<endl;
  }
  cout << seg.getmax(0, INF) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 300005;
const int mod = 1000000007;

int N, K;

struct SegmentTree {
private:
  int n;
  vector<int> node;

public:
  SegmentTree(vector<int> v) {
    int sz = v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
  }

  void update(int x, int val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = max(node[2 * x + 1], node[2 * x + 2]);
    }
  }

  int getmax(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return node[k];

    int vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
int main() {
  int N, K;
  cin >> N >> K;
  SegmentTree seg(vector<int>(N, 0));
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    int l = max(0, a - K);
    int r = min(INF, a + K + 1);
    int v = seg.getmax(l, r);
    seg.update(a, v + 1);
  }
  cout << seg.getmax(0, INF) << endl;
  return 0;
}