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

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

template <typename T> class LazySegmentTree {
  using fn = function<T(T, T)>;

public:
  LazySegmentTree(vector<T> &base, fn _fx, fn _fa, fn _fm, T _ex, T _em)
      : fx(_fx), fa(_fa), fm(_fm), ex(_ex), em(_em) {
    n = 1;
    while (n < base.size())
      n *= 2;

    node.resize(2 * n - 1, ex);
    REP(i, 0, base.size()) node[n - 1 + i] = base[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = fx(node[2 * i + 1], node[2 * i + 2]);

    lazy.resize(2 * n - 1, em);
  }

  void eval(int k, int l, int r) {
    if (lazy[k] == em)
      return;

    if (r - l > 1) {
      lazy[2 * k + 1] = fm(lazy[2 * k + 1], lazy[k]);
      lazy[2 * k + 2] = fm(lazy[2 * k + 2], lazy[k]);
    }
    node[k] = fa(node[k], lazy[k]);
    lazy[k] = em;
  }

  void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      lazy[k] = fm(lazy[k], x);
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = fx(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (r <= a || b <= l)
      return ex;
    if (a <= l && r <= b)
      return node[k];
    T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return fx(vl, vr);
  }

  void print(int w = 4) {
    cout << "#node : " << endl;
    int prev = 0;
    for (int i = 1; i <= n; i *= 2) {
      for (int j = prev; j < prev + i; j++) {
        cout << setw(n * w / i) << setfill(' ');
        if (node[j] == ex)
          cout << "ex";
        else
          cout << node[j];
      }
      cout << endl;
      prev += i;
    }
    cout << "#lazy : " << endl;
    prev = 0;
    for (int i = 1; i <= n; i *= 2) {
      for (int j = prev; j < prev + i; j++) {
        cout << setw(n * w / i) << setfill(' ');
        if (lazy[j] == em)
          cout << "em";
        else
          cout << lazy[j];
      }
      cout << endl;
      prev += i;
    }
  }

private:
  int n;
  vector<T> node, lazy;
  fn fx, fa, fm;
  T ex, em;
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  int maxv = 0;
  REP(i, 0, N) {
    cin >> A[i];
    maxv = max(maxv, A[i]);
  }

  auto fx = [](auto l, auto r) { return max(l, r); };
  auto fa = [](auto l, auto r) { return max(l, r); };
  auto fm = [](auto l, auto r) { return max(l, r); };
  vector<int> base(maxv + 1, 0);
  REP(i, 0, N) base[A[i]] = 1;
  LazySegmentTree<int> seg(base, fx, fa, fm, 0, 0);

  int ans = 1;
  for (int i = N - 1; i >= 0; i--) {
    int cur = seg.query(A[i], A[i] + 1);
    ans = max(ans, cur);
    seg.update(max(0, A[i] - K), min(maxv + 1, A[i] + K + 1), cur + 1);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int N, K;
vector<int> memo;
vector<int> A;

int search(int pos) {
  if (memo[pos] != -1)
    return memo[pos];

  int max_cnt = 0;
  REP(i, pos + 1, N) {
    if (abs(A[i] - A[pos]) <= K)
      max_cnt = max(max_cnt, search(i));
  }

  return memo[pos] = (max_cnt + 1);
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  A.resize(N);
  REP(i, 0, N) cin >> A[i];

  memo.resize(N, -1);
  int ans = 0;
  REP(i, 0, N) { ans = max(ans, search(i)); }
  cout << ans << endl;
  return 0;
}