#![allow(non_snake_case)] #![allow(dead_code, unused_macros)] use std::collections::HashMap; use std::hash::Hash; #[allow(unused_imports)] use proconio::{input, marker::Usize1, marker::Chars}; #[allow(unused_imports)] use itertools::Itertools; macro_rules! d { ( $( $x:expr ),* $(,)? ) => { println!( concat!( $( stringify!($x), "={:?} " ),* ), $( $x ),* ); }; } fn frequencies(xs: I) -> HashMap where I: IntoIterator, T: Eq + Hash, { let mut d = HashMap::new(); for x in xs { *d.entry(x).or_insert(0) += 1; } d } fn yn(b: bool) -> &'static str { if b { "Yes" } else { "No" } } fn main() { input! { N: usize, D: i64, A: [i64; N], } let mut ld: HashMap = HashMap::new(); let mut rd: HashMap = frequencies(A.iter().cloned()); let mut ans = 0; for &a in &A { let lcnt = *ld.get(&(a-D)).unwrap_or(&0); let rcnt = *rd.get(&(a+D)).unwrap_or(&0); ans += lcnt * rcnt; *ld.entry(a).or_insert(0) += 1; *rd.entry(a).or_insert(0) -= 1; } println!("{ans}"); }