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

#include <bits/stdc++.h>
using namespace std;
int Read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, k, f[600055], maxn[2400055], a[600055];
void modify(int o, int l, int r, int pos, int x) {
  if (l == r) {
    maxn[o] = max(maxn[o], x);
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    modify(o << 1, l, mid, pos, x);
  else
    modify(o << 1 | 1, mid + 1, r, pos, x);
  maxn[o] = max(maxn[o << 1], maxn[o << 1 | 1]);
}
int query(int o, int l, int r, int nl, int nr) {
  if (nl <= l && r <= nr)
    return maxn[o];
  int mid = (l + r) >> 1, ans = 0;
  if (nl <= mid)
    ans = max(ans, query(o << 1, l, mid, nl, nr));
  if (mid < nr)
    ans = max(ans, query(o << 1 | 1, mid + 1, r, nl, nr));
  return ans;
}
signed main() {
  n = Read(), k = Read();
  for (int i = 1; i <= n; i++)
    a[i] = Read() + 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    f[i] = query(1, 1, 300001, max(a[i] - k, 1), min(a[i] + k, 300001)) + 1;
    modify(1, 1, 300001, a[i], f[i]);
    ans = max(ans, f[i]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int Read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, k, f[300055], maxn[1200055], a[300055];
void modify(int o, int l, int r, int pos, int x) {
  if (l == r) {
    maxn[o] = max(maxn[o], x);
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    modify(o << 1, l, mid, pos, x);
  else
    modify(o << 1 | 1, mid + 1, r, pos, x);
  maxn[o] = max(maxn[o << 1], maxn[o << 1 | 1]);
}
int query(int o, int l, int r, int nl, int nr) {
  if (nl <= l && r <= nr)
    return maxn[o];
  int mid = (l + r) >> 1, ans = 0;
  if (nl <= mid)
    ans = max(ans, query(o << 1, l, mid, nl, nr));
  if (mid < nr)
    ans = max(ans, query(o << 1 | 1, mid + 1, r, nl, nr));
  return ans;
}
signed main() {
  n = Read(), k = Read();
  for (int i = 1; i <= n; i++)
    a[i] = Read() + 1;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    f[i] = query(1, 1, n, max(a[i] - k, 1), min(a[i] + k, 300001)) + 1;
    modify(1, 1, n, a[i], f[i]);
    ans = max(ans, f[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int dp[4 * 300000 + 5];
int n, x, k;
int qry(int ni, int ns, int ne, int nl, int nr) {
  if (nl <= ns && ne <= nr)
    return dp[ni];
  if (nr < ns || ne < nl)
    return 0;
  int mid = ns + (ne - ns) / 2;
  int l = ni * 2 + 1, r = ni * 2 + 2;
  int x = qry(l, ns, mid, nl, nr);
  int y = qry(r, mid + 1, ne, nl, nr);
  return max(x, y);
}
int upd(int ni, int ns, int ne, int idx, int val) {
  if (ne < idx || idx < ns)
    return dp[ni];
  if (ns == ne)
    return dp[ni] = val;
  int mid = ns + (ne - ns) / 2;
  int l = ni * 2 + 1, r = ni * 2 + 2;
  int x = upd(l, ns, mid, idx, val);
  int y = upd(r, mid + 1, ne, idx, val);
  return dp[ni] = max(x, y);
}
signed main() {
  cin >> n >> k;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    int q = 1 + qry(0, 0, (int)3e5, max(0, x - k), min((int)3e5, x + k));
    ans = max(ans, q);
    upd(0, 0, (int)3e5, x, q);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int dp[4 * 30000 + 5];
int n, x, k;
int qry(int ni, int ns, int ne, int nl, int nr) {
  if (nl <= ns && ne <= nr)
    return dp[ni];
  if (nr < ns || ne < nl)
    return 0;
  int mid = ns + (ne - ns) / 2;
  int l = ni * 2 + 1, r = ni * 2 + 2;
  int x = qry(l, ns, mid, nl, nr);
  int y = qry(r, mid + 1, ne, nl, nr);
  return max(x, y);
}
int upd(int ni, int ns, int ne, int idx, int val) {
  if (ne < idx || idx < ns)
    return dp[ni];
  if (ns == ne)
    return dp[ni] = val;
  int mid = ns + (ne - ns) / 2;
  int l = ni * 2 + 1, r = ni * 2 + 2;
  int x = upd(l, ns, mid, idx, val);
  int y = upd(r, mid + 1, ne, idx, val);
  return dp[ni] = max(x, y);
}
signed main() {
  cin >> n >> k;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    int q = 1 + qry(0, 1, (int)3e5, max(1, x - k), min((int)3e5, x + k));
    cout << 11 << endl;
    ans = max(ans, q);
    upd(0, 1, (int)3e5, x, q);
  }
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
ll c[600005] = {0}, tree[1200005] = {0};
ll query(ll ql, ll qr, ll s, ll e, ll in) {
  if (ql > e || qr < s || s > e)
    return -1;
  if (ql <= s && qr >= e) {
    return tree[in];
  }

  int mid = (s + e) / 2;
  // cout<<"Okay";
  return max(query(ql, qr, s, mid, 2 * in + 1),
             query(ql, qr, mid + 1, e, 2 * in + 2));
}
void update(ll s, ll e, ll in, ll key, ll up) {
  if (s > e)
    return;
  if (s == e && s == key) {
    tree[in] = up;
    return;
  } else if (s == e)
    return;
  ll mid = (s + e) / 2;

  if (mid >= key)
    update(s, mid, 2 * in + 1, key, up);
  else
    update(mid + 1, e, 2 * in + 2, key, up);
  tree[in] = max(tree[2 * in + 1], tree[2 * in + 2]);
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll curr = query(max(0ll, a[i] - k), min(300000ll, a[i] + k), 0, 300000, 0);
    // cout<<curr<<endl;
    ans = max(ans, curr + 1);
    update(0, 300000, 0, a[i], curr + 1);
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
ll c[600005] = {0}, tree[1200005] = {0};
ll query(ll ql, ll qr, ll s, ll e, ll in) {
  if (ql > e || qr < s || s > e)
    return -1;
  if (ql <= s && qr >= e) {
    return tree[in];
  }

  int mid = (s + e) / 2;
  // cout<<"Okay";
  return max(query(ql, qr, s, mid, 2 * in + 1),
             query(ql, qr, mid + 1, e, 2 * in + 2));
}
void update(ll s, ll e, ll in, ll key, ll up) {
  if (s > e)
    return;
  if (s == e && s == key) {
    tree[in] = up;
    return;
  } else if (s == e)
    return;
  ll mid = (s + e) / 2;

  update(s, mid, 2 * in + 1, key, up);

  update(mid + 1, e, 2 * in + 2, key, up);
  tree[in] = max(tree[2 * in + 1], tree[2 * in + 2]);
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll curr = query(max(0ll, a[i] - k), min(300000ll, a[i] + k), 0, 300000, 0);
    // cout<<curr<<endl;
    ans = max(ans, curr + 1);
    update(0, 300000, 0, a[i], curr + 1);
  }
  cout << ans;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define NMAX 300005
using namespace std;
typedef long long ll;
ll segmTree[NMAX * 4], l[NMAX * 4], r[NMAX * 4], leaves[NMAX];
void init(size_t nd) {
  if (nd == 0)
    l[0] = 0, r[0] = NMAX - 1;
  else {
    if (nd & 1)
      l[nd] = l[nd / 2], r[nd] = (l[nd / 2] + r[nd / 2]) / 2;
    else
      l[nd] = r[nd - 1] + 1, r[nd] = r[nd / 2 - 1];
  }
  if (l[nd] == r[nd])
    leaves[l[nd]] = nd;
  else {
    init(nd * 2 + 1);
    init(nd * 2 + 2);
  }
}
void upd(ll p, ll x) {
  ll nd = leaves[p];
  if (segmTree[nd] >= x)
    return;
  segmTree[nd] = x;
  do {
    nd = (nd - 1) / 2;
    segmTree[nd] = max(segmTree[nd * 2 + 1], segmTree[nd * 2 + 2]);
  } while (nd > 0);
}
ll query(ll L, ll R) {
  ll ans = 0;
  while (L <= R) {
    ll nd = leaves[L];
    while (nd > 0 && l[(nd - 1) / 2] >= L && r[(nd - 1) / 2] <= R)
      nd = (nd - 1) / 2;
    ans = max(ans, segmTree[nd]);
    L = r[nd] + 1;
  }
  return ans;
}
ll dp[NMAX], v[NMAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init(0);
  ll n, k, ans = 0;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  for (ll i = n - 1; i >= 0; i--) {
    dp[i] = query(max(0ll, v[i] - k), min((ll)NMAX - 1, v[i] + k)) + 1;
    upd(v[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define NMAX 300005
using namespace std;
typedef long long ll;
ll segmTree[NMAX * 4], l[NMAX * 4], r[NMAX * 4], leaves[NMAX];
void init(size_t nd) {
  if (nd == 0)
    l[0] = 0, r[0] = NMAX - 1;
  else {
    if (nd & 1)
      l[nd] = l[nd / 2], r[nd] = (l[nd / 2] + r[nd / 2]) / 2;
    else
      l[nd] = r[nd - 1] + 1, r[nd] = r[nd / 2 - 1];
  }
  if (l[nd] == r[nd])
    leaves[l[nd]] = nd;
  else {
    init(nd * 2 + 1);
    init(nd * 2 + 2);
  }
}
void upd(ll p, ll x) {
  ll nd = leaves[p];
  if (segmTree[nd] >= x)
    return;
  segmTree[nd] = x;
  do {
    nd = (nd - 1) / 2;
    segmTree[nd] = max(segmTree[nd * 2 + 1], segmTree[nd * 2 + 2]);
  } while (nd > 0);
}
ll query(ll L, ll R) {
  ll ans = 0;
  while (L <= R) {
    ll nd = leaves[L];
    while (nd > 0 && l[(nd - 1) / 2] >= L && r[(nd - 1) / 2] <= R)
      nd = (nd - 1) / 2;
    ans = max(ans, segmTree[nd]);
    L = r[nd] + 1;
  }
  return ans;
}
ll dp[NMAX], v[NMAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init(0);
  ll n, k, ans = 0;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  for (ll i = n - 1; i >= 0; i--) {
    dp[i] = query(max(0ll, v[i] - k), v[i] + k) + 1;
    upd(v[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ft                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int N = 3e5;
int tr[4 * N];
int Q(int node, int s, int e, int i, int j) {
  if (i > e || j < s) {
    return 0;
  } else if (i <= s && e <= j) {
    return tr[node];
  }
  int l = node * 2;
  int r = l + 1;
  int m = (s + e) / 2;
  int p = Q(l, s, m, i, j);
  int q = Q(r, m + 1, e, i, j);
  return max(p, q);
}
void up(int node, int s, int e, int i, int v) {
  if (i > e || i < s) {
    return;
  } else if (s == e && i == s) {
    tr[node] = v;
    return;
  }
  int l = node * 2;
  int r = l + 1;
  int m = (s + e) / 2;
  up(l, s, m, i, v);
  up(r, m + 1, e, i, v);
  tr[node] = max(tr[l], tr[r]);
}
int main() {
  ft int t, i, j, n, m, l, r, ans = -1;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> l;
    r = Q(1, 0, N, max(0, l - m), min(N, l + m));
    up(1, 0, N, l, r + 1);
    ans = max(ans, r + 1);
  }
  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ft                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int N = 3e5 + 1;
int tr[3 * N];
int Q(int node, int s, int e, int i, int j) {
  if (i > e || j < s) {
    return 0;
  } else if (i <= s && e <= j) {
    return tr[node];
  }
  int l = node * 2;
  int r = l + 1;
  int m = (s + e) / 2;
  int p = Q(l, s, m, i, j);
  int q = Q(r, m + 1, e, i, j);
  return max(p, q);
}
void up(int node, int s, int e, int i, int v) {
  if (i > e || i < s) {
    return;
  } else if (s == e && i == s) {
    tr[node] = v;
    return;
  }
  int l = node * 2;
  int r = l + 1;
  int m = (s + e) / 2;
  up(l, s, m, i, v);
  up(r, m + 1, e, i, v);
  tr[node] = max(tr[l], tr[r]);
}
int main() {
  ft int t, i, j, n, m, l, r, ans = -1;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> l;
    r = Q(1, 1, N - 1, max(0, l - m), min(N - 1, l + m));
    up(1, 1, N - 1, l, r + 1);
    ans = max(ans, r + 1);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i(0); i < N; i++) {
    cin >> A[i];
  }
  vector<int> dp(N, 1);

  for (int i(0); i < N; i++) {
    for (int j(i + 1); j < min(i + 1000, N); j++) {
      if (abs(A[i] - A[j]) <= K) {
        dp[j] = max(dp[j], dp[i] + 1);
      }
    }
  }
  int ans(0);
  for (int i(0); i < N; i++) {
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i(0); i < N; i++) {
    cin >> A[i];
  }
  vector<int> dp(N, 1);

  for (int i(0); i < N; i++) {
    for (int j(i + 1); j < i + 1000; j++) {
      if (abs(A[i] - A[j]) <= K) {
        dp[j] = max(dp[j], dp[i] + 1);
      }
    }
  }
  int ans(0);
  for (int i(0); i < N; i++) {
    ans = max(dp[i], ans);
  }
  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 T[N << 2], a[N], dp[N], n, k;

void change(int rt, int tl, int tr, int pos, int val) {
  if (tl == tr) {
    T[rt] = max(T[rt], val);
    return;
  }
  int mid = tl + tr >> 1;
  if (mid >= pos)
    change(2 * rt, tl, mid, pos, val);
  else
    change(2 * rt + 1, mid + 1, tr, pos, val);
  T[rt] = max(T[2 * rt], T[2 * rt + 1]);
}

int query(int rt, int ql, int qr, int tl, int tr) {
  if (tl >= ql && tr <= qr) {
    return T[rt];
  }
  int mid = tl + tr >> 1;
  int res = 0;
  if (mid >= ql)
    res = max(res, query(2 * rt, ql, qr, tl, mid));
  if (mid + 1 <= qr)
    res = max(res, query(2 * rt + 1, ql, qr, mid + 1, tr));
  return res;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  dp[1] = 1;
  int ans = 0;
  change(1, 0, N, a[1], dp[1]);
  for (int i = 2; i <= n; ++i) {
    dp[i] = query(1, max(0, a[i] - k), min(a[i] + k, N - 5), 0, N) + 1;
    change(1, 0, N, a[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n, k;
int a[300005], dp[300005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
  }
  dp[1] = 1;
  for (int i = 1; i <= n; ++i) {
    int pos = 0;
    for (int j = i - 1; j >= 1; --j) {
      if (abs(a[i] - a[j]) <= k) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
  }
  int mx = 0;
  for (int i = 1; i <= n; ++i) {
    mx = max(mx, dp[i]);
  }
  printf("%d\\n", mx);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

const ll mod = 1e9 + 7;
const int N = 6e5 + 10, M = 1e6 + 10;
const ll INF = 1e18;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

#define pi acos(-1)

int n, k;
int a[N];
struct wc {
  int l, r;
  int dat;
} tr[N << 4];

void pushup(int p) { tr[p].dat = max(tr[p << 1].dat, tr[p << 1 | 1].dat); }

void build(int p, int l, int r) {
  tr[p] = {l, r};
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
}

void modify(int p, int x, int v) {
  if (tr[p].l == x && tr[p].r == x) {
    tr[p].dat = max(tr[p].dat, v);
    return;
  }
  int mid = tr[p].l + tr[p].r >> 1;
  if (x <= mid)
    modify(p << 1, x, v);
  else
    modify(p << 1 | 1, x, v);
  pushup(p);
}

int ask(int p, int l, int r) {
  if (tr[p].l >= l && tr[p].r <= r) {
    return tr[p].dat;
  }
  int maxn = 0;
  int mid = tr[p].l + tr[p].r >> 1;
  if (r <= mid)
    maxn = max(maxn, ask(p << 1, l, r));
  else if (l > mid)
    maxn = max(maxn, ask(p << 1 | 1, l, r));
  else {
    maxn = max({maxn, ask(p << 1, l, mid), ask(p << 1 | 1, mid + 1, r)});
  }
  return maxn;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  build(1, 1, N - 1);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int num = ask(1, max(1, a[i] - k), a[i] + k) + 1;
    ans = max(ans, num);
    modify(1, a[i], num);
    // cout << num << ' ';
  }
  cout << ans << endl;
}
/*

*/

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

const ll mod = 1e9 + 7;
const int N = 3e5 + 10, M = 1e6 + 10;
const ll INF = 1e18;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

#define pi acos(-1)

int n, k;
int a[N];

bool cmp(int a, int b) { return abs(a - b) <= k; }

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, cmp);
  int num = 1;
  for (int i = 2; i <= n; i++) {
    if (abs(a[i] - a[i - 1]) > k)
      break;
    num++;
  }
  cout << num << endl;
  return 0;
}

/*

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

#include <cstdio>
class segment_tree {
private:
  int mx[1200005];
  inline int max(int a, int b) { return a > b ? a : b; }
  inline void update(int k) { mx[k] = max(mx[k << 1], mx[k << 1 | 1]); }

public:
  void change(int k, int l, int r, int pos, int x) {
    if (l == r && r == pos) {
      mx[k] = x;
      return;
    }
    int mid = l + r >> 1;
    if (mid >= pos)
      change(k << 1, l, mid, pos, x);
    else
      change(k << 1 | 1, mid + 1, r, pos, x);
    update(k);
  }
  int query(int k, int l, int r, int x, int y) {
    if (x < 0)
      x = 0;
    if (y > 300000)
      y = 300000;
    if (l >= x && r <= y)
      return mx[k];
    int mid = l + r >> 1, res = 0;
    if (mid >= x)
      res = max(res, query(k << 1, l, mid, x, y));
    if (mid < y)
      res = max(res, query(k << 1 | 1, mid + 1, r, x, y));
    return res;
  }
} t;
int n, k, a[300005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i)
    t.change(1, 0, 300000, a[i], t.query(1, 0, 300000, a[i] - k, a[i] + k) + 1);
  printf("%d", t.query(1, 0, 300000, 0, 300000));
}

Error: WA

Fixed submission:

#include <cstdio>
class segment_tree {
private:
  int mx[12000005];
  inline int max(int a, int b) { return a > b ? a : b; }
  inline void update(int k) { mx[k] = max(mx[k << 1], mx[k << 1 | 1]); }

public:
  void change(int k, int l, int r, int pos, int x) {
    if (l == r && r == pos) {
      mx[k] = x;
      return;
    }
    int mid = l + r >> 1;
    if (mid >= pos)
      change(k << 1, l, mid, pos, x);
    else
      change(k << 1 | 1, mid + 1, r, pos, x);
    update(k);
  }
  int query(int k, int l, int r, int x, int y) {
    if (x <= 0)
      x = 1;
    if (y > 300000)
      y = 300000;
    if (l >= x && r <= y)
      return mx[k];
    int mid = l + r >> 1, res = 0;
    if (mid >= x)
      res = max(res, query(k << 1, l, mid, x, y));
    if (mid < y)
      res = max(res, query(k << 1 | 1, mid + 1, r, x, y));
    return res;
  }
} t;
int n, k, a[300005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i)
    t.change(1, 1, 300000, a[i], t.query(1, 1, 300000, a[i] - k, a[i] + k) + 1);
  printf("%d", t.query(1, 1, 300000, 1, 300000));
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void build(int node, int start, int end, vector<int> &dp, vector<int> &a) {
  if (start == end) {
    dp[node] = 1;
  } else {
    int mid = start + (end - start) / 2;
    build(2 * node + 1, start, mid, dp, a);
    build(2 * node + 2, mid + 1, end, dp, a);
    if (dp[2 * node + 1] > dp[2 * node + 2])
      dp[node] = dp[2 * node + 1];
    else
      dp[node] = dp[2 * node + 2];
  }
}

void update(int node, int in, int val, int start, int end, vector<int> &dp) {
  if (start == end) {
    dp[node] = val;
    return;
  }

  int l = 2 * node + 1;
  int r = 2 * node + 2;
  int mid = start + (end - start) / 2;
  if (start <= in && in <= mid)
    update(l, in, val, start, mid, dp);
  else
    update(r, in, val, mid + 1, end, dp);
  if (dp[l] > dp[r])
    dp[node] = dp[l];
  else
    dp[node] = dp[r];
}

int query(int node, int start, int end, int l, int r, vector<int> &dp) {
  if (l <= start && end <= r)
    return dp[node];
  if (r < start || l > end)
    return 0;
  int mid = start + (end - start) / 2;
  int left = query(2 * node + 1, start, mid, l, r, dp);
  int right = query(2 * node + 2, mid + 1, end, l, r, dp);
  if (left > right)
    return left;
  else
    return right;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> dp(10000000, 0);
  // for(int i=0; i<n; i++)
  // dp[a[i]]++;

  // build(0,0,300000,dp,a);

  for (int i = 0; i < n; i++) {
    int curr = query(0, 0, 300000, a[i] - k, a[i] + k, dp);
    update(0, a[i], curr + 1, 0, 300000, dp);
  }
  cout << query(0, 0, 300000, 0, 300000, dp) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> dp(n, 1);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (abs(a[j] - a[i]) <= k && dp[j] + 1 > dp[i])
        dp[i] = 1 + dp[j];
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *		author:  zvm
 *		created: 28.09.2020 17:53:16
 **/

#include <bits/stdc++.h>

using namespace std;

template <typename T> class Segment_tree {
public:
  Segment_tree(int _n) {
    n = _n;
    tree.resize(2 * n, 0);
  }

  T combine(T lchild, T rchild) { return max(lchild, rchild); }

  void update(int idx, T val) {
    for (tree[idx += n] = val, idx /= 2; idx >= 1; idx /= 2) {
      tree[idx] = combine(tree[idx << 1], tree[idx << 1 | 1]);
    }
  }

  T query(int l, int r) {
    T ans = 0;
    for (l += n, r += n; l <= r; l /= 2, r /= 2) {
      if (l & 1) {
        ans = combine(ans, tree[l++]);
      }
      if (!(r & 1)) {
        ans = combine(ans, tree[r--]);
      }
    }
    return ans;
  }

private:
  int n;
  vector<T> tree;
};

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

  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  int sz = 1 << 19;
  Segment_tree<int> dp(1 << 19);

  // dp[i] stores max length of flat subseq ending with val i
  dp.update(a[0], 1);

  for (int i = 1; i < n; ++i) {
    dp.update(a[i], 1 + dp.query(max(a[i] - k, 0), min(a[i] + k, 300000)));
  }

  cout << dp.query(0, sz - 1) << endl;
}


Error: TLE

Fixed submission:

/**
 *		author:  zvm
 *		created: 28.09.2020 17:53:16
 **/

#include <bits/stdc++.h>

using namespace std;

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

  int n, k;
  cin >> n >> k;

  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  // dp[i] stores max length of flat subseq that ends at pos i
  vector<int> dp(n, 1);

  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      dp[j] = max(dp[j], (abs(a[i] - a[j]) <= k ? dp[i] + 1 : 0));
    }
  }

  cout << dp.back() << endl;
}