結果

問題 No.1505 Zero-Product Ranges
ユーザー Masaki KitaguchiMasaki Kitaguchi
提出日時 2022-01-16 04:23:44
言語 Rust
(1.83.0 + proconio)
結果
TLE  
実行時間 -
コード長 1,187 bytes
コンパイル時間 13,134 ms
コンパイル使用メモリ 380,196 KB
実行使用メモリ 16,256 KB
最終ジャッジ日時 2024-11-22 16:05:15
合計ジャッジ時間 113,617 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 TLE * 30
権限があれば一括ダウンロードができます

ソースコード

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 zeros = a
        .iter()
        .enumerate()
        .filter(|(_, x)| **x == 0)
        .map(|(i, _)| i)
        .collect::<Vec<_>>();

    let mut next_zero = std::collections::HashMap::new();
    {
        zeros.iter().for_each(|&j| {
            (0..=j).for_each(|i| {
                if !next_zero.contains_key(&i) {
                    next_zero.insert(i, j);
                }
            })
        });
    };

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

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