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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define req(i, a, b) for (ll i = (a); i >= (b); --i)
#define name "a"
#define pb push_back
#define sz size()
#define ff first
#define ss second
#define xd '\\n'
#define dc " "
typedef pair<ll, ll> ii;
const ll maxn = 3e5 + 7, oo = 1e18, base = 311, mod = 1e9 + 7;
const double PI = 3.141592654;
ll t, n, a[maxn], res, f[maxn], k, st[maxn << 2];
ll get(int i, int l, int r, int x, int y) {
  if (l > y || r < x)
    return 0;
  if (x <= l && r <= y)
    return st[i];
  int m = (l + r) / 2;
  return max(get(2 * i, l, m, x, y), get(2 * i + 1, m + 1, r, x, y));
}
void update(int i, int l, int r, int x, int v) {
  if (l > x || r < x)
    return;
  if (l == r) {
    st[i] = v;
    return;
  }
  int m = (l + r) / 2;
  update(2 * i, l, m, x, v);
  update(2 * i + 1, m + 1, r, x, v);
  st[i] = max(st[2 * i], st[2 * i + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(name".inp","r",stdin);
  // freopen(name".out","w",stdout);
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) {
    f[i] = get(1, 0, 300000, max(0ll, a[i] - k), a[i] + k) + 1;
    update(1, 0, 300000, a[i], f[i]);
  }
  rep(i, 1, n) res = max(res, f[i]);
  cout << res;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define req(i, a, b) for (ll i = (a); i >= (b); --i)
#define name "a"
#define pb push_back
#define sz size()
#define ff first
#define ss second
#define xd '\\n'
#define dc " "
typedef pair<ll, ll> ii;
const ll maxn = 3e5 + 7, oo = 1e18, base = 311, mod = 1e9 + 7;
const double PI = 3.141592654;
ll t, n, a[maxn], res, f[maxn], k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(name".inp","r",stdin);
  // freopen(name".out","w",stdout);
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  f[1] = 1;
  rep(i, 2, n) rep(j, 1, i - 1) if (abs(a[i] - a[j]) <= k) f[i] =
      max(f[i], f[j] + 1);
  rep(i, 1, n) res = max(res, f[i]);
  cout << res;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  auto cmp = [](pair<int, int> a, pair<int, int> b) {
    return a.second < b.second;
  };
  priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(
      cmp); // value, longestLength;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    pair<int, int> n = {a, 1};
    vector<pair<int, int>> others;
    bool inserted = false;
    while (!pq.empty()) {
      if (abs(pq.top().first - a) <= k) {
        n.second = pq.top().second + 1;
        pq.push(n);
        inserted = true;
        break;
      } else {
        others.push_back(pq.top());
        pq.pop();
      }
    }
    if (!inserted) {
      pq.push(n);
    }
    for (auto other : others) {
      pq.push(other);
    }
  }
  cout << pq.top().second << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> values(n);
  vector<int> dp(n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    values[i] = a;
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (abs(values[j] - a) <= k) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

#define N 1000010

struct Data {
  LL val, idx;
};

LL a[N], c[N], q[N];
LL Maxn[N * 4];
Data b[N];
int n;
LL ans, d;

bool cmp(Data x, Data y) { return (x.val < y.val); }

void update(int o, int l, int r, int x, LL v) {
  if (l == x && r == x) {
    Maxn[o] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(o * 2, l, mid, x, v);
  if (x > mid)
    update(o * 2 + 1, mid + 1, r, x, v);
  Maxn[o] = max(Maxn[o * 2], Maxn[o * 2 + 1]);
}

void query(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    ans = max(Maxn[o], ans);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    query(o * 2, l, mid, x, y);
  if (y > mid)
    query(o * 2 + 1, mid + 1, r, x, y);
}

int main() {
  cin >> n >> d;
  memset(Maxn, 0, sizeof(Maxn));
  memset(q, 0, sizeof(q));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i].val = a[i];
    b[i].idx = i;
  }

  sort(b, b + n, cmp);

  for (int i = 0; i < n; i++) {
    c[i] = b[i].val;
    q[b[i].idx] = i;
  }
  LL tot = 1;
  for (int i = 0; i < n; i++) {
    LL l = a[i] - d, r = a[i] + d;
    int ll = lower_bound(c, c + n, l) - c;
    int rr = upper_bound(c, c + n, r) - c - 1;
    ans = 0;
    query(1, 0, n - 1, ll, rr);
    if (ans == 0)
      update(1, 0, n - 1, q[i], 1);
    else {
      update(1, 0, n - 1, q[i], ans + 1);
      if (ans + 1 > tot)
        tot = ans + 1;
    }
  }
  cout << tot << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

#define N 100010

struct Data {
  LL val, idx;
};

LL a[N], c[N], q[N];
LL Maxn[N * 4];
Data b[N];
int n;
LL ans, d;

bool cmp(Data x, Data y) { return (x.val < y.val); }

void update(int o, int l, int r, int x, LL v) {
  if (l == x && r == x) {
    Maxn[o] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(o * 2, l, mid, x, v);
  if (x > mid)
    update(o * 2 + 1, mid + 1, r, x, v);
  Maxn[o] = max(Maxn[o * 2], Maxn[o * 2 + 1]);
}

void query(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    ans = max(Maxn[o], ans);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    query(o * 2, l, mid, x, y);
  if (y > mid)
    query(o * 2 + 1, mid + 1, r, x, y);
}

int main() {
  cin >> n >> d;
  memset(Maxn, 0, sizeof(Maxn));
  memset(q, 0, sizeof(q));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i].val = a[i];
    b[i].idx = i;
  }

  sort(b, b + n, cmp);

  for (int i = 0; i < n; i++) {
    c[i] = b[i].val;
    q[b[i].idx] = i;
  }
  LL tot = 1;
  for (int i = 0; i < n; i++) {
    LL l = a[i] - d, r = a[i] + d;
    int ll = lower_bound(c, c + n, l) - c;
    int rr = upper_bound(c, c + n, r) - c - 1;
    ans = 0;
    query(1, 0, n - 1, ll, rr);
    if (ans == 0)
      update(1, 0, n - 1, q[i], 1);
    else {
      update(1, 0, n - 1, q[i], ans + 1);
      if (ans + 1 > tot)
        tot = ans + 1;
    }
  }
  cout << tot << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
const int NMAX = 3e5 + 10;
const int MOD = 1e9 + 7;

int Max[NMAX << 3];
int maxx[NMAX], a[NMAX];

void push_up(int rt) { Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]); }

