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

/*
       ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄
      ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌
      ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌
      ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌ ▄▄▄▄▄▄▄▄ ▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌▐░░░░░░░░▌▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌ ▀▀▀▀▀▀█░▌▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌       ▐░▌▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌
      ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌
       ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀

 #can_change_the_world_with_the_help_of_0_1
                                    -darshan786    */

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define F first
#define MAXN 1010
#define int long long
#define S second
#define all(v) v.begin(), v.end()
#define PI pair<int, int>
#define PIS pair<int, string>

int values[41];

void value() {
  values[0] = 1;
  for (int i = 1; i <= 40; i++) {
    values[i] = 2 * values[i - 1];
  }
}
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]);
}

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

  int n, k;

  cin >> n >> k;

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

  auto solve = [&]() {
    int mx = 1;

    for (int i = 1; i <= n; ++i) {
      int t1 = query(1, 1, 3e5, max(1LL, a[i] - k), min(300000ll, 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);
      }
    }

    return mx;
  };

  cout << solve();
}


Error: TLE

Fixed submission:

/*
       ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄
      ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌
      ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌
      ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌ ▄▄▄▄▄▄▄▄ ▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌▐░░░░░░░░▌▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌ ▀▀▀▀▀▀█░▌▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░▌       ▐░▌▐░▌       ▐░▌▐░▌       ▐░▌
      ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌
      ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌
       ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀

 #can_change_the_world_with_the_help_of_0_1
                                    -darshan786    */

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mod 1000000007
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define F first
#define MAXN 1010
#define int long long
#define S second
#define all(v) v.begin(), v.end()
#define PI pair<int, int>
#define PIS pair<int, string>

int values[41];

void value() {
  values[0] = 1;
  for (int i = 1; i <= 40; i++) {
    values[i] = 2 * values[i - 1];
  }
}

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

  int n, k;
  cin >> n >> k;
  int s[n + 1];
  for (int i = 0; i < n; i++)
    cin >> s[i];

  vector<int> dp(n, 0);

  auto solve = [&]() {
    map<int, int> ma;

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

      int curr = s[i];

      int low = max(curr - k, 0ll);

      int high = min(curr + k, n);

      for (int j = low; j < high + 1; j++)
        dp[i] = max(dp[i], ma[j] + 1);

      ma[curr] = max(dp[i], ma[curr]);
    }

    int ans = 0;

    for (int i : dp)
      ans = max(i, ans);

    return ans;
  };

  cout << solve() << endl;
}