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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define forn(i, a, b) for (int i = a; i < b; ++i)
#define SZ(x) int(x.size())
#define pb push_back
#define F first
#define S second
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}

const int N = 3 * 1e5 + 1;
int n, k;

typedef int T;
struct segtree {
  vector<T> st;
  int n;
  T neutro = 0;

  segtree(int p) {
    n = p;
    st.assign(n * 4, 0);
    build(1, 0, n - 1);
  }

  void build(int p, int L, int R) {
    if (L == R)
      st[p] = 0;
    else {
      int m = (L + R) / 2, l = p * 2, r = l + 1;
      build(l, L, m);
      build(r, m + 1, R);
      st[p] = max(st[l], st[r]);
    }
  }

  T query(int i, int j) { return query(1, 0, n - 1, i, j); }
  void upd(int i, int j, T val) { upd(1, 0, n - 1, i, j, val); }

  T query(int p, int L, int R, int i, int j) {
    if (i > R || j < L)
      return neutro;
    if (i <= L && j >= R)
      return st[p];
    int m = (L + R) / 2, l = p * 2, r = l + 1;
    T lf = query(l, L, m, i, j);
    T rg = query(r, m + 1, R, i, j);
    return max(lf, rg);
  }

  void upd(int p, int L, int R, int i, int j, T val) {
    if (i > R || j < L)
      return;
    if (i <= L && j >= R)
      st[p] = val;
    else {
      int m = (L + R) / 2, l = p * 2, r = l + 1;
      upd(l, L, m, i, j, val);
      upd(r, m + 1, R, i, j, val);
      st[p] = max(st[l], st[r]);
    }
  }
};

int main() {
#ifdef LOCAL
  freopen("d.txt", "r", stdin);
// freopen("main.txt", "w", stdout);
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
  ios::sync_with_stdio(0);
  cin.tie(0);
#define endl '\\n'
#define dbg(...) 18
#endif
  cin >> n >> k;
  int mx = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  segtree st(mx + 1);
  for (int i = 0; i < n; i++) {
    st.upd(a[i], a[i], st.query(a[i] - k, a[i] + k) + 1);
  }
  cout << (st.query(0, mx)) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define forn(i, a, b) for (int i = a; i < b; ++i)
#define SZ(x) int(x.size())
#define pb push_back
#define F first
#define S second
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}

const int N = 3 * 1e5 + 1;
int n, k;

int main() {
#ifdef LOCAL
  freopen("d.txt", "r", stdin);
// freopen("main.txt", "w", stdout);
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
  ios::sync_with_stdio(0);
  cin.tie(0);
#define endl '\\n'
#define dbg(...) 18
#endif
  cin >> n >> k;
  map<int, int> mp;
  int a, mx;
  for (int i = 0; i < n; i++) {
    cin >> a;
    auto x = mp.lower_bound(a - k);
    if (x == mp.end()) {
      mp[a] = 1;
    } else {
      mx = 0;
      while (x->F <= a + k && x != mp.end()) {
        mx = max(mx, x->S);
        // mp.erase(x->F);
        x++;
      }
      mp[a] = mx + 1;
    }
  }
  int ans = 0;
  for (auto x : mp)
    ans = max(ans, x.S);
  cout << ans << endl;
  return 0;
}