Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]
G = {i:[0,[]] for i in range(N)}
for i in range(N):
    for j in range(i+1,N):
        if 0<=A[j]-A[i]<=K:
            G[i][1].append(j)
            G[j][0] += 1
            break
for i in range(N):
    for j in range(i+1,N):
        if 0<A[i]-A[j]<=K:
            G[i][1].append(j)
            G[j][0] += 1
            break
S = []
for i in range(N):
    if G[i][0]==0:
        S.append(i)
dist = [-1 for _ in range(N)]
for i in S:
    que = deque([(i,0)])
    dist[i] = 0
    while que:
        x,d = que.popleft()
        for y in G[x][1]:
            if dist[y]<d+1:
                dist[y]=d+1
                que.append((y,d+1))
dmax = max(dist)
print(dmax+1)

Fixed submission:
from collections import deque
INFTY = 10**10
def update(i,a):
    j = i+N
    T[j] = a
    j = j//2
    while j>=1:
        T[j]=max(T[2*j],T[2*j+1])
        j = j//2
def find(l,r):
    que = deque([(1,0,N)])
    A = []
    while que:
        j,x,D = que.popleft()
        y = x+D
        if y<=l or x>=r:
            A.append(-INFTY)
        elif l<=x and y<=r:
            A.append(T[j])
        else:
            que.append((2*j,x,D//2))
            que.append((2*j+1,x+D//2,D//2))
    return max(A)
n,K = map(int,input().split())
A = [int(input()) for _ in range(n)]
k = 0
while 2**k<3*10**5:
    k += 1
N = 2**k
T = [-INFTY for _ in range(2*N)]
dp = [0 for _ in range(n)]
dp[0]=1
update(A[0],dp[0])
for i in range(1,n):
    a = A[i]
    l = max(0,a-K)
    r = min(a+K,N-1)+1
    dp[i] = max(1,find(l,r)+1)
    update(a,dp[i])
dmax = 0
for i in range(n):
    dmax = max(dmax,dp[i])
print(dmax)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]
G = {i:[0,[-1,-1]] for i in range(N)}
for i in range(N):
    for j in range(i+1,N):
        if A[j]>=A[i] and A[j]-A[i]<=K and G[i][1][1]<0:
            G[i][1][1]=j
            G[j][0] += 1
        if A[j]<A[i] and A[i]-A[j]<=K and G[i][1][0]<0:
            G[i][1][0]=j
            G[j][0] += 1
        if G[i][1][0]>=0 and G[i][1][1]>=0:break
S = []
for i in range(N):
    if G[i][0]==0:
        S.append(i)
dist = [-1 for _ in range(N)]
for i in S:
    que = deque([(i,0)])
    dist[i] = 0
    while que:
        x,d = que.popleft()
        y = G[x][1][0]
        if y>=0 and dist[y]<d+1:
            dist[y]=d+1
            que.append((y,d+1))
        y = G[x][1][1]
        if y>=0 and dist[y]<d+1:
            dist[y]=d+1
            que.append((y,d+1))
dmax = max(dist)
print(dmax+1)

Fixed submission:
from collections import deque
INFTY = 10**10
def update(i,a):
    j = i+N
    T[j] = a
    j = j//2
    while j>=1:
        T[j]=max(T[2*j],T[2*j+1])
        j = j//2
def find(l,r):
    que = deque([(1,0,N)])
    A = []
    while que:
        j,x,D = que.popleft()
        y = x+D
        if y<=l or x>=r:
            A.append(-INFTY)
        elif l<=x and y<=r:
            A.append(T[j])
        else:
            que.append((2*j,x,D//2))
            que.append((2*j+1,x+D//2,D//2))
    return max(A)
n,K = map(int,input().split())
A = [int(input()) for _ in range(n)]
k = 0
while 2**k<3*10**5:
    k += 1
N = 2**k
T = [-INFTY for _ in range(2*N)]
dp = [0 for _ in range(n)]
dp[0]=1
update(A[0],dp[0])
for i in range(1,n):
    a = A[i]
    l = max(0,a-K)
    r = min(a+K,N-1)+1
    dp[i] = max(1,find(l,r)+1)
    update(a,dp[i])
dmax = 0
for i in range(n):
    dmax = max(dmax,dp[i])
print(dmax)