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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1)
      os << ' ';
  }
  return os;
}

class SegTree {
public:
  SegTree(vector<ll> vec) {
    int size = vec.size();
    n = 1;
    while (n < size)
      n *= 2;

    node.resize(2 * n - 1, IDENT);
    lazy.resize(2 * n - 1, INF);

    loop(i, 0, size) node[i + n - 1] = vec[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = f(node[c1(i)], node[c2(i)]);
  }

  void update(int l, int r, ll v) { updateRec(l, r, v, 0, 0, n); }

  ll get(int l, int r) { return getRec(l, r, 0, 0, n); }

  void debug() {
    cout << "node: " << node << endl;
    cout << "lazy: " << lazy << endl;
  }

private:
  int n;
  vector<ll> node, lazy;
  const ll INF = 1000000000000000000;
  const ll IDENT = 0;

  ll f(ll a, ll b) { return max(a, b); }

  void f_lazy(ll &a, ll b) { a = b; }

  inline int p(int i) { return (i - 1) / 2; }
  inline int c1(int i) { return i * 2 + 1; }
  inline int c2(int i) { return i * 2 + 2; }

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

    if (r - l > 1) {
      f_lazy(lazy[c1(k)], lazy[k] / 2);
      f_lazy(lazy[c2(k)], lazy[k] / 2);
    }

    lazy[k] = INF;
  }

  void updateRec(int l, int r, ll v, int k, int selfL, int selfR) {
    eval(k, l, r);
    if (r <= selfL or selfR <= l)
      return;
    if (l <= selfL and selfR <= r) {
      lazy[k] = (r - l) * v;
      eval(k, l, r);
      return;
    }

    updateRec(l, r, v, c1(k), selfL, (selfL + selfR) / 2);
    updateRec(l, r, v, c2(k), (selfL + selfR) / 2, selfR);
    node[k] = f(node[c1(k)], node[c2(k)]);
  }

  ll getRec(int l, int r, int k, int selfL, int selfR) {
    if (r <= selfL or selfR <= l)
      return IDENT;
    eval(k, l, r);
    if (l <= selfL and selfR <= r) {
      eval(k, l, r);
      return node[k];
    }

    return f(getRec(l, r, c1(k), selfL, (selfL + selfR) / 2),
             getRec(l, r, c2(k), (selfL + selfR) / 2, selfR));
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> A(n, 0);
  loop(i, 0, n) cin >> A[i];
  SegTree seg(vector<ll>(600100, 0));

  loop(i, 0, n) {
    ll a = A[i];
    ll ma = seg.get(max(a - k, 0LL), a + k + 1);
    seg.update(a, a + 1, ma + 1);
    // seg.debug();
  }

  ll ans = 0;
  loop(i, 0, 300000) { ans = max(ans, seg.get(i, i + 1)); }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)

typedef pair<ll, ll> P;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> A(n);
  loop(i, 0, n) cin >> A[i];
  queue<P> Q;
  Q.emplace(-1, 0);
  loop(i, 0, n) {
    int m = Q.size();
    ll a = A[i];
    loop(j, 0, m) {
      auto p = Q.front();
      Q.pop();
      if (p.first == -1 or abs(p.first - a) <= k) {
        // とれるからとる
        Q.emplace(a, p.second + 1);

        // あえてとらない
        Q.push(p);
      } else {
        // とらない
        Q.push(p);
      }
    }
  }

  ll ma = 0;
  while (Q.size()) {
    auto p = Q.front();
    Q.pop();
    ma = max(ma, p.second);
  }
  cout << ma << endl;
  return 0;
}