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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pll pair<int, int>
#define int long long int
#define endl "\\n"

#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define ones(x) __builtin_popcount(x)
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 998244353

ll mod_pow(ll a, ll b, ll m) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}

ll mod_inverse(int a, int m) { return mod_pow(a, m - 2, m); }

const int maxn = 3e5 + 5;

int a[maxn];

int tree[6 * maxn];

int query(int v, int l, int r, int lq, int rq) {
  if (lq <= l && r <= rq) {
    return tree[v];
  }

  if (l > rq or lq > r) {
    return 0;
  }

  int mid = (l + r) / 2;

  int t1 = query(2 * v + 1, l, mid, lq, rq);
  int t2 = query(2 * v + 2, mid + 1, r, lq, rq);

  return max(t1, t2);
}

void update(int v, int pos, int l, int r, int val) {
  if (l == r) {
    tree[v] = val;
    return;
  }

  int mid = (l + r) / 2;

  if (pos <= mid) {
    update(2 * v + 1, pos, l, mid, val);
  } else {
    update(2 * v + 2, pos, mid + 1, r, val);
  }

  tree[v] = max(tree[2 * v + 1], tree[2 * v + 2]);
}

signed main() {
  fast;

  int n, k;

  cin >> n >> k;

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

  int mx = 1;

  for (int i = 1; i <= n; ++i) {
    int t1 = query(1, 1, 3e5, max(1LL, a[i] - k), min((ll)3e5, a[i] + k));
    int t2 = query(1, 1, 3e5, a[i], a[i]);

    if (t2 < t1 + 1) {
      update(1, a[i], 1, 3e5, t1 + 1);
      mx = max(mx, t1 + 1);
    }
  }

  cout << mx;

  return 0;
}

Error: TLE

Fixed submission:

/// // // // // // // // // ///
// Author : Kh@ntil S@nghani //
/// // // // // // // // // ///

#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define ll long long int
#define ld long double
#define ss second
#define ff first
#define inf LLONG_MAX
#define con continue
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define M 1000000007
#define endl '\\n'
// if vector isn't soted then it will remove consecutive same elements
#define make_unique(vec) vec.erase(unique(all(vec)), vec.end())
template <class T> ostream &operator<<(ostream &cout, vector<T> V) {
  cout << "[ ";
  for (auto v : V)
    cout << v << ' ';
  return cout << ']';
}
template <typename T> ostream &operator<<(ostream &out, const set<T> &N_A) {
  out << "{ ";
  for (const auto &i : N_A)
    out << i << ", ";
  return out << "}" << endl;
}
template <typename T, typename G>
ostream &operator<<(ostream &out, const map<T, G> &N_A) {
  out << "< ";
  for (const auto &i : N_A)
    out << i << ", ";
  return out << ">" << endl;
}
template <typename T> inline istream &operator>>(istream &in, vector<T> &N_A) {
  for (auto &x : N_A)
    in >> x;
  return in;
}
#define MAXN 10000001

ll dp[300005], a[300005];
ll n, k;
ll rec(ll x) {
  if (x >= n)
    return 0;
  if (dp[x] != -1)
    return dp[x];
  ll ans = 1;

  for (ll i = x + 1; i < n; i++) {
    if (i < n) {
      if (abs(a[x] - a[i]) <= k) {
        ans = max(ans, 1 + rec(i));
      }
    }
  }

  // ans=max(ans,rec(i+1));

  return dp[x] = ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll _;
  _ = 1;
  // cin>>_;

  while (_--) {
    // cout<<fixed<<setprecision(9)<<y<<" "<<x<<endl;
    // cout<<_<<endl;
    cin >> n >> k;
    for (ll i = 0; i < n; i++) {
      dp[i] = -1;
      cin >> a[i];
    }
    for (ll i = n; i < n + 5; i++) {
      dp[i] = -1;
      a[i] = -1;
    }
    ll ans = 0;
    // for(ll i=0;i<n;i++)
    // {
    // 	ans=max(ans,rec(i));
    // }
    for (ll i = n - 1; i >= 0; i--) {
      ans = max(ans, rec(i));
    }
    cout << ans << endl;
    // for(ll i=0;i<n;i++) cout<<dp[i]<<" ";
  }
  return 0;
}