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

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long MOD = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int MAXN = 300050;

ll a[MAXN];
struct Node {
  ll mx, lz_sum;
} tr[4 * MAXN + 10];
ll in[MAXN + 10];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
/*ll max(ll a,ll b)
{
        if(a>=b)return a;
        else return b;
}
ll min(ll a,ll b)
{
        if(a>=b)return b;
        else return a;
}*/
void build(int p, int l, int r) {
  if (r == l) {
    tr[p].mx = in[l];
    tr[p].lz_sum = 0;
    return;
  }
  build(p * 2, l, (l + r) / 2);
  build(p * 2 + 1, (l + r) / 2 + 1, r);
  tr[p].mx = max(tr[p * 2].mx, tr[p * 2 + 1].mx);
}
void change(ll val, int l, int r, int p, int lb, int rb) {
  if (lb == l && rb == r) {
    tr[p].mx = val;
    return;
  }
  int mid = (l + r) / 2;
  if (lb <= mid)
    change(val, l, mid, p * 2, lb, rb);
  if (rb >= mid + 1)
    change(val, mid + 1, r, p * 2 + 1, lb, rb);
  tr[p].mx = max(tr[p * 2].mx, tr[p * 2 + 1].mx);
}
ll query_max(int p, int l, int r, int lb, int rb) {
  if (lb <= l && rb >= r)
    return tr[p].mx;
  int mid = (l + r) / 2;
  ll temp = 0;
  if (lb <= mid && rb > mid)
    temp = max(query_max(p * 2, l, mid, lb, rb),
               query_max(p * 2 + 1, mid + 1, r, lb, rb));
  else if (lb > mid)
    temp = query_max(p * 2 + 1, mid + 1, r, lb, rb);
  else if (rb <= mid)
    temp = query_max(p * 2, l, mid, lb, rb);
  return temp;
}
void solve(int T) {
  ll N, l, r, k;
  N = read();
  k = read();
  ll maxx = 0;
  for (int i = 1; i <= N; i++) {
    a[i] = read();
    maxx = max(a[i], maxx);
  }
  build(1, 0, N);
  ll tmp = 0;
  for (int i = 1; i <= N; i++) {
    tmp = query_max(1, 0, maxx, max(0ll, a[i] - k), a[i] + k);
    change(tmp + 1, 0, maxx, 1, a[i], a[i]);
  }
  printf("%lld", query_max(1, 1, N, 1, N));
}
signed main() {
  int t = 1;
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // scanf("%d",&t);
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const long long MOD = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int MAXN = 300050;

int a[MAXN], dp[MAXN];
int lg[MAXN] = {-1};
int maxn[MAXN][50];
int minn[MAXN][50];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
void create_ST(int n) {
  for (int i = 1; i <= n; i++) {
    lg[i] = lg[i / 2] + 1;
    maxn[i][0] = a[i];
    minn[i][0] = a[i];
  }
  for (int i = 1; i <= lg[n]; i++) {
    for (int j = 1; j + (1 << i) - 1 <= n; j++) {
      maxn[j][i] = max(maxn[j][i - 1], maxn[j + (1 << (i - 1))][i - 1]);
      minn[j][i] = min(minn[j][i - 1], minn[j + (1 << (i - 1))][i - 1]);
    }
  }
}
ll query_max(int l, int r) {
  int len = lg[r - l + 1];
  return max(maxn[l][len], maxn[r - (1 << (len)) + 1][len]);
}
ll query_min(int l, int r) {
  int len = lg[r - l + 1];
  return min(minn[l][len], minn[r - (1 << (len)) + 1][len]);
}

void solve(int T) {
  int n, l, r, k;
  n = read();
  k = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
  }
  create_ST(n);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (query_max(1, i - 1) + k < a[i] || query_min(1, i - 1) - k > a[i]) {
      dp[i] = 1;
      continue;
    }
    for (int j = 1; j < i; j++) {
      if (a[j] + k < a[i] || a[j] - k > a[i])
        continue;
      dp[i] = max(dp[i], dp[j] + 1);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(ans, dp[i]);
  printf("%d", ans);
}
signed main() {
  int t = 1;
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // scanf("%d",&t);
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}