結果

問題 No.778 クリスマスツリー
ユーザー cympfhcympfh
提出日時 2018-12-27 14:44:13
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,691 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 168,200 KB
実行使用メモリ 40,840 KB
最終ジャッジ日時 2024-04-09 03:03:21
合計ジャッジ時間 2,328 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 65 ms
40,328 KB
testcase_07 AC 33 ms
18,448 KB
testcase_08 WA -
testcase_09 AC 102 ms
18,700 KB
testcase_10 AC 106 ms
18,876 KB
testcase_11 AC 100 ms
18,832 KB
testcase_12 AC 96 ms
19,724 KB
testcase_13 AC 42 ms
18,816 KB
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque };

#[allow(unused_macros)]
macro_rules! trace {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    };
    ($($args:expr),*) => { trace!(($($args),*)) }
}

#[allow(unused_macros)]
macro_rules! put {
    ($var:expr) => {
        let _ = writeln!(&mut std::io::stdout(), "{}", $var);
    };
    ($var:expr, $($args:expr),*) => {
        let _ = write!(&mut std::io::stdout(), "{} ", $var);
        put!($($args),*);
    };
}

use std::ops::Range;

struct BIT { size: usize, array: Vec<i32> }

#[allow(dead_code)]
impl BIT {
    fn new(n: usize) -> BIT {
        BIT  { size: n, array: vec![0; n + 1] }
    }
    fn add(&mut self, idx: usize, w: i32) {
        let mut x = idx + 1;
        while x <= self.size {
            self.array[x] += w;
            let xi = x as i32;
            x += (xi&-xi) as usize;
        }
    }
    fn sum_up(&self, idx: usize) -> i32 { // [0, idx)
        let mut sum = 0;
        let mut x = idx;
        while x > 0 {
            sum += self.array[x];
            let xi = x as i32;
            x -= (xi&-xi) as usize;
        }
        sum
    }
    fn sum(&self, range: Range<usize>) -> i32 {
        self.sum_up(range.end) - self.sum_up(range.start)
    }
}

fn bfs(u: usize, mut bit: &mut BIT, tree: &Vec<Vec<usize>>) -> i32 {
    let mut a = bit.sum_up(u);
    bit.add(u, 1);
    for &v in tree[u].iter() {
        a += bfs(v, &mut bit, &tree);
    }
    bit.add(u, -1);
    a
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let mut bit = BIT::new(n);
    let mut tree = vec![vec![]; n];
    // let mut rev = vec![None; n];
    for i in 1..n {
        let a: usize = sc.cin();
        tree[a].push(i);
        // rev[i] = Some(a);
    }

    let ans = bfs(0, &mut bit, &tree);
    put!(ans);
}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
}
0