結果

問題 No.3456 Common Difference is D
コンテスト
ユーザー norioc
提出日時 2026-02-28 14:38:59
言語 Rust
(1.93.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,180 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,587 ms
コンパイル使用メモリ 210,892 KB
実行使用メモリ 10,464 KB
最終ジャッジ日時 2026-02-28 14:39:22
合計ジャッジ時間 3,517 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#![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<I, T>(xs: I) -> HashMap<T, i64>
where
    I: IntoIterator<Item = T>,
    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<i64, i64> = HashMap::new();
    let mut rd: HashMap<i64, i64> = 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}");
}
0