alexjercan/bugnet huggingface dataset:
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#include <atcoder/segtree>
using namespace atcoder;

int op(int a, int b) { return max(a, b); }

int e() { return 0; }

int main() {
  int N, K, i;
  cin >> N >> K;
  vector<int> a(N);
  for (i = 0; i < N; i++)
    cin >> a[i];

  segtree<int, op, e> sg(300001);
  for (i = 0; i < N; i++) {
    int l = max(a[i] - K, 0);
    int r = min(a[i] + K, 300000);
    sg.set(a[i], sg.prod(l, r + 1) + 1);
  }
  cout << sg.all_prod() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, K, i, j, ans = 1;
  cin >> N >> K;
  vector<int> a(N), b(N, 1);
  for (i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (i = 1; i < N; i++) {
    for (j = i - 1; j >= 0; j--) {
      if (abs(a[i] - a[j]) <= K) {
        b[i] = max(b[i], b[j] + 1);
        if (ans < b[i])
          break;
      }
    }
    ans = max(ans, b[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define MAX 2e9
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
const double PI = acos(-1);

using namespace std;
int A[300001];
int tr[1200010];

int mx(int x, int s, int e, int l, int r) {
  if (s > r || e < l)
    return 0;
  if (s >= l && e <= r)
    return tr[x];
  else
    return max(mx(x * 2, s, (s + e) / 2, l, r),
               mx(x * 2 + 1, (s + e) / 2 + 1, e, l, r));
}
void update(int x, int s, int e, int i, int val) {
  if (i < s || i > e)
    return;
  tr[x] = max(val, tr[x]);
  if (s != e) {
    update(x * 2, s, (s + e) / 2, i, val);
    update(x * 2 + 1, (s + e) / 2 + 1, e, i, val);
  }
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  //	freopen("input.txt", "r", stdin);

  int N, K;
  cin >> N >> K;
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    A[i]++;
    int cnt = mx(1, 1, 300001, max(0, A[i] - K), min(300001, A[i] + K)) + 1;
    update(1, 1, 300001, A[i], cnt);
    ans = max(ans, cnt);
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define MAX 2e9
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
const double PI = acos(-1);

using namespace std;
int A[300001];
int tr[1200001];

int mx(int x, int s, int e, int l, int r) {
  if (s > r || e < l)
    return 0;
  if (s >= l && e <= r)
    return tr[x];
  else
    return max(mx(x * 2, s, (s + e) / 2, l, r),
               mx(x * 2 + 1, (s + e) / 2 + 1, e, l, r));
}
void update(int x, int s, int e, int i, int val) {
  if (i < s || i > e)
    return;
  // tr[x] = max(val, tr[x]);
  if (s != e) {
    update(x * 2, s, (s + e) / 2, i, val);
    update(x * 2 + 1, (s + e) / 2 + 1, e, i, val);
  }
  tr[x] = max(tr[x * 2], tr[x * 2 + 1]);
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  //	freopen("input.txt", "r", stdin);

  int N, K;
  cin >> N >> K;
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    int cnt = mx(1, 1, 300000, max(0, A[i] - K), min(300000, A[i] + K)) + 1;
    update(1, 1, 300000, A[i], cnt);
    ans = max(ans, cnt);
  }
  cout << ans;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

using tlll = tuple<ll, ll, ll>;

int main() {

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N), G(N, 1), MV(N, 1e6);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll maxl = 0;
  MV[N - 1] = 1;
  for (ll i = N - 1; i >= 0; i--) {

    for (ll j = i + 1; j < N; j++) {

      if (abs(A[j] - A[i]) <= K) {

        G[i] = max(G[i], G[j] + 1);
        if (maxl < G[j] + 1) {
          maxl = G[j] + 1;
          MV[i] = maxl;
          break;
        }
        if (MV[j] <= G[i] - 1) {
          MV[i] = maxl;
          break;
        }
      }
    }
  }

  sort(G.begin(), G.end());
  cout << G[G.size() - 1] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

using tlll = tuple<ll, ll, ll>;

int main() {

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N), G(N, 1);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll maxl = 0;
  for (ll i = N - 1; i >= 0; i--) {
    for (ll j = i + 1; j < N; j++) {

      if (abs(A[j] - A[i]) <= K) {

        G[i] = max(G[i], G[j] + 1);
        if (maxl < G[j] + 1) {
          maxl = G[j] + 1;
          break;
        }
      }
    }
  }

  sort(G.begin(), G.end());
  cout << G[G.size() - 1] << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

// #pragma GCC optimize ("Ofast")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
ll n, m, a, b, c, k, temp, x, y;
const int MAXN = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
vll read(int n) {
  vll v(n);
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
  return v;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int tree[6 * MAXN];
int qfst(int l, int r) {
  int res = 0;
  for (l += 300001, r += 300001; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(tree[l++], res);
    if (r & 1)
      res = max(tree[--r], res);
  }
  return res;
}
void modify(int p, int value) {
  for (tree[p += 300001] = value; p > 1; p >>= 1)
    tree[p >> 1] = max(tree[p], tree[p ^ 1]);
}
void sol(void) {
  cin >> n >> k;
  vll v = read(n);
  for (int i = 0; i < n; i++) {
    int mx = qfst(v[i], v[i] + 1);
    int ending = qfst(max(0, v[i] - k), min(v[i] + k + 1, 300001)) + 1;
    modify(v[i], max(mx, ending));
  }
  cout << qfst(0, 300001);
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  // cin>>test;
  for (int i = 1; i <= test; i++)
    sol();
}


Error: WA

Fixed submission:

// #pragma GCC optimize ("Ofast")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
ll n, m, a, b, c, k, temp, x, y;
const int MAXN = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
vll read(int n) {
  vll v(n);
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
  return v;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int tree[4 * MAXN];
int qfst(int l, int r) {
  int res = 0;
  for (l += 300001, r += 300001; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(tree[l++], res);
    if (r & 1)
      res = max(tree[--r], res);
  }
  return res;
}
void modify(int p, int value) {
  for (tree[p += 300001] = value; p > 1; p >>= 1)
    tree[p >> 1] = max(tree[p], tree[p ^ 1]);
}
void sol(void) {
  cin >> n >> k;
  vll v = read(n);
  for (int i = 0; i < n; i++) {
    int mx = qfst(v[i], v[i] + 1);
    int ending = qfst(max(0, v[i] - k), min(v[i] + k + 1, 300001)) + 1;
    modify(v[i], max(mx, ending));
  }
  cout << qfst(0, 300001);
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  // cin>>test;
  for (int i = 1; i <= test; i++)
    sol();
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct segt {
  int l, r, maxv;
} tree[2400010];
int a[300010], dp[300010];
void build(int le, int ri, int i) {
  tree[i].l = le;
  tree[i].r = ri;
  tree[i].maxv = 0;
  if (le == ri)
    return;
  int mid = (le + ri) >> 1;
  build(le, mid, i << 1);
  build(mid + 1, ri, i << 1 | 1);
}
void change(int p, int x, int i) {
  if (tree[i].l == tree[i].r) {
    tree[i].maxv = max(tree[i].maxv, x);
    return;
  }
  if (tree[i << 1].r >= p)
    change(p, x, i << 1);
  else
    change(p, x, i << 1 | 1);
  tree[i].maxv = max(tree[i << 1].maxv, tree[i << 1 | 1].maxv);
}
int ask(int le, int ri, int i) {
  if (le <= tree[i].l && tree[i].r <= ri)
    return tree[i].maxv;
  int cnt = 0;
  if (tree[i << 1].r >= le)
    cnt = ask(le, ri, i << 1);
  if (tree[i << 1 | 1].l <= ri)
    cnt = max(cnt, ask(le, ri, i << 1 | 1));
  return cnt;
}
signed main() {
  int n, k, i, ans = 1;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  build(1, 300002, 1);
  // dp[1]=1;
  // change(2,1,1);
  for (i = 1; i <= n; i++) {
    dp[i] = max(
        1ll, ask(max(0ll, a[i] - k) + 1, min(300000ll, a[i] + k) + 1, 1) + 1);
    change(a[i] + 1, dp[i], 1);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct segt {
  int l, r, maxv;
} tree[2400010];
int a[300010], dp[300010];
void build(int le, int ri, int i) {
  tree[i].l = le;
  tree[i].r = ri;
  tree[i].maxv = 0;
  if (le == ri)
    return;
  int mid = (le + ri) >> 1;
  build(le, mid, i << 1);
  build(mid + 1, ri, i << 1 | 1);
}
void change(int p, int x, int i) {
  //	if(tree[i].l==tree[i].r)
  //	{
  //		tree[i].maxv=max(tree[i].maxv,x);
  //		return;
  //	}
  if (tree[i << 1].r >= p)
    change(p, x, i << 1);
  else
    change(p, x, i << 1 | 1);
  tree[i].maxv = max(tree[i << 1].maxv, tree[i << 1 | 1].maxv);
}
int ask(int le, int ri, int i) {
  if (le <= tree[i].l && tree[i].r <= ri)
    return tree[i].maxv;
  int cnt = 0;
  if (tree[i << 1].r >= le)
    cnt = ask(le, ri, i << 1);
  if (tree[i << 1 | 1].l <= ri)
    cnt = max(cnt, ask(le, ri, i << 1 | 1));
  return cnt;
}
signed main() {
  int n, k, i, ans = 1;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  build(1, 300002, 1);
  // dp[1]=1;
  change(2, 1, 1);
  for (i = 2; i <= n; i++) {
    dp[i] = max(
        1ll, ask(max(0ll, a[i] - k) + 1, min(300000ll, a[i] + k) + 1, 1) + 1);
    change(a[i] + 1, dp[i], 1);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

int n, k, a[300010], lst, dis[300010];

template <class I> inline I abs(I x) { return x >= 0 ? x : -x; }
template <class I> inline I max(I a, I b) { return a > b ? a : b; }

inline bool cmp(int a, int b) { return abs(a - b) > k; }

int main(void) {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    dis[i] = 1;
    for (int j = i - 1; i - j <= 100 && j >= 1; --j)
      if (abs(a[i] - a[j]) <= k && dis[j] + 1 > dis[i])
        dis[i] = dis[j] + 1;
  }
  for (int i = 1; i <= n; ++i)
    lst = max(lst, dis[i]);
  printf("%d\\n", lst);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int n, k, a[300010], lst, dis[300010], pre[300010];

template <class I> inline I abs(I x) { return x >= 0 ? x : -x; }
template <class I> inline I max(I a, I b) { return a > b ? a : b; }

int main(void) {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    dis[i] = 1, pre[i] = -1;
    for (int j = i - 1; j >= 1; --j)
      if (abs(a[i] - a[j]) <= k && dis[j] + 1 > dis[i])
        pre[i] = j, dis[i] = dis[j] + 1;
  }
  for (int i = 1; i <= n; ++i)
    lst = max(lst, dis[i]);
  printf("%d\\n", lst);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int val[N << 1];
int a[N];
void pushUp(int u) { val[u] = max(val[u << 1], val[u << 1 | 1]); }
void update(int u, int l, int r, int x, int d) {
  if (l == r) {
    val[u] = max(d, val[u]);
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    update(u << 1, l, mid, x, d);
  else
    update(u << 1 | 1, mid + 1, r, x, d);
  pushUp(u);
}
int query(int u, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return val[u];
  }
  int res = 0;
  int mid = l + r >> 1;
  if (x <= mid)
    res = max(res, query(u << 1, l, mid, x, y));
  if (y > mid)
    res = max(res, query(u << 1 | 1, mid + 1, r, x, y));
  return res;
}
int maxDog;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    maxDog = max(maxDog, a[i]);
  }
  for (int i = 1; i <= n; i++) {
    int t = query(1, 1, maxDog, max(1, a[i] - k), min(maxDog, a[i] + k)) + 1;
    update(1, 1, maxDog, a[i], t);
  }
  printf("%d\\n", query(1, 1, maxDog, 1, maxDog));
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int val[N << 1];
int a[N];
void pushUp(int u) { val[u] = max(val[u << 1], val[u << 1 | 1]); }
void update(int u, int l, int r, int x, int d) {
  if (l == r) {
    val[u] += d;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    update(u << 1, l, mid, x, d);
  else
    update(u << 1 | 1, mid + 1, r, x, d);
  pushUp(u);
}
int query(int u, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return val[u];
  }
  int res = 0;
  int mid = l + r >> 1;
  if (x <= mid)
    res = max(res, query(u << 1, l, mid, x, y));
  if (y > mid)
    res = max(res, query(u << 1 | 1, mid + 1, r, x, y));
  return res;
}
int maxDog;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    maxDog = max(maxDog, a[i]);
  }
  for (int i = 1; i <= n; i++) {
    int t = 0;
    if (a[i] - k >= 0 && a[i] + k <= maxDog)
      t = query(1, 1, n, a[i] - k, a[i] + k) + 1;
    update(1, 1, n, a[i], t);
  }
  printf("%d\\n", query(1, 1, n, 1, n));
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f;
const double pi = 3.1415926535897932384626;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int maxn = 3e5 + 5;
int n, k, tot, a[maxn], sum[maxn << 2];
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
void push_up(int rt) { sum[rt] = max(sum[rt << 1], sum[rt << 1 | 1]); }
void update(int x, int c, int l, int r, int rt) {
  if (l == r) {
    sum[rt] = max(sum[rt], c);
    return;
  }
  int m = (l + r) >> 1;
  if (x <= m)
    update(x, c, l, m, rt << 1);
  else
    update(x, c, m + 1, r, rt << 1 | 1);
  push_up(rt);
}
int query(int a, int b, int l, int r, int rt) {
  if (a <= l && b >= r)
    return sum[rt];
  int m = (l + r) >> 1;
  int ans = 0;
  if (a <= m)
    ans = max(ans, query(a, b, l, m, rt << 1));
  if (b > m)
    ans = max(ans, query(a, b, m + 1, r, rt << 1 | 1));
  return ans;
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read() + 1;
    tot = max(tot, a[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int tmp = query(max(1, a[i] - k), min(tot, a[i] + k), 1, tot, 1) + 1;
    update(a[i], tmp, 1, tot, 1);
    ans = max(ans, tmp);
  }
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f;
const double pi = 3.1415926535897932384626;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int maxn = 3e5 + 5;
int n, k, a[maxn], ans = 1;
void dfs(int x, int val) {
  if (x == n) {
    ans = max(ans, val);
    return;
  }
  int i = n + 1;
  for (i = x + 1; i <= n; i++) {
    if (abs(a[x] - a[i]) <= k)
      break;
  }
  if (i <= n)
    dfs(i, val + 1);
  dfs(x + 1, 1);
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  dfs(1, 1);
  printf("%d\\n", ans);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <regex>
#include <set>
#include <vector>
using namespace std;

int N, K;
int A[303030];
int dp[303030];

const int MAX_N = 1 << 18;
int seg_n, dat[4 * MAX_N - 1];
void init(int n_) {
  seg_n = 1;
  while (seg_n < n_)
    seg_n *= 2;
  for (int i = 0; i < 2 * seg_n - 1; i++)
    dat[i] = INT32_MIN;
}
void update(int k, int a) {
  k += seg_n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return INT32_MIN;
  if (a <= l && r <= b)
    return dat[k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int dp_dfs(int n) {
  if (dp[n] != 0)
    return dp[n];

  int res = query(A[n] - K, A[n] + K + 1, 0, 0, seg_n);
  if (res == INT32_MIN) {
    return dp[n] = 1;
  } else {
    return dp[n] = res + 1;
  }
  // for (int i=n+1; i<N; i++) {
  //     if (A[n] - K <= A[i] && A[i] <= A[n] + K) {
  //         max_len = max(max_len, dp_dfs(i) + 1);
  //     }
  // }
}

int kmax[303030];

int main() {
  cin >> N >> K;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }

  // cout << 111 << endl;
  init(303030);
  // cout << 222 << endl;

  for (int i = N - 1; i >= 0; i--) {
    // cout << 333 << endl;
    int dp_ret = dp_dfs(i);
    // kmax[A[i]] = max(kmax[A[i]], dp_ret);
    update(A[i], dp_ret);
    // cout << 444 << endl;
  }

  cout << query(0, 303030, 0, 0, seg_n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <regex>
#include <set>
#include <vector>
using namespace std;

int N, K;
int A[303030];
int dp[303030];

int dp_dfs(int n) {
  if (dp[n] != 0)
    return dp[n];

  int max_len = 1;
  for (int i = n + 1; i < N; i++) {
    if (A[n] - K <= A[i] && A[i] <= A[n] + K) {
      max_len = max(max_len, dp_dfs(i) + 1);
    }
  }
  return dp[n] = max_len;
}

int main() {
  cin >> N >> K;
  for (size_t i = 0; i < N; i++) {
    cin >> A[i];
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, dp_dfs(i));
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define int long long
#define zmrl(x) (int)((x).size())
#define ahen(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
const int N = 300001;

int tree[300300 << 1];

void update(int idx, int val) {
  tree[idx += N] = val;
  for (idx >>= 1; idx; idx >>= 1) {
    tree[idx] = max(tree[idx << 1], tree[idx << 1 | 1]);
  }
}

int solve(int le, int ri) {
  int ret = 0;
  for (le += N, ri += N; le <= ri; le >>= 1, ri >>= 1) {
    if (le & 1)
      ret = max(ret, tree[le++]);
    if (~ri & 1)
      ret = max(ret, tree[ri--]);
  }
  return ret;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int A[300300];
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  int res = 1;
  update(A[1], 1);
  for (int i = 2; i <= n; i++) {
    int maxlen = solve(max(A[i] - k, 0LL), min(A[i] + k, N - 1));
    res = max(res, maxlen + 1);
    if (tree[A[i] + N] < maxlen + 1) {
      update(A[i], maxlen + 1);
    }
  }
  cout << res << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define zmrl(x) (int)((x).size())
#define ahen(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
const int N = 300001;

int tree[300300 << 1];

void update(int idx, int val) {
  tree[idx += N] = val;
  for (idx >>= 1; idx; idx >>= 1) {
    tree[idx] = max(tree[idx << 1], tree[idx << 1 | 1]);
  }
}

int solve(int le, int ri) {
  int ret = 0;
  for (le += N, ri += N; le <= ri; le >>= 1, ri >>= 1) {
    if (le & 1)
      ret = max(ret, tree[le++]);
    if (~ri & 1)
      ret = max(ret, tree[ri--]);
  }
  return ret;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int A[300300], dp[300300];
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  dp[0] = 0;
  int res = dp[1] = 1;
  update(A[1], 1);
  for (int i = 2; i <= n; i++) {
    int maxidx = solve(A[i] - k, A[i] + k);
    res = max(dp[i] = dp[maxidx] + 1, res);
    dp[i] = max(dp[i], dp[i - 1]);
    update(A[i], i);
  }
  cout << res << '\\n';
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 1500010;
int n, m, a[N];

struct SegTree {
  int l[N], r[N], maxn[N];

  void build(int x, int ql, int qr) {
    l[x] = ql;
    r[x] = qr;
    if (ql == qr)
      return;
    int mid = (ql + qr) >> 1;
    build(x * 2, ql, mid);
    build(x * 2 + 1, mid + 1, qr);
  }

  int query(int x, int ql, int qr) {
    if (l[x] == ql && r[x] == qr)
      return maxn[x];
    int mid = (l[x] + r[x]) >> 1;
    if (qr <= mid)
      return query(x * 2, ql, qr);
    if (ql > mid)
      return query(x * 2 + 1, ql, qr);
    return max(query(x * 2, ql, mid), query(x * 2 + 1, mid + 1, qr));
  }

  void update(int x, int p, int v) {
    if (l[x] == p && r[x] == p) {
      maxn[x] = max(maxn[x], v);
      return;
    }
    int mid = (l[x] + r[x]) >> 1;
    if (p <= mid)
      update(x * 2, p, v);
    else
      update(x * 2 + 1, p, v);
    maxn[x] = max(maxn[x * 2], maxn[x * 2 + 1]);
  }
} seg;

int main() {
  scanf("%d%d", &n, &m);
  seg.build(1, 1, 300010);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i]++;
    seg.update(1, a[i],
               seg.query(1, max(1, a[i] - m), min(300010, a[i] + m)) + 1);
  }
  printf("%d\\n", seg.maxn[1]);
  return 0;
}

Error: 0

Fixed submission:

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

const int N = 1200010;
int n, m, a[N];

struct SegTree {
  int l[N], r[N], maxn[N];

  void build(int x, int ql, int qr) {
    l[x] = ql;
    r[x] = qr;
    if (ql == qr)
      return;
    int mid = (ql + qr) >> 1;
    build(x * 2, ql, mid);
    build(x * 2 + 1, mid + 1, qr);
  }

  int query(int x, int ql, int qr) {
    if (l[x] == ql && r[x] == qr)
      return maxn[x];
    int mid = (l[x] + r[x]) >> 1;
    if (qr <= mid)
      return query(x * 2, ql, qr);
    if (ql > mid)
      return query(x * 2 + 1, ql, qr);
    return max(query(x * 2, ql, mid), query(x * 2 + 1, mid + 1, qr));
  }

  void update(int x, int p, int v) {
    if (l[x] == p && r[x] == p) {
      maxn[x] = max(maxn[x], v);
      return;
    }
    int mid = (l[x] + r[x]) >> 1;
    if (p <= mid)
      update(x * 2, p, v);
    else
      update(x * 2 + 1, p, v);
    maxn[x] = max(maxn[x * 2], maxn[x * 2 + 1]);
  }
} seg;

int main() {
  scanf("%d%d", &n, &m);
  seg.build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    seg.update(1, a[i], seg.query(1, max(1, a[i] - m), min(n, a[i] + m)) + 1);
  }
  printf("%d\\n", seg.maxn[1]);
  return 0;
}