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

// #include <atcoder/all>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
// using namespace atcoder;
struct Segtree {
  typedef int sgT;
  vector<sgT> seg;
  int nbn;
  sgT INF;
  void init(int n) {
    for (nbn = 1; nbn < n + 2; nbn <<= 1)
      ;
    INF = 0; // min:+inf,max:-inf,0
    seg.assign(nbn * 2, INF);
  }
  sgT comp(sgT l, sgT r) { return max(l, r); } // min<->max
  void update(int k, sgT x) {
    k += nbn - 1;
    seg[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      seg[k] = comp(seg[k * 2 + 1], seg[k * 2 + 2]);
    }
  }
  sgT query2(int a, int b, int l, int r, int k) {
    if (b <= l || r <= a)
      return INF;
    if (a <= l && r <= b)
      return seg[k];
    return comp(query2(a, b, l, (l + r) / 2, k * 2 + 1),
                query2(a, b, (l + r) / 2, r, k * 2 + 2));
  }
  sgT query(int a, int b) { return query2(a, b, 0, nbn, 0); }
  sgT get(int a) { return seg[nbn + a - 1]; } // =query(a,a+1)
  // void disp(){ for(auto x :seg) cout<<x<<", "; puts("");}
  int getpos(sgT v, int l, int r) { // vの最左位置を二分探で求める
    int lo = l - 1, hi = r;
    while (hi - lo > 1) {
      int md = (hi + lo) >> 1;
      if (query(lo, md + 1) == v)
        hi = md;
      else
        lo = md;
    }
    return hi;
  }
};

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  const int zsz = 301000;
  int n, k;
  cin >> n >> k;
  VI a(n);
  rep(i, n) cin >> a[i];
  Segtree sg;
  sg.init(zsz + 1);
  rep(i, n) {
    int cr = sg.query(max(0, a[i] - k), min(zsz, a[i] + k + 1)) + 1;
    sg.update(a[i], cr);
  }
  cout << sg.query(0, zsz) << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

// #include <atcoder/all>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
// using namespace atcoder;

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

// using mint = static_modint<MD>;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VI a(n);
  rep(i, n) cin >> a[i];
  VI dp(n + 10);
  dp[0] = 1;
  rep(i, n) {
    rep2(j, i + 1, n) {
      if (abs(a[i] - a[j]) <= k)
        dp[j] = max(dp[j], dp[i] + 1);
    }
  }
  int ans = 0;
  rep(i, n) ans = max(ans, dp[i]);
  cout << ans << "\\n";
  return 0;
}