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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> T[20];

int query(int h, int k, int i, int j) {
  int m = (k << h) + (1 << h - 1);
  if (i == (k << h) && j == (k + 1 << h) - 1)
    return T[h][k];
  if (j < m)
    return query(h - 1, k << 1, i, j);
  if (i >= m)
    return query(h - 1, k << 1 | 1, i, j);
  return max(query(h - 1, k << 1, i, m - 1), query(h - 1, k << 1 | 1, m, j));
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int d = 19;
  for (int i = 0; i <= d; i++) {
    T[i].clear();
    T[i].resize(1 << (d - i), 0);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    int q = query(d, 0, max(a - k, 0), min(a + k, 300000));
    ans = max(ans, q + 1);

    int p = a;
    T[0][p] = max(T[0][p], q + 1);
    p >>= 1;
    for (int j = 1; j <= d; j++) {
      T[j][p] = max(T[j - 1][p << 1], T[j - 1][p << 1 | 1]);
      p >>= 1;
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> T[20];

int query(int h, int k, int i, int j) {
  int m = (k << h) + (1 << h - 1);
  if (i == (k << h) && j == (k + 1 << h) - 1)
    return T[h][k];
  if (j < m)
    return query(h - 1, k << 1, i, j);
  if (i >= m)
    return query(h - 1, k << 1 | 1, i, j);
  return max(query(h - 1, k << 1, i, m - 1), query(h - 1, k << 1 | 1, m, j));
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int d = ceil(log2(n + 5));
  for (int i = 0; i <= d; i++) {
    T[i].clear();
    T[i].resize(1 << (d - i), 0);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    int q = query(d, 0, max(a - k, 0), min(a + k, 300000));
    ans = max(ans, q + 1);

    int p = a;
    T[0][p] = max(T[0][p], q + 1);
    p >>= 1;
    for (int j = 1; j <= d; j++) {
      T[j][p] = max(T[j - 1][p << 1], T[j - 1][p << 1 | 1]);
      p >>= 1;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> sgt;
int sz{1};

void g() {
  while (sz < 3e5)
    sz *= 2;
  sgt.resize(2 * sz);
}

int f(int l, int r) {
  int ans{};
  l += sz, r += sz;
  while (l <= r) {
    if (l & 1)
      ans = max(ans, sgt[l++]);
    if (!(r & 1))
      ans = max(ans, sgt[r--]);
    l /= 2, r /= 2;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  g();
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int z = f(max(0, a[i] - k), min(300000, a[i] + k));
    //	cout << z << endl;
    a[i] += sz;
    sgt[a[i]] = z + 1;
    a[i] /= 2;
    while (a[i]) {
      sgt[a[i]] = max(sgt[2 * a[i]], sgt[2 * a[i] + 1]);
      a[i] /= 2;
    }
  }
  cout << sgt[1];
}

Error: WA

Fixed submission:

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

vector<int> sgt;
int sz{1};

void g() {
  while (sz < 3e4)
    sz *= 2;
  sgt.resize(2 * sz);
}

int f(int l, int r) {
  int ans{};
  l += sz, r += sz;
  while (l <= r) {
    if (l & 1)
      ans = max(ans, sgt[l++]);
    if (!(r & 1))
      ans = max(ans, sgt[r--]);
    l /= 2, r /= 2;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  g();
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int z = f(max(0, a[i] - k), min(300000, a[i] + k));
    //	cout << z << endl;
    a[i] += sz;
    sgt[a[i]] = z + 1;
    a[i] /= 2;
    while (a[i]) {
      sgt[a[i]] = max(sgt[2 * a[i]], sgt[2 * a[i] + 1]);
      a[i] /= 2;
    }
  }
  cout << sgt[1];
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> constexpr int intof(const T x) {
  return static_cast<int>(x);
}

constexpr int mmax(const int x, const int y) { return max(x, y); }
constexpr int e() { return 0; }

int solve(const vector<int> &xs, const int k) {
  const int hi = *max_element(cbegin(xs), cend(xs));
  atcoder::segtree<int, mmax, e> max_lengths_by_val(hi + 1);

  for (const int x : xs) {
    const int h =
        max_lengths_by_val.prod(max(0, x - k), min(hi + 1, x + k + 1));
    max_lengths_by_val.set(x, h + 1);
  }

  return max_lengths_by_val.all_prod();
}

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

  int N, K;
  cin >> N >> K;

  vector<int> xs(N, 0);
  for (auto &x : xs)
    cin >> x;

  cout << solve(xs, K) << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

template <typename T> constexpr int intof(const T x) {
  return static_cast<int>(x);
}

int solve(const vector<int> &xs, const int k) {
  const int n = xs.size();
  vector<multiset<int>> trees(n);
  trees[0].insert(xs[0]);

  for (int i = 1; i < n; ++i) {
    trees[i] = trees[i - 1];
    trees[i].insert(xs[i]);
  }

  int ans = 1;

  for (int i = 1; i < n; ++i) {
    const auto tree = trees[i - 1];
    const auto lo = tree.lower_bound(max(0, xs[i] - k));
    const auto hi = tree.upper_bound(xs[i] + k);
    ans = max(ans, intof(distance(lo, hi)));
  }

  return ans;
}

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

  int N, K;
  cin >> N >> K;

  vector<int> xs(N, 0);
  for (auto &x : xs)
    cin >> x;

  cout << solve(xs, K) << '\\n';

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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vb vector<bool>
#define vl vector<long>
#define vi vector<int>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define f first
#define s second

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char dir[] = {'R', 'L', 'D', 'U'};
const int MOD = 1e9 + 7;

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<int> dp(n);

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

int main() {
  fastio

      // ifstream fin(".in");
      // ofstream fout(".out");

      int t = 1;
  // cin >> t;

  while (t-- > 0)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vb vector<bool>
#define vl vector<long>
#define vi vector<int>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char dir[] = {'R', 'L', 'D', 'U'};
const int MOD = 1e9 + 7;

void solve() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int dp[n];
  for (int i = 0; i < n; i++)
    dp[i] = 1;

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

  int result = 1;
  for (int i = 0; i < n; i++)
    if (result < dp[i])
      result = dp[i];
  cout << result << endl;
}

int main() {
  fastio

      // ifstream fin(".in");
      // ofstream fout(".out");

      int t = 1;
  // cin >> t;

  while (t-- > 0)
    solve();
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define ll int
#define MX 300000
using namespace std;
ll tr[4 * MX + 5], a[MX + 5];
void update(ll pos, ll st, ll ed, ll x, ll val) {
  if (st == ed && st == x) {
    tr[pos] = max(tr[pos], val);
    return;
  }
  ll mid = st + ((ed - st) >> 1);
  if (x <= mid)
    update(2 * pos, st, mid, x, val);
  else
    update(2 * pos + 1, mid + 1, ed, x, val);
  tr[pos] = max(tr[2 * pos], tr[2 * pos + 1]);
}
ll query(ll pos, ll st, ll ed, ll l, ll r) {
  if (st > r || ed < l)
    return 0;
  if (st >= l && ed <= r) {
    return tr[pos];
  }
  ll mid = st + ((ed - st) >> 1);
  return max(query(2 * pos, st, mid, l, r),
             query(2 * pos + 1, mid + 1, ed, l, r));
}
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]++;
  }
  for (int i = 1; i <= n; i++) {
    ll l = max(1, a[i] - k);
    ll r = min(300001, a[i] + k);
    ll p = query(1, 1, 300001, l, r);
    update(1, 1, 300001, a[i], p + 1);
  }
  cout << query(1, 1, 300001, 1, 300001) << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define ll int
#define MX 300000
using namespace std;
ll tr[4 * MX + 2], a[MX + 2];
void update(ll pos, ll st, ll ed, ll x, ll val) {
  if (st == ed && st == x) {
    tr[pos] = max(tr[pos], val);
    return;
  }
  ll mid = st + ((ed - st) >> 1);
  if (x <= mid)
    update(2 * pos, st, mid, x, val);
  else
    update(2 * pos + 1, mid + 1, ed, x, val);
  tr[pos] = max(tr[2 * pos], tr[2 * pos + 1]);
}
ll query(ll pos, ll st, ll ed, ll l, ll r) {
  if (st > r || ed < l)
    return 0;
  if (st >= l && ed <= r) {
    return tr[pos];
  }
  ll mid = st + ((ed - st) >> 1);
  return max(query(2 * pos, st, mid, l, r),
             query(2 * pos + 1, mid + 1, ed, l, r));
}
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    ll l = max(0, a[i] - k);
    ll r = min(300000, a[i] + k);
    ll p = query(1, 1, 300000, l, r);
    update(1, 1, 300000, a[i], p + 1);
  }
  cout << query(1, 1, 300000, 1, 300000) << "\\n";
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fileio                                                                 \\
  freopen("http://in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
#define ff first
#define ss second
#define N 2000001
#define M 1000000007
#define MOD 998244353
#define INF 1000000000000000
ll t[3000000] = {0};

void update(ll v, ll tl, ll tr, ll pos, ll new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2 + 1, tl, tm, pos, new_val);
    else
      update(v * 2 + 2, tm + 1, tr, pos, new_val);
    if (t[2 * v + 1] > t[2 * v + 2]) {
      t[v] = t[2 * v + 1];
    } else
      t[v] = t[2 * v + 2];
  }
}
ll sum(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  if ((sum(v * 2 + 1, tl, tm, l, min(r, tm)) >
       sum(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r)))
    return sum(v * 2 + 1, tl, tm, l, min(r, tm));
  else
    return sum(v * 2 + 2, tm + 1, tr, max(l, tm + 1), r);
}
int main() {

  ll n, m, i, k;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll dp[300001] = {0}, j, ans = 0;
  for (i = 0; i < n; i++) {
    ll mini = max(0ll, a[i] - k);
    ll maxi = min(300000ll, a[i] + k);
    ll p = sum(0, 0, 300000, mini, maxi);
    ans = max(ans, p + 1);
    update(0, 0, 300000, a[i], p + 1);
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SPEED                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fileio                                                                 \\
  freopen("http://in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
#define ff first
#define ss second
#define N 2000001
#define M 1000000007
#define MOD 998244353
#define INF 1000000000000000
vector<ll> v[100001];
ll vis[100001] = {0};
void dfs(ll u) {
  vis[u] = 1;
  for (auto it : v[u]) {
    if (vis[it] == 0) {
      dfs(it);
    }
  }
}
int main() {
  ll n, m, i, k;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll dp[300001] = {0}, j;
  for (i = 0; i < n; i++) {
    ll mini = max(0ll, a[i] - k);
    ll maxi = min(300000ll, a[i] + k);
    ll ans = 0;
    for (j = mini; j <= maxi; j++) {

      ans = max(dp[j] + 1, ans);
    }
    dp[a[i]] = ans;
    //	cout<<dp[a[i]]<<" "<<a[i]<<endl;
  }
  ll ans = 0;
  for (i = 0; i < 300001; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define _IO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const ll N = 3e5 + 5, mod = 1e9 + 7, inf = 1e18;
ll seg[4 * N];

void update(ll rt, ll l, ll r, ll id, ll v) {
  if (l > id || r < id)
    return;
  if (l == r) {
    seg[rt] = max(seg[rt], v);
    return;
  }
  ll md = (l + r) / 2;
  update(rt * 2, l, md, id, v);
  update(rt * 2 + 1, md + 1, r, id, v);
  seg[rt] = max(seg[rt * 2], seg[rt * 2 + 1]);
}

ll query(ll rt, ll l, ll r, ll i, ll j) {
  if (l > j || r < i)
    return -inf;
  if (l >= i && r <= j)
    return seg[rt];
  ll md = (l + r) / 2;
  return max(query(rt * 2, l, md, i, j), query(rt * 2 + 1, md + 1, r, i, j));
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, N, max(0LL, x - k), min(N, x + k));
    update(1, 0, N, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define _IO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const ll N = 3e5, mod = 1e9 + 7, inf = 1e18;
ll seg[3 * N];

void update(ll rt, ll l, ll r, ll id, ll v) {
  if (l > id || r < id)
    return;
  if (l == r) {
    seg[rt] = max(seg[rt], v);
    return;
  }
  ll md = (l + r) / 2;
  update(rt * 2, l, md, id, v);
  update(rt * 2 + 1, md + 1, r, id, v);
  seg[rt] = max(seg[rt * 2], seg[rt * 2 + 1]);
}

ll query(ll rt, ll l, ll r, ll i, ll j) {
  if (l > j || r < i)
    return -inf;
  if (l >= i && r <= j)
    return seg[rt];
  ll md = (l + r) / 2;
  return max(query(rt * 2, l, md, i, j), query(rt * 2 + 1, md + 1, r, i, j));
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, N, max(0LL, x - k), min(N, x + k));
    update(1, 0, N, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\\n";
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  vector<int> dp(n, 0);
  REP(i, n) for (int j = max(0, i - 100); j < i; j++) {
    if (abs(a[i] - a[j]) <= k)
      dp[i] = max(dp[i], dp[j] + 1);
  }
  int res = 0;
  REP(i, n)
  res = max(res, dp[i]);
  cout << res + 1 << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  vector<int> dp(n, 0);
  REP(i, n) REP(j, i) {
    if (abs(a[i] - a[j]) <= k)
      dp[i] = max(dp[i], dp[j] + 1);
  }
  int res = 0;
  REP(i, n)
  res = max(res, dp[i]);
  cout << res + 1 << '\\n';
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

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

int e() { return (int)-1e9; }
int main() {
  int am = 300100;
  int n, k;
  cin >> n >> k;
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  segtree<int, op, e> seg(am);

  for (int i = 0; i < n; i++) {

    int ai = A[i];
    int next = seg.prod(max(0, ai - k), min(am - 1, ai + k + 1)) + 1;
    seg.set(ai, max(next, 1));
  }

  cout << seg.prod(0, am) << endl;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1000000000;

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

  vector<int> dp(N);
  int ans = 0;

  for (int i = 0; i < N; i++) {
    chmax(dp[i], 1);
    for (int j = 0; j < i; j++) {

      if (abs(A[j] - A[i]) <= K)
        chmax(dp[i], dp[j] + 1);
      chmax(ans, dp[i]);
    }
  }
  cout << dp[N - 1] << endl;
}
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 = 0;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  build(1, 300002, 1);
  for (i = 1; i <= n; i++) {
    dp[i] = 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: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
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;
}
int main() {
  int n, k, i, ans = 1;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  build(0, n, 1);
  dp[1] = 1;
  change(1, 1, 1);
  for (i = 2; i <= n; i++) {
    dp[i] = max(1, ask(max(0, a[i] - k), min(n, a[i] + k), 1) + 1);
    change(a[i], dp[i], 1);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;

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

#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
int maxx[N << 2];

void push_up(int i) { maxx[i] = max(maxx[lson(i)], maxx[rson(i)]); }
int qMax(int i, int tl, int tr, int l, int r) {
  if (tl <= l && r <= tr) {
    return maxx[i];
  }
  int mid = (l + r) >> 1;
  int ans1 = 0, ans2 = 0;
  if (tl <= mid) {
    ans1 = qMax(lson(i), tl, tr, l, mid);
  }
  if (mid < tr) {
    ans2 = qMax(rson(i), tl, tr, mid + 1, r);
  }
  return max(ans1, ans2);
}

void updateS(int i, int l, int r, int idx, int val) {
  if (l == r) {
    maxx[i] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (idx <= mid) {
    updateS(lson(i), l, mid, idx, val);
  } else {
    updateS(rson(i), mid + 1, r, idx, val);
  }
  push_up(i);
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> k;
  rep(i, 1, n) { cin >> a[i]; }
  int ans = 0;
  rep(i, 1, n) {
    int l = max(0, a[i] - k) + 1, r = min(N - 5, a[i] + k) + 1;
    int res = qMax(1, l, r, 1, N - 4);
    res++;
    ans = max(res, ans);
    // cout<<"res:"<<res<<'\\n';
    // cout<<"ans:"<<ans<<'\\n';
    updateS(1, 1, N - 4, a[i] + 1, res);
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;

const int N = 3e5 + 5;
int a[N];
int dp[N];
int ans = 0;
int n, k;
void dfs(int x, int len) {
  if (len == n)
    return;
  ans = max(ans, dp[x]);
  for (int ad = 1; x + ad <= n; ++ad) {
    if (abs(a[x + ad] - a[x]) > 3) {
      if (dp[x + ad])
        ans = max(ans, dp[x + ad]);
      else
        dfs(x + ad, 1);
    } else {
      dp[x + ad] = len + 1;
      dfs(x + ad, len + 1);
      break;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> k;
  rep(i, 1, n) { cin >> a[i]; }
  dp[1] = 1;
  dfs(1, 1);
  cout << ans;
}