Language: C++

//https://atcoder.jp/contests/abl/tasks/abl_d
//ACL Beginner Contest : D - Flat Subsequence

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<queue>

/*
Problem.
数列A1 ... ANと整数Kについて、
以下の条件を満たす数列Bの長さとして考えられる最大値を出力せよ。
- BはAの（連続とは限らない）部分列
- どのBの隣あう要素の差の絶対値もK以下

Point.
SegTreeを用いる。
格納する値は、各要素選択時の最大の連結数である。
rep
  1.各要素xについて、x±kの範囲で今までに選択された要素を見て、xの連結数はその範囲の最大値+1となる。
  2.xの連結数を更新する
  3.今までの最大連結数と比較
最大連結数を出力
*/

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pii = pair<int, int>;
using ppi = pair<pii, int>;


int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }
    void set(int p, S x) { //再帰的に指定地点の値の更新を行う
        assert(0 <= p && p < this->_n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < this->_n);
        return d[p + size];
    }

    S prod(int l, int r) { //opの演算を指定範囲で行い、結果を返す
        assert(0 <= l && l <= r && r <= this->_n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= this->_n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

int op(int a, int b){ //演算（結合律が必要）
    return max(a, b);
}

int e(){ //単位元,モノロイド(a○e = e○a = a)
    return 0;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
  segtree<int, op, e> seg(300005);
  int ans = 0;
  for(int i = 0; i < n; i++){
    int x = a[i];
    int l = max(x-k, 0), r = min(x+k+1, 300001); //半開区間を指定する。
    int now = seg.prod(l, r) + 1; //半開区間の全ての数における、それまでに選択された値のうち連結数の最大値
    seg.set(x, now); //値の更新
    ans = max(ans, now);
  }

  cout << ans << endl;

  return 0;

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}


Compiler output:
a.cc: In member function 'void segtree<S, op, e>::set(int, S)':
a.cc:55:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   55 |         assert(0 <= p && p < this->_n);
      |         ^~~~~~
a.cc:55:9: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc: In member function 'S segtree<S, op, e>::get(int)':
a.cc:62:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   62 |         assert(0 <= p && p < this->_n);
      |         ^~~~~~
a.cc: In member function 'S segtree<S, op, e>::prod(int, int)':
a.cc:67:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   67 |         assert(0 <= l && l <= r && r <= this->_n);
      |         ^~~~~~
a.cc: In member function 'int segtree<S, op, e>::max_right(int, F)':
a.cc:87:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
   87 |         assert(0 <= l && l <= this->_n);
      |         ^~~~~~
a.cc: In member function 'int segtree<S, op, e>::min_left(int, F)':
a.cc:114:9: error: there are no arguments to 'assert' that depend on a template parameter, so a declaration of 'assert' must be available [-fpermissive]
  114 |         assert(0 <= r && r <= _n);
      |         ^~~~~~
a.cc: In instantiation of 'S segtree<S, op, e>::prod(int, int) [with S = int; S (* op)(S, S) = op; S (* e)() = e]':
a.cc:168:23:   required from here
  168 |     int now = seg.prod(l, r) + 1; //半開区間の全ての数における、それまでに選択された値のうち連結数の最大値
      |               ~~~~~~~~^~~~~~
a.cc:67:15: error: 'assert' was not declared in this scope
   67 |         assert(0 <= l && l <= r && r <= this->_n);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:10:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
    9 | #include<queue>
  +++ |+#include <cassert>
   10 | 
a.cc: In instantiation of 'void segtree<S, op, e>::set(int, S) [with S = int; S (* op)(S, S) = op; S (* e)() = e]':
a.cc:169:12:   required from here
  169 |     seg.set(x, now); //値の更新
      |     ~~~~~~~^~~~~~~~
a.cc:55:15: error: 'assert' was not declared in this scope
   55 |         assert(0 <= p && p < this->_n);
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
a.cc:55:15: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'