Buggy python submission (verdict: Time Limit Exceeded):
# Input Part
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]

Amax = max(A)
# 初期化
n = 1
while n < Amax:
    n *= 2
#print(n)
# セグメント木を持つグローバル配列
# すべての値をINT_MAXに 
INF = 0
dat = [INF] * (2*n-1)

#k番目の値(0-indexed)をaに変更
def update(k,a):
    # 葉の節点
    k += n - 1
    dat[k] = a
    # 登りながら更新
    while k > 0:
        k = int((k - 1) / 2)
        dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2])

# [a,b]の最小値を求める
# 後ろのほうの引数は、計算の簡単のための引数。
# kは節点の番号、1, rはその節点が[1, r)に対応づいていることを表す。
# したがって、外からはquery(a, b, 0, 0, n)として呼ぶ。
def query(a,b,k,l,r):
    # [a,b]と[1, r)が交差しなければ、INT_MAX
    if a==b:
        return dat[n+a-1]
    
    if r <= a or b <= l:
        return INF
    # [a, b]が[1, r]を完全に含んでいれば、この節点の値 
    if a <= l and r <= b:
        return dat[k]
    # そうでなければ、2つの子の最小値
    else:
        vl = query(a,b,k * 2 + 1,l, int((l + r) / 2))
        vr = query(a,b, k * 2 + 2, int((l + r) / 2), r)
    
    return max(vl,vr)

def main():
    for i in range(N):
        tmin = max(0,A[i]-K)
        tmax = min(Amax,A[i]+K)+1
        update(A[i],query(tmin,tmax,0,0,n)+1)
        #print(dat)
    print(dat[0])
main()


Fixed submission:
# Input Part
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]

Amax = 3*10**5

# N: 処理する区間の長さ
N0 = 2**(Amax-1).bit_length()
INF = 0
data = [INF]*(2*N0)
# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])
# 区間[l, r)の最小値
def query(l, r):
    L = l + N0; R = r + N0
    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

def main():
    for i in range(N):
        tmin = max(0,A[i]-K)
        tmax = min(Amax,A[i]+K)+1
        update(A[i],query(tmin,tmax)+1)
        #print(data)
    print(data[0])
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N,K = map(int,input().split())
    A = [int(input()) for _ in range(N)]
    dp2 = [0] * N
    max0 = 0
    for i in range(N):
        tmax = 0
        for j in range(i+1):
            if -K <= (A[i]-A[j]) <= K:
                tmax = max(tmax,dp2[j]+1)
        dp2[i] = tmax  
        max0 = max(max0,tmax)
    print(max0)
main()

Fixed submission:
# Input Part
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]

Amax = 3*10**5

# N: 処理する区間の長さ
N0 = 2**(Amax-1).bit_length()
INF = 0
data = [INF]*(2*N0)
# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])
# 区間[l, r)の最小値
def query(l, r):
    L = l + N0; R = r + N0
    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

def main():
    for i in range(N):
        tmin = max(0,A[i]-K)
        tmax = min(Amax,A[i]+K)+1
        update(A[i],query(tmin,tmax)+1)
        #print(data)
    print(data[0])
main()

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]
dp2 = [0] * N
for i in range(N):
    tmax = 0
    for j in range(i+1):
        if abs(A[i]-A[j]) <= K:
            tmax = max(tmax,dp2[j]+1)
    dp2[i] = tmax  

print(max(dp2))

Fixed submission:
# Input Part
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]

Amax = 3*10**5

# N: 処理する区間の長さ
N0 = 2**(Amax-1).bit_length()
INF = 0
data = [INF]*(2*N0)
# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])
# 区間[l, r)の最小値
def query(l, r):
    L = l + N0; R = r + N0
    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

def main():
    for i in range(N):
        tmin = max(0,A[i]-K)
        tmax = min(Amax,A[i]+K)+1
        update(A[i],query(tmin,tmax)+1)
        #print(data)
    print(data[0])
main()

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]
dp = [[0] * N for _ in range(N)]
dp2 = [0] * N
for i in range(N):
    for j in range(i,N):
        if abs(A[i]-A[j]) <= K:
            dp[i][j] = 1
for i in range(N):
    tmax = 1
    for j in range(i+1):
        if dp[j][i]==1:
            tmax = max(tmax,dp2[j]+1)
    dp2[i] = tmax  

print(max(dp2))



Fixed submission:
# Input Part
N,K = map(int,input().split())
A = [int(input()) for _ in range(N)]

Amax = 3*10**5

# N: 処理する区間の長さ
N0 = 2**(Amax-1).bit_length()
INF = 0
data = [INF]*(2*N0)
# a_k の値を x に更新
def update(k, x):
    k += N0-1
    data[k] = x
    while k >= 0:
        k = (k - 1) // 2
        data[k] = max(data[2*k+1], data[2*k+2])
# 区間[l, r)の最小値
def query(l, r):
    L = l + N0; R = r + N0
    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])

        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

def main():
    for i in range(N):
        tmin = max(0,A[i]-K)
        tmax = min(Amax,A[i]+K)+1
        update(A[i],query(tmin,tmax)+1)
        #print(data)
    print(data[0])
main()