fn main() { let v = read_vec::(); let (n, d) = (v[0] as usize, v[1]); let mut a = vec![0; n]; for i in 0..n { a[i] = read::(); } let mut a_sorted = a.clone(); a_sorted.sort(); for anum in a { let ans = a_sorted.upper_bound(&(anum - d)); println!("{}", ans); } } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } use std::cmp::Ordering; pub trait BinarySearch { fn upper_bound(&self, x: &T) -> usize; } impl BinarySearch for [T] { fn upper_bound(&self, x: &T) -> usize { let mut low = 0; let mut high = self.len(); while low != high { let mid = (low + high) / 2; match self[mid].cmp(x) { Ordering::Less | Ordering::Equal => { low = mid + 1; } Ordering::Greater => { high = mid; } } } low } }