結果
問題 | No.778 クリスマスツリー |
ユーザー | fine |
提出日時 | 2020-04-25 22:14:30 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 134 ms / 2,000 ms |
コード長 | 4,514 bytes |
コンパイル時間 | 14,140 ms |
コンパイル使用メモリ | 378,584 KB |
実行使用メモリ | 34,432 KB |
最終ジャッジ日時 | 2024-11-07 23:14:08 |
合計ジャッジ時間 | 16,239 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 1 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 76 ms
34,304 KB |
testcase_07 | AC | 49 ms
13,184 KB |
testcase_08 | AC | 134 ms
23,424 KB |
testcase_09 | AC | 121 ms
17,408 KB |
testcase_10 | AC | 124 ms
17,408 KB |
testcase_11 | AC | 113 ms
17,536 KB |
testcase_12 | AC | 119 ms
17,280 KB |
testcase_13 | AC | 64 ms
17,152 KB |
testcase_14 | AC | 77 ms
34,432 KB |
コンパイルメッセージ
warning: variable does not need to be mutable --> src/main.rs:8:13 | 8 | let mut s = { | ----^ | | | help: remove this `mut` ... 179 | / input! { 180 | | n: usize, 181 | | a: [usize; n - 1], 182 | | } | |_____- in this macro invocation | = note: `#[warn(unused_mut)]` on by default = note: this warning originates in the macro `input` (in Nightly builds, run with -Z macro-backtrace for more info)
ソースコード
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let mut s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } use std::convert::From; pub trait Monoid { type T: Clone + PartialEq; const UNIT: Self::T; fn merge(x: &Self::T, y: &Self::T) -> Self::T; fn apply(target: &Self::T, x: Self::T) -> Self::T; } #[derive(Debug, PartialEq)] pub struct SegmentTree<M: Monoid> { n: usize, data: Vec<M::T>, } impl<M: Monoid> SegmentTree<M> { pub fn new(size: usize) -> Self { Self::with_initial_value(size, M::UNIT) } pub fn with_initial_value(size: usize, initial_value: M::T) -> Self { let mut n : usize = 1; while n < size { n <<= 1; } let mut data = vec![initial_value.clone(); 2 * n - 1]; if initial_value != M::UNIT { for i in (0..(n-1)).rev() { data[i] = M::merge(&data[i * 2 + 1], &data[i * 2 + 2]); } } SegmentTree { n, data } } } impl<M: Monoid> From<&[M::T]> for SegmentTree<M> { fn from(v: &[M::T]) -> Self { let mut st = SegmentTree::new(v.len()); for i in 0..v.len() { st.data[i + st.n - 1] = v[i].clone(); } for i in (0..(st.n-1)).rev() { st.data[i] = M::merge(&st.data[i * 2 + 1], &st.data[i * 2 + 2]); } st } } impl<M: Monoid> SegmentTree<M> { pub fn update(&mut self, idx: usize, val: M::T) { let mut idx = idx + self.n - 1; self.data[idx] = M::apply(&self.data[idx], val); while idx > 0 { idx = (idx - 1) / 2; self.data[idx] = M::merge(&self.data[idx * 2 + 1], &self.data[idx * 2 + 2]); } } pub fn query(&self, begin: usize, end: usize) -> M::T { self._query(begin, end, 0, 0, self.n) } fn _query(&self, begin: usize, end: usize, k: usize, l: usize, r: usize) -> M::T { if r <= begin || end <= l { return M::UNIT; } if begin <= l && r <= end { return self.data[k].clone(); } let vl = self._query(begin, end, k * 2 + 1, l, (l + r) / 2); let vr = self._query(begin, end, k * 2 + 2, (l + r) / 2, r); M::merge(&vl, &vr) } } #[derive(Debug, PartialEq)] struct RangeMin; impl Monoid for RangeMin { type T = i64; const UNIT : Self::T = std::i64::MAX; fn merge(x: &Self::T, y: &Self::T) -> Self::T { x + y } fn apply(vm: &Self::T, vo: Self::T) -> Self::T { vm + vo } } #[derive(Debug, PartialEq)] struct RangeSum; impl Monoid for RangeSum { type T = i64; const UNIT : Self::T = 0; fn merge(x: &Self::T, y: &Self::T) -> Self::T { x + y } fn apply(vm: &Self::T, vo: Self::T) -> Self::T { vm + vo } } fn solve(cur: usize, g: &Vec<Vec<usize>>, ans: &mut i64, st: &mut SegmentTree<RangeSum>) { *ans += st.query(0, cur + 1); st.update(cur, 1); for &nex in g[cur].iter() { solve(nex, g, ans, st); } st.update(cur, -1); } fn main() { input! { n: usize, a: [usize; n - 1], } let mut g : Vec<Vec<usize>> = vec![vec![]; n]; for (i, &j) in a.iter().enumerate() { g[j].push(i + 1); } let mut ans : i64 = 0; let mut st = SegmentTree::<RangeSum>::new(n); solve(0, &g, &mut ans, &mut st); println!("{}", ans); }