結果

問題 No.1505 Zero-Product Ranges
ユーザー Masaki Kitaguchi
提出日時 2022-01-16 04:33:06
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,132 bytes
コンパイル時間 12,746 ms
コンパイル使用メモリ 379,724 KB
実行使用メモリ 10,536 KB
最終ジャッジ日時 2024-11-22 16:19:24
合計ジャッジ時間 15,277 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

#[macro_export]
macro_rules! setup {
    { mut $input:ident: SplitWhitespace $(,)? } => {
        use std::io::Read;
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).ok();
        let mut $input = buf.split_whitespace();
    };
}

#[macro_export]
macro_rules! parse_next {
    ($str_iter:expr) => {
        $str_iter.next().unwrap().parse().ok().unwrap()
    };
}

fn main() {
    setup! { mut input: SplitWhitespace };

    let n: usize = parse_next!(input);
    let a: Vec<usize> = (0..n).map(|_| parse_next!(input)).collect();

    let mut next_zero = std::collections::HashMap::new();
    {
        let mut zero = None;
        for i in (0..n).rev() {
            if a[i] == 0 {
                zero = Some(i);
            }
            match zero {
                Some(j) => {
                    next_zero.insert(i, j);
                }
                None => {}
            }
        }
    };

    let ans = (|| {
        (0..n).fold(0, |acc, l| match next_zero.get(&l) {
            Some(j) => acc + (n - j),
            None => acc,
        })
    })();

    println!("{}", ans);
}
0