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

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

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\\n"
#define pb push_back
#define sq(a) (a) * (a)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \\
  cerr << #v << " : ";                                                         \\
  for (auto x : v)                                                             \\
    cerr << x << ' ';                                                          \\
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return result;
}

const int N = 3e5;

// Use Segment Tree for fuck sake

struct item {
  ll v;
};

struct segtree {
  int size;
  vector<item> values;

  void init(int n) {
    size = 1;
    while (size < n)
      size <<= 1;

    values.assign(2 * size, NEUTRAL_ELEMENT);
  }

  item NEUTRAL_ELEMENT = {0};

  item single(int v) { return {v}; }

  item merge(item a, item b) { return {max(a.v, b.v)}; }

  void build(vector<int> &a, int x, int lx, int rx) {
    if (rx - lx == 1) {
      if (lx < a.size())
        values[x] = single(a[lx]);
      return;
    }

    int m = (lx + rx) / 2;

    build(a, 2 * x + 1, lx, m);
    build(a, 2 * x + 2, m, rx);

    values[x] = merge(values[2 * x + 1], values[2 * x + 2]);
  }

  void build(vector<int> &a) { build(a, 0, 0, size); }

  void set(int i, int v, int x, int lx, int rx) {
    if (rx - lx == 1) {
      values[x] = single(v);
      return;
    }

    int m = (lx + rx) / 2;

    if (i < m)
      set(i, v, 2 * x + 1, lx, m);
    else
      set(i, v, 2 * x + 2, m, rx);

    values[x] = merge(values[2 * x + 1], values[2 * x + 2]);
  }

  void set(int i, int v) { set(i, v, 0, 0, size); }

  item calc(int l, int r, int x, int lx, int rx) {
    if (lx >= r || rx <= l)
      return NEUTRAL_ELEMENT;

    if (lx >= l && rx <= r)
      return values[x];

    int m = (lx + rx) / 2;

    item s1 = calc(l, r, 2 * x + 1, lx, m);
    item s2 = calc(l, r, 2 * x + 2, m, rx);

    return merge(s1, s2);
  }

  item calc(int l, int r) { return calc(l, r, 0, 0, size); }
};

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

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

  segtree st;
  st.init(N + 1);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;

    int l = max(x - k, 0), r = min(x + k, N);
    int temp = st.calc(l, r + 1).v + 1;
    ans = max(ans, temp);
    st.set(x, temp);
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\\n"
#define pb push_back
#define sq(a) (a) * (a)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \\
  cerr << #v << " : ";                                                         \\
  for (auto x : v)                                                             \\
    cerr << x << ' ';                                                          \\
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n) {
    if (n & 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return result;
}

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

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

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

  int ans = 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);
    }
    ans = max(ans, dp[i]);
  }

  cout << ans << endl;
  return 0;
}