Problem:
Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
vector<pair<int, int>> seg;
int query(int node, int l, int r, int q_l, int q_r)
{
	if (q_l > r || q_r < l)
		return 0;
	if (seg[node].second != 0)
	{
		if (l == r)
		{
			seg[node].first = max(seg[node].first, seg[node].second);
			seg[node].second = 0;
		}
		else
		{
			seg[node].first = max(seg[node].first, seg[node].second);
			seg[node * 2].second = max(seg[node * 2].second, seg[node].second);
			seg[node * 2 + 1].second = max(seg[node * 2 + 1].second, seg[node].second);
			seg[node].second = 0;
		}
	}
	if (l >= q_l && r <= q_r)
		return seg[node].first;
	return max(query(node * 2, l, (l + r) / 2, q_l, q_r), query(node * 2 + 1, (l + r) / 2 + 1, r, q_l, q_r));
}
void update(int node, int l, int r, int q_l, int q_r, int val)
{
	if (q_l > r || q_r < l)
		return;
	if (seg[node].second != 0)
	{
		if (l == r)
		{
			seg[node].first = max(seg[node].first, seg[node].second);
			seg[node].second = 0;
		}
		else
		{
			seg[node].first = max(seg[node].first, seg[node].second);
			seg[node * 2].second = max(seg[node * 2].second, seg[node].second);
			seg[node * 2 + 1].second = max(seg[node * 2 + 1].second, seg[node].second);
			seg[node].second = 0;
		}
	}
	if (l >= q_l && r <= q_r)
	{
		seg[node].first = max(seg[node].first, val);
		if (l != r)
		{
			seg[node * 2].second = max(seg[node * 2].second, val);
			seg[node * 2 + 1].second = max(seg[node * 2 + 1].second, val);
			seg[node].second = 0;
		}
		return;
	}
	update(2 * node, l, (l + r) / 2, q_l, q_r, val);
	update(2 * node + 1, 1 + (l + r) / 2, r, q_l, q_r, val);
	seg[node].first = max(seg[node * 2].first, seg[node * 2 + 1].first);
	return;
}
void solve()
{
	int n, k;
	cin >> n >> k;
	int maxl = 300000 * 4 + 1;
	vector<int> arr(n);
	for (int c = 0; c < n; c++)
		cin >> arr[c];
	seg.assign(maxl, make_pair(0, 0));
	int ans = 1;
	update(1, 0, 300000, arr[0], arr[0], 1);
	for (int c = 1; c < n; c++)
	{
		int a = query(1, 0, 300000, max(0, arr[c] - k), min(300000, arr[c] + k));
		ans = max(ans, a + 1);
		update(1, 0, 300000, arr[c], arr[c], a + 1);
	}
	cout << ans << endl;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	t = 1;
	for (; t > 0; t--)
		solve();
}