Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";

using namespace std;
const ll maxn = 300000;
ll tree[4 * maxn + 20];
ll query(ll node, ll st, ll en, ll le, ll re) {
  if (le > en or re < st)
    return 0;
  if (le <= st and en <= re)
    return tree[node];
  if (st == le && en == re)
    return tree[node];
  ll mid = (st + en) / 2;
  return max(query(2 * node, st, mid, le, re),
             query(2 * node + 1, mid + 1, en, le, re));
}
void update(ll node, ll st, ll en, ll idx, ll val) {
  if (st == en) {
    tree[node] = max(val, tree[node]);
    return;
  }

  ll mid = (st + en) / 2;
  if (idx <= mid)
    update(2 * node, st, mid, idx, val);
  else
    update(2 * node + 1, mid + 1, en, idx, val);

  tree[node] = max(tree[2 * node], tree[2 * node + 1]);
}
signed main() {
  fast;

  ll n, k, i, j;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll pmi = max(0LL, x - k);
    ll pma = min(maxn, x + k);
    ll num = query(1LL, 0LL, maxn, pmi, pma);
    update(1LL, 0LL, maxn, x, num + 1);
  }
  ll ans = query(1LL, 0LL, maxn, 0LL, maxn);

  cout << ans;
}

Error: TLE

Fixed submission:

// Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!

/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 998244353
#define watch(x) cout << (#x) << " is " << (x) << "\\n"
#define all(v) (v).begin(), (v).end()
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(0);
#define printclock                                                             \\
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\\n";
#define pii pair<ll, ll>
#define vi(x) vector<x>
using namespace std;
const ll maxn = 300000;
ll tree[4 * maxn + 20];
ll query(ll node, ll st, ll en, ll le, ll re) {

  if (st == le && en == re)
    return tree[node];
  ll mid = (st + en) / 2;
  if (re <= mid)
    return query(2 * node, st, mid, le, re);
  else if (le > mid)
    return query(2 * node + 1, mid + 1, en, le, re);
  else
    return max(query(2 * node, st, mid, le, mid),
               query(2 * node + 1, mid + 1, en, mid + 1, re));
}
void update(ll node, ll st, ll en, ll idx, ll val) {
  if (st == en) {
    tree[node] = val;
  } else {
    ll mid = (st + en) / 2;
    if (idx <= mid)
      update(2 * node, st, mid, idx, val);
    else
      update(2 * node + 1, mid + 1, en, idx, val);
    tree[node] = max(tree[2 * node], tree[2 * node + 1]);
  }
}
int32_t main() {
  fast;

  ll n, k, i, j;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll pmi = max(0LL, x - k);
    ll pma = min(maxn, x + k);
    ll num = query(1LL, 0LL, maxn, pmi, pma + 1);
    update(1LL, 0LL, maxn, x, num + 1);
  }
  ll ans = query(1LL, 0LL, maxn, 0LL, maxn);
  cout << ans;
}