void update(int pos, int c, int l, int r, int rt) {
  if (l == r) {
    Max[rt] = c;
    return;
  }

  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(pos, c, l, mid, rt << 1);
  else
    update(pos, c, mid + 1, r, rt << 1 | 1);
  push_up(rt);
}

int queryMax(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R)
    return Max[rt];

  int mid = (l + r) >> 1;
  int ans = 0;
  if (L <= mid)
    ans = max(ans, queryMax(L, R, l, mid, rt << 1));
  if (R > mid)
    ans = max(ans, queryMax(L, R, mid + 1, r, rt << 1 | 1));
  return ans;
}

int main(int argc, char const *argv[]) {
  int n, k, N = 0;
  scanf("%d%d", &n, &k);
  for (register int i = 1; i <= n; i++)
    scanf("%d", a + i), a[i]++, N = max(N, a[i] + k);
  int ans = 0, ant;
  for (register int i = 1; i <= n; i++) {
    ant = 1 + queryMax(max(1, a[i] - k), min(a[i] + k, N), 1, N, 1);
    if (maxx[a[i]] < ant) {
      maxx[a[i]] = ant;
      update(a[i], ant, 1, N, 1);
    }
    ans = max(ans, ant);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
const int NMAX = 3e5 + 10;
const int MOD = 1e9 + 7;

int a[NMAX], Max[NMAX << 3];

void push_up(int rt) { Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]); }

void update(int pos, int c, int l, int r, int rt) {
  if (l == r) {
    Max[rt] = c;
    return;
  }

  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(pos, c, l, mid, rt << 1);
  else
    update(pos, c, mid + 1, r, rt << 1 | 1);
  push_up(rt);
}

int queryMax(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R)
    return Max[rt];

  int mid = (l + r) >> 1;
  int ans = 0;
  if (L <= mid)
    ans = max(ans, queryMax(L, R, l, mid, rt << 1));
  if (R > mid)
    ans = max(ans, queryMax(L, R, mid + 1, r, rt << 1 | 1));
  return ans;
}

int maxx[NMAX * 2];
int main(int argc, char const *argv[]) {
  int n, k;
  scanf("%d%d", &n, &k);
  for (register int i = 1; i <= n; i++)
    scanf("%d", a + i), a[i]++;
  int ans = 0, ant;
  for (register int i = 1; i <= n; i++) {
    ant = 1 + queryMax(max(1, a[i] - k), a[i] + k, 1, n + k + 1, 1);
    if (maxx[a[i]] < ant) {
      maxx[a[i]] = ant;
      update(a[i], ant, 1, n + k + 1, 1);
    }
    ans = max(ans, ant);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int MAX_N = 300001;
int node[4 * MAX_N], segtree_size;
void segtree_init(int n) {
  segtree_size = 1;
  while (segtree_size < n)
    segtree_size *= 2;
  for (int i = 0; i < 2 * segtree_size - 1; ++i)
    node[i] = 0;
}
void segtree_update(int a, int x) {
  a += segtree_size - 1;
  node[a] = max(node[a], x);
  while (a > 0) {
    a = (a - 1) / 2;
    node[a] = max(node[2 * a + 1], node[2 * a + 2]);
  }
}
int segtree_query(int a, int b, int k = 0, int l = 0, int r = segtree_size) {
  if (b <= l || r <= a) {
    return 0;
  }
  if (a <= l && r <= b) {
    return node[k];
  }
  int vl = segtree_query(a, b, 2 * k + 1, l, (l + r) / 2);
  int vr = segtree_query(a, b, 2 * k + 2, (l + r) / 2, r);
  return max(vl, vr);
}

int solve_naive(const vector<int> &va, const int k) {
  vector<int> dp(va.size(), 1);
  for (int i = 0; i < va.size(); ++i) {
    for (int j = 0; j < i; ++j) {
      if (abs(va[i] - va[j]) <= k)
        dp[i] = max(dp[i], dp[j] + 1);
    }
  }
  return *max_element(dp.begin(), dp.end());
}

int main() {
  int n, k;
  cin >> n >> k;
  segtree_init(MAX_N + 1);
  vector<int> va(n);
  for (int i = 0; i < n; ++i)
    cin >> va[i];
  // cout << solve_naive(va, k) << endl;
  for (int i = 0; i < n; ++i) {
    int a = va[i];
    int l = segtree_query(a - k, a + k + 1);
    segtree_update(a, l + 1);
  }
  cout << segtree_query(0, segtree_size);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int MAX_N = 10;
int node[4 * MAX_N], segtree_size;
void segtree_init(int n) {
  segtree_size = 1;
  while (segtree_size < n)
    segtree_size *= 2;
  for (int i = 0; i < 2 * segtree_size - 1; ++i)
    node[i] = 0;
}
void segtree_update(int a, int x) {
  a += segtree_size - 1;
  node[a] = x;
  while (a > 0) {
    a = (a - 1) / 2;
    node[a] = max(node[2 * a + 1], node[2 * a + 2]);
  }
}
int segtree_query(int a, int b, int k = 0, int l = 0, int r = segtree_size) {
  // [a, b] の最大値w
  if (b <= l || r <= a) {

    return 0;
  }
  if (a <= l && r <= b) {
    return node[k];
  }
  int vl = segtree_query(a, b, 2 * k + 1, l, (l + r) / 2);
  int vr = segtree_query(a, b, 2 * k + 2, (l + r) / 2, r);
  return max(vl, vr);
}

// segtree[a] = (最後の値がaになるような、条件を満たす最長の数列の長さ)

int main() {
  int n, k;
  cin >> n >> k;
  segtree_init(MAX_N + 1);
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    int l = segtree_query(a - k, a + k + 1);
    segtree_update(a, l + 1);
  }
  cout << segtree_query(0, n + 1);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 7;

int n, a[N], k, tree[4 * N];

#define m (l + r) / 2
#define lson 2 * node
#define rson 2 * node + 1

void update(int pos, int v, int l, int r, int node) {
  if (l == r) {
    tree[node] = v;
    return;
  }
  if (pos <= m)
    update(pos, v, l, m, lson);
  else
    update(pos, v, m + 1, r, rson);
  tree[node] = max(tree[lson], tree[rson]);
}

int query(int ql, int qr, int l, int r, int node) {
  if (ql <= l && qr >= r) {
    return tree[node];
  }
  int ans = 0;
  if (ql <= m)
    ans = max(ans, query(ql, qr, l, m, lson));
  if (qr > m)
    ans = max(ans, query(ql, qr, m + 1, r, rson));
  return ans;
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int res = query(max(a[i] - k, 0), a[i] + k, 0, N, 1) + 1;
    ans = max(ans, res);
    update(a[i], res, 0, N, 1);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1;
const int N = 6e5 + 7;
inline int ls(int p) { return p << 1; }
inline int rs(int p) { return p << 1 | 1; }
int n, k, a[maxn];
struct node {
  int l, r, pre;
} t[4 * N];

void update(int pos, int v, int l, int r, int p) {
  if (l == r) {
    t[p].pre = v;
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid)
    update(pos, v, l, mid, ls(p));
  else
    update(pos, v, mid + 1, r, rs(p));
  t[p].pre = max(t[ls(p)].pre, t[rs(p)].pre);
}
int query(int x, int y, int l, int r, int p) {
  if (x <= l && y >= r) {
    return t[p].pre;
  }
  int mid = l + r >> 1;
  int ans = 0;
  if (x <= mid)
    ans = max(ans, query(x, y, l, mid, ls(p)));
  if (y > mid)
    ans = max(ans, query(x, y, mid + 1, r, rs(p)));
  return ans;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int res = query(max(a[i] - k, 0), a[i] + k, 0, N, 1) + 1;
    ans = max(ans, res);
    update(a[i], res, 0, N, 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 5;
int n, k, a[N], dp[N][5];

int solve(int i, int pre, int f) {
  if (i == n) {
    return 0;
  }
  int &re = dp[i][f];
  if (re != -1) {
    return re;
  }
  re = 0;
  if (pre == -1) {
    re = max(re, solve(i + 1, pre, 0));
    re = max(re, solve(i + 1, a[i], 1) + 1);
  } else {
    for (int j = i; j < n; j++) {
      if (a[j] - pre <= k && a[j] - pre >= 0) {
        re = max(re, solve(j + 1, a[j], 1) + 1);
        break;
      }
    }
    for (int j = i; j < n; j++) {
      if (pre - a[j] <= k && pre - a[j] >= 0) {
        re = max(re, solve(j + 1, a[j], 2) + 1);
        break;
      }
    }
  }
  return re;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << solve(0, -1, 0) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 5;
int n, k, a[N], dp[N][5];

int solve(int i, int pre, int f) {
  if (i == n) {
    return 0;
  }
  int &re = dp[i][f];
  if (re != -1) {
    return re;
  }
  re = solve(i + 1, pre, 0);
  if (pre == -1) {
    re = max(re, solve(i + 1, a[i], 1) + 1);
  } else {
    for (int j = i; j < n; j++) {
      if (a[j] - pre <= k && a[j] - pre >= 0) {
        re = max(re, solve(j + 1, a[j], 1) + 1);
        break;
      }
    }
    for (int j = i; j < n; j++) {
      if (pre - a[j] <= k && pre - a[i] >= 0) {
        re = max(re, solve(j + 1, a[j], 2) + 1);
        break;
      }
    }
  }
  return re;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << solve(0, -1, 0) << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
using namespace std;
const int N = 3e5 + 10, M = 1e7 + 10;

int a[N], n, m, k, len, dp[N], ans, tr[M];
int query(int x, int y, int l, int r, int rt) {
  int mid = (l + r) / 2;
  int maxn = 0;
  if (x <= l && r <= y)
    return tr[rt];
  if (x <= mid)
    maxn = max(maxn, query(x, y, l, mid, rt << 1));
  if (y > mid)
    maxn = max(maxn, query(x, y, mid + 1, r, rt << 1 | 1));
  return maxn;
}
void pushup(int rt) { tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]); }
void change(int nt, int l, int r, int rt, int k) {
  if (l == r) {
    tr[rt] = max(tr[rt], k);
    return;
  }
  int mid = (l + r) >> 1;
  if (nt <= mid)
    change(nt, l, mid, rt << 1, k);
  else
    change(nt, mid + 1, r, rt << 1 | 1, k);
  pushup(rt);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    len = max(len, a[i] + k);
  }
  for (int i = 1; i <= n; i++)
    a[i]++;
  for (int i = 1; i <= n; i++) {
    int l = max(a[i] - k, 1), r = a[i] + k;
    dp[i] = query(l, r, 1, len, 1) + 1;
    change(a[i], 1, len, 1, dp[i]);
    ans = max(ans, dp[i]);
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
using namespace std;
const int N = 3e5 + 10;

int a[N], n, m, k, len, dp[N], ans, tr[N << 2];
int query(int x, int y, int l, int r, int rt) {
  int mid = (l + r) / 2;
  int maxn = 0;
  if (x <= l && r <= y)
    return tr[rt];
  if (x <= mid)
    maxn = max(maxn, query(x, y, l, mid, rt << 1));
  if (y > mid)
    maxn = max(maxn, query(x, y, mid + 1, r, rt << 1 | 1));
  return maxn;
}
void pushup(int rt) { tr[rt] = max(tr[rt << 1], tr[rt << 1 | 1]); }
void change(int nt, int l, int r, int rt, int k) {
  if (l == r) {
    tr[rt] = max(tr[rt], k);
    return;
  }
  int mid = (l + r) >> 1;
  if (nt <= mid)
    change(nt, l, mid, rt << 1, k);
  else
    change(nt, mid + 1, r, rt << 1 | 1, k);
  pushup(rt);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    len = max(len, a[i] + k);
  }
  for (int i = 1; i <= n; i++)
    a[i]++;
  for (int i = 1; i <= n; i++) {
    int l = max(a[i] - k, 1), r = a[i] + k;
    dp[i] = query(l, r, 1, len, 1) + 1;
    change(a[i], 1, len, 1, dp[i]);
    ans = max(ans, dp[i]);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll tree[2000000];
void updatetree(ll ind, ll l, ll h, ll i, ll val) {
  if (i < l || i > h || l > h)
    return;
  if (l == h) {
    tree[ind] = val;
    return;
  }
  ll md = l + (h - l) / 2;
  updatetree(2 * ind + 1, l, md, i, val);
  updatetree(2 * ind + 2, md + 1, h, i, val);
  tree[ind] = max(tree[2 * ind + 1], tree[2 * ind + 2]);
}
ll query(ll ind, ll l, ll r, ll i, ll j) {
  if (l > r || l > j || r < i)
    return 0;
  if (i <= l && j >= r) {
    return tree[ind];
  }
  ll md = l + (r - l) / 2;
  ll l1 = query(2 * ind + 1, l, md, i, j);
  ll r1 = query(2 * ind + 2, md + 1, r, i, j);
  return max(l1, r1);
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  ll dp[n][2];
  updatetree(0, 0, 1000000, v[0], 1);
  dp[0][0] = 1;
  dp[0][1] = 1;
  ll ans = 1;
  for (ll i = 1; i < n; i++) {
    ll x = query(0, 0, 1000000, v[i] - k, v[i]);
    dp[i][0] = x + 1;
    ll y = query(0, 0, 1000000, v[i], v[i] + k);
    dp[i][1] = y + 1;

    ans = max(ans, max(dp[i][0], dp[i][1]));
    updatetree(0, 0, 1000000, v[i], max(dp[i][0], dp[i][1]));
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll tree[1000000];
void updatetree(ll ind, ll l, ll h, ll i, ll val) {
  if (i < l || i > h || l > h)
    return;
  if (l == h) {
    tree[ind] = val;
    return;
  }
  ll md = l + (h - l) / 2;
  updatetree(2 * ind + 1, l, md, i, val);
  updatetree(2 * ind + 2, md + 1, h, i, val);
  tree[ind] = max(tree[2 * ind + 1], tree[2 * ind + 2]);
}
ll query(ll ind, ll l, ll r, ll i, ll j) {
  if (l > r || l > j || r < i)
    return 0;
  if (i <= l && j >= r) {
    return tree[ind];
  }
  ll md = l + (r - l) / 2;
  ll l1 = query(2 * ind + 1, l, md, i, j);
  ll r1 = query(2 * ind + 2, md + 1, r, i, j);
  return max(l1, r1);
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  ll dp[n][2];
  updatetree(0, 0, 300000, v[0], 1);
  dp[0][0] = 1;
  dp[0][1] = 1;
  ll ans = 1;
  for (ll i = 1; i < n; i++) {
    ll x = query(0, 0, 300000, v[i] - k, v[i]);
    dp[i][0] = x + 1;
    ll y = query(0, 0, 300000, v[i], v[i] + k);
    dp[i][1] = y + 1;
    // cout<<dp[i][0]<<" "<<dp[i][1]<<endl;
    ans = max(ans, max(dp[i][0], dp[i][1]));
    updatetree(0, 0, 300000, v[i], max(tree[v[i]], max(dp[i][0], dp[i][1])));
  }
  cout << ans;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define read(a) scanf("%d", &a)
#define readf(a) scanf("%lf", &a)
#define pb push_back
#define mk make_pair
#define _rand mt19937 RAND(time(0))
#define mem(a) memset(a, 0, sizeof(a))
#define Buff ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int INF = 1e9 + 7;
const int N = 3e5 + 7;
const int M = 1e6 + 7;

#define lson rt << 1
#define rson rt << 1 | 1
template <class T> class SegTree {
public:
  T maxv[N << 2];
  void init() { mem(maxv); }
  inline void pushup(int rt) { maxv[rt] = max(maxv[lson], maxv[rson]); }
  void update(int rt, int l, int r, int pos, T val) {
    if (l == r) {
      maxv[rt] = max(maxv[rt], val);
      return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid)
      update(lson, l, mid, pos, val);
    else
      update(rson, mid + 1, r, pos, val);
    pushup(rt);
  }

  T query(int rt, int l, int r, int L, int R) {
    if (L <= l && r <= R)
      return maxv[rt];
    int mid = (l + r) >> 1;
    T ans = 0;
    if (L <= mid)
      ans = max(ans, query(lson, l, mid, L, R));
    if (R > mid)
      ans = max(ans, query(rson, mid + 1, r, L, R));
    return ans;
  }
};
SegTree<int> tree;

int a[N];
signed main() {
  tree.init();
  int n, k;
  read(n);
  read(k);

  int ans = 0, maxx = 0;
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    maxx = max(maxx, a[i]);
  }
  for (int i = 1; i <= n; i++) {
    int l = max(0, a[i] - k);
    int r = min(maxx, a[i] + k);
    int tmp = tree.query(1, 1, maxx, l, r);
    //        cout << "tmp = " << tmp << "\\n";
    ans = max(ans, tmp + 1);
    tree.update(1, 1, maxx, a[i], tmp + 1);
  }
  printf("%d\\n", ans);
  return 0;
}
/**
 **/


Error: 0

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define read(a) scanf("%d", &a)
#define readf(a) scanf("%lf", &a)
#define pb push_back
#define mk make_pair
#define _rand mt19937 RAND(time(0))
#define mem(a) memset(a, 0, sizeof(a))
#define Buff ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int INF = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 1e6 + 7;
int a[N];
signed main() {
  Buff;
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int ans = 0;
  int g = 0;
  for (int i = 2; i <= n; i++) {
    int f = g + (a[i] - a[i - 1]);
    if (abs(f) <= abs(a[i] - a[i - 1]))
      g += a[i] - a[i - 1];
    else
      g = a[i] - a[i - 1];
    if (abs(g) <= k) {
      ans++;
      g = 0;
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
/**
 **/