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

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define fill fill_n
#define pf push_front
#define pb push_back
#define FOR(i, l, r) for (int i = l; i < r; i++)
#define ROF(i, r, l) for (int i = r; i >= l; i--)
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
typedef vector<int> vi;
typedef vector<ll> vli;
typedef vector<pair<int, int>> vpi;

const int M = 1e9 + 7;
const int N = 3e5;
ll po(int, int);

int tree[4 * N + 5];

void update(int n, int s, int e, int id, int val) {
  if (s == e) {
    tree[n] = val;
    return;
  }
  int mid = (s + e) >> 1;
  if (id >= s && id <= mid) {
    update(2 * n, s, mid, id, val);

  } else
    update(2 * n + 1, mid + 1, e, id, val);

  tree[n] = max(tree[2 * n], tree[2 * n + 1]);
}
int query(int n, int s, int e, int l, int r) {
  if (s > r || e < l)
    return 0;
  if (s >= l && e <= r)
    return tree[n];
  int mid = (s + e) >> 1;
  return max(query(2 * n, s, mid, l, r), query(2 * n + 1, mid + 1, e, l, r));
}

void solve() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  int el;
  int ans = 0;
  FOR(i, 0, n) {
    cin >> el;
    int x = query(1, 0, N, el - k, el + k);
    ans = max(ans, x + 1);
    update(1, 0, N, el, x + 1);
  }

  cout << ans << "\\n";
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;
  FOR(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }

  return 0;
}

ll po(int a, int b) {
  if (b == 0)
    return 1;
  ll ans = 1;
  if (b % 2 == 0) {
    ans = po(a, b / 2) % M;
    ans = (ans % M * ans % M) % M;
  } else {
    ans = po(a, (b - 1) / 2) % M;
    ans = (ans % M * ans % M * a) % M;
  }
  return ans % M;
}


Error: TLE

Fixed submission:

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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

// #define ordered_set tree<int, null_type,less<int>,
// rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define fill fill_n
#define pf push_front
#define pb push_back
#define FOR(i, l, r) for (int i = l; i < r; i++)
#define ROF(i, r, l) for (int i = r; i >= l; i--)
#define all(v) (v).begin(), (v).end()
#define ff first
#define ss second
typedef vector<int> vi;
typedef vector<ll> vli;
typedef vector<pair<int, int>> vpi;

const int M = 1e9 + 7;
const int N = 2e5;
ll po(int, int);

vi e[N];
vector<bool> vis;
void dfs(int u) {
  vis[u] = 1;
  for (auto i : e[u]) {
    if (!vis[i])
      dfs(i);
  }
}

void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  FOR(i, 0, n) { cin >> a[i]; }

  int dp[n];
  FOR(i, 0, n) { dp[i] = 1; }
  FOR(i, 1, n) {

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

  cout << ans << "\\n";
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;
  FOR(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }

  return 0;
}

ll po(int a, int b) {
  if (b == 0)
    return 1;
  ll ans = 1;
  if (b % 2 == 0) {
    ans = po(a, b / 2) % M;
    ans = (ans % M * ans % M) % M;
  } else {
    ans = po(a, (b - 1) / 2) % M;
    ans = (ans % M * ans % M * a) % M;
  }
  return ans % M;
}