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: Runtime Error)

#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

#define ROOT(d, x) (d).root(&(d), (x))
#define UNITE(d, x, y) (d).unite(&(d), (x), (y))
#define UPDATE(d, x, y) (d).update(&(d), (x), (y))
#define QUERY(d, x, y) (d).query(&(d), (x), (y))

/* prototype */

struct union_find;
struct segment_int;

static inline int get(void);
static inline void put(int);

static inline uint32_t flp2(uint32_t x);

int32_t nextint(void);
int64_t nextlong(void);
uint32_t nextstr(char *);
void printint(int32_t);
void printlong(int64_t);
void rsort_d32(void *base, const int n, const int size, const int index);
void rsort_r32(void *base, const int n, const int size, const int index);

void union_find_init(struct union_find *p, int n);
void segment_init(struct segment_int *p, int n, int (*op)(int, int), int defval);

typedef struct union_find{
	int *p_prev, *p_rank, *p_size;
	int (*root)(struct union_find *, int);
	void (*unite)(struct union_find *, int, int);
	int (*size)(struct union_find *, int);
} union_find;

typedef struct segment_int{
	int n;
	int *data;
	int defval;
	int (*op)(int, int);
	void (*update)(struct segment_int *, int, int);
	int (*query)(struct segment_int *, int, int);
} segment;
/* main */

//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

int max2(int a, int b){ return a<b?b:a; }

segment s;

int a[300000];
int main(void){
	int n=nextint();
	int k=nextint();
	segment_init(&s, 300001, max2, 0);
	for(int i=0; i<n; i++){
		a[i]=nextint();
	}
	for(int i=0; i<n; i++){
		int now = QUERY(s, a[i]-k, a[i]+k+1)+1;
		UPDATE(s, a[i], now);
	}
	int ans = QUERY(s, INT_MIN, INT_MAX);
	printf("%d\n", ans);
}

/* definition */

static inline int get(void){ return getchar_unlocked(); }
static inline void put(int c){ putchar_unlocked(c); }

static inline uint32_t flp2(uint32_t x){ return 0x80000000>>(__builtin_clz(x-1)-1); }

void memcpyex(void *dest, void *src, size_t size, size_t n){
	if(n == 0) { return; }
	if(n == 1) { memcpy(dest, src, size); }
	size_t half = n/2;
	memcpyex(dest, src, size, half);
	memcpy((char*)dest+half*size, dest, half*size);
	if(n%2==1){
		memcpy((char*)dest+(n-1)*size, src, size);
	}
}

int32_t nextint(void){ char c=get(); while(c!='-'&&(c<'0'||'9'<c)) c=get(); bool s=false; if(c=='-'){s=true;c=get();} uint32_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=get(); } return s?-x:x; }
int64_t nextlong(void){ char c=get(); while(c!='-'&&(c<'0'||'9'<c)) c=get(); int s=0; if(c=='-'){s=1;c=get();} uint64_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=get(); } return s?-x:x; }
uint32_t nextstr(char *s){ char c=get(); while(c==' '||c=='\n') c=get(); uint32_t len=0; while(c!=' '&&c!='\n'){ *s++=c; len++; c=get(); } *s='\0'; return len; }
void printint(int32_t x){ if(x<0){x=-x; putchar('-');}else if(x==0){putchar('0');} int d=0; char s[10]; while(x){ s[d]='0'+x%10; x/=10; d++; } for (int i=d-1; i>=0; i--) putchar(s[i]); putchar('\n'); }
void printlong(int64_t x){ if(x<0){x=-x; putchar('-');}else if(x==0){putchar('0');} int d=0; char s[20]; while(x){ s[d]='0'+x%10; x/=10; d++; } for (int i=d-1; i>=0; i--) putchar(s[i]); putchar('\n'); }

void rsort_d32(void *base, const int count, const int size, const int index){
	typedef int32_t rsort_t;
	rsort_t (*a)[size] = (rsort_t(*)[size]) base;
	rsort_t (*b)[size] = (rsort_t(*)[size]) malloc(count*size*sizeof(rsort_t));
	int c[0x100];
	for(int k=0; k<4; k++){
		memset(c, 0, sizeof(c)); for(int i=0; i<count; i++){ c[(a[i][index]>>(k<<3))&0xFF]++; } for(int i=1; i<0x100; i++) c[i]+=c[i-1]; for(int i=count-1; i>=0; i--){ int to=--c[(a[i][index]>>(k<<3))&0xFF]; for(int j=0; j<size; j++){ b[to][j]=a[i][j]; } }{rsort_t(*t)[size]=a; a=b; b=t;}
	}
	free(b);
}
void rsortr_d32(void *base, const int count, const int size, const int index){
	typedef int32_t rsort_t;
	rsort_t (*a)[size] = (rsort_t(*)[size]) base;
	rsort_t (*b)[size] = (rsort_t(*)[size]) malloc(count*size*sizeof(rsort_t));
	int c[0x100];
	for(int k=0; k<4; k++){
		memset(c, 0, sizeof(c)); for(int i=0; i<count; i++){ c[(a[i][index]>>(k<<3))&0xFF]++; } for(int i=0xFE; i>=0; i--) c[i]+=c[i+1]; for(int i=count-1; i>=0; i--){ int to=--c[(a[i][index]>>(k<<3))&0xFF]; for(int j=0; j<size; j++){ b[to][j]=a[i][j]; } }{rsort_t(*t)[size]=a; a=b; b=t;}
	}
	free(b);
}

static int union_find_root(struct union_find *p, int x){ return p->p_prev[x]==x?x:(p->p_prev[x]=union_find_root(p, p->p_prev[x])); }
static int union_find_size(struct union_find *p, int x){ return p->p_size[union_find_root(p, x)]; }
static void union_find_unite(struct union_find *p, int x, int y){
	x=union_find_root(p, x), y=union_find_root(p, y); if(x==y) return;
	if(p->p_rank[x]<p->p_rank[y]){
		p->p_prev[x]=y; p->p_size[y]+=p->p_size[x];
	}else if(p->p_rank[x]>p->p_rank[y]){
		p->p_prev[y]=x; p->p_size[x]+=p->p_size[y];
	}else{
		p->p_prev[x]=y; p->p_rank[y]++; p->p_size[y]+=p->p_size[x];
	}
}
void union_find_init(struct union_find *p, int n){
	p->p_prev = (int*) malloc(n*sizeof(int));
	p->p_rank = (int*) malloc(n*sizeof(int));
	p->p_size = (int*) malloc(n*sizeof(int));
	p->root = union_find_root;
	p->unite = union_find_unite;
	p->size = union_find_size;
	for(int i=0; i<n; i++){ p->p_prev[i]=i; p->p_rank[i]=1; p->p_size[i]=1; }
}

static void segment_int_update(struct segment_int *p, int k, int x){
	k += p->n - 1;
	p->data[k] = x;
	while(k>0){
		k=(k-1)/2;
		p->data[k] = p->op(p->data[k*2+1], p->data[k*2+2]);
	}
}
static int segment_int_query_inner(struct segment_int *p, int a, int b, int k, int l, int r){
	if(r<=a || b<=l) return p->defval;
	if(a<=l && r<=b) return p->data[k];
	int m=(l+r)/2;
	int vl = segment_int_query_inner(p, a, b, k*2+1, l, m);
	int vr = segment_int_query_inner(p, a, b, k*2+2, m, r);
	return p->op(vl, vr);
}
static int segment_int_query(struct segment_int *p, int l, int r){
	if(l<0) l=0;
	if(r>p->n) r=p->n;
	return segment_int_query_inner(p, l, r, 0, 0, p->n);
}
void segment_init(struct segment_int *p, int n, int (*op)(int, int), int defval){
	p->n = flp2(n);
	p->data = (int*) malloc(p->n * sizeof(int));
	for(int i=0; i<p->n; i++){ p->data[i] = defval; }
	p->defval = defval;
	p->op = op;
	p->update = segment_int_update;
	p->query = segment_int_query;
}