結果

問題 No.778 クリスマスツリー
ユーザー koba-e964koba-e964
提出日時 2021-12-10 11:59:37
言語 Rust
(1.77.0)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 3,129 bytes
コンパイル時間 6,994 ms
コンパイル使用メモリ 159,732 KB
実行使用メモリ 34,036 KB
最終ジャッジ日時 2023-09-25 07:01:31
合計ジャッジ時間 4,085 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 70 ms
33,928 KB
testcase_07 AC 30 ms
10,536 KB
testcase_08 AC 120 ms
22,384 KB
testcase_09 AC 107 ms
13,700 KB
testcase_10 AC 102 ms
13,764 KB
testcase_11 AC 107 ms
13,712 KB
testcase_12 AC 100 ms
13,760 KB
testcase_13 AC 52 ms
13,528 KB
testcase_14 AC 67 ms
34,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

/// Binary Indexed Tree (Fenwick Tree). Holds an array of type T.
/// T is a commutative monoid. Indices are 1 .. n.
/// Verified by yukicoder No.404 (http://yukicoder.me/submissions/155373)
struct BIT<T> {
    n: usize,
    ary: Vec<T>,
    e: T,
}

impl<T: Clone + std::ops::AddAssign<T>> BIT<T> {
    fn new(n: usize, e: T) -> Self {
        let n = n.next_power_of_two();
        BIT { n: n, ary: vec![e.clone(); n + 1], e: e }
    }
    /**
     * gets the sum in [1 .. idx]
     * @param idx
     * @return sum
     */
    fn accum(&self, mut idx: usize) -> T {
        let mut sum = self.e.clone();
        while idx > 0 {
            sum += self.ary[idx].clone();
            idx &= idx - 1;
        }
        sum
    }
    /**
     * performs data[idx] += val;
     */
    fn add<U: Clone>(&mut self, mut idx: usize, val: U)
        where T: std::ops::AddAssign<U> {
        assert!(idx > 0);
        let n = self.n;
        while idx <= n {
            self.ary[idx] += val.clone();
            idx += idx & idx.wrapping_neg();
        }
    }
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn dfs(v: usize, ch: &[Vec<usize>], bit: &mut BIT<i64>) -> i64 {
    let mut ans = bit.accum(v);
    bit.add(v + 1, 1);
    for &w in &ch[v] {
        ans += dfs(w, ch, bit);
    }
    bit.add(v + 1, -1);
    ans
}

// https://yukicoder.me/problems/no/778 (2.5)
// セグメント木や BIT を持ちながら DFS。セグメント木や BIT は先祖の頻度を管理することにする。
fn solve() {
    input! {
        n: usize,
        a: [usize; n - 1],
    }
    let mut ch = vec![vec![]; n];
    for i in 0..n - 1 {
        ch[a[i]].push(i + 1);
    }
    let mut bit = BIT::new(n, 0);
    println!("{}", dfs(0, &ch, &mut bit));
}
0