Загрузка данных


import sys
import bisect

def solve():
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    n = int(input_data[0])
    t = int(input_data[1])
    k = [int(x) for x in input_data[2:2+n]]
    
    right_elements = sorted(k)
    
    count_m_for_val = {}
    for x in k:
        if x not in count_m_for_val:
            idx_low = bisect.bisect_left(right_elements, x - t)
            idx_high = bisect.bisect_right(right_elements, x + t)
            count_m_for_val[x] = idx_low + (n - idx_high)
            
    ans = 0
    left_elements = []
    
    for x in k:
        pos = bisect.bisect_right(right_elements, x) - 1
        right_elements.pop(pos)
        
        if x in count_m_for_val:
            idx_low = bisect.bisect_left(right_elements, x - t)
            idx_high = bisect.bisect_right(right_elements, x + t)
            total_m_right = idx_low + (len(right_elements) - idx_high)
        else:
            total_m_right = 0
            
        idx_j_low = bisect.bisect_left(left_elements, x - t)
        idx_j_high = bisect.bisect_right(left_elements, x + t)
        total_j_left = idx_j_high - idx_j_low
        
        ans += total_j_left * total_m_right
        
        bisect.insort(left_elements, x)
        
    print(ans)

if __name__ == '__main__':
    solve()