結果
問題 | No.899 γatheree |
ユーザー | ziita |
提出日時 | 2019-10-10 21:44:46 |
言語 | Rust (1.77.0 + proconio) |
結果 |
RE
|
実行時間 | - |
コード長 | 7,312 bytes |
コンパイル時間 | 16,458 ms |
コンパイル使用メモリ | 386,396 KB |
実行使用メモリ | 22,872 KB |
最終ジャッジ日時 | 2024-11-21 23:43:20 |
合計ジャッジ時間 | 17,826 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | RE | - |
testcase_02 | RE | - |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | RE | - |
testcase_13 | RE | - |
testcase_14 | RE | - |
testcase_15 | RE | - |
testcase_16 | RE | - |
testcase_17 | RE | - |
testcase_18 | RE | - |
testcase_19 | RE | - |
testcase_20 | RE | - |
testcase_21 | RE | - |
testcase_22 | WA | - |
testcase_23 | RE | - |
コンパイルメッセージ
warning: unused variable: `l` --> src/main.rs:94:19 | 94 | (Some(l), Some(r)) => Some(r), | ^ help: if this is intentional, prefix it with an underscore: `_l` | = note: `#[warn(unused_variables)]` on by default warning: variable does not need to be mutable --> src/main.rs:124:13 | 124 | let mut lazy_data = vec![None; size_p2 * 2]; | ----^^^^^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default
ソースコード
#![allow(unused_imports)] #![allow(dead_code)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] use std::cmp::*; use std::collections::*; use std::ops::*; use std::io::{Write, BufWriter}; const MOD: usize = 998244353; // 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),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }}; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } use std::cmp; use std::marker::PhantomData; pub trait Monoid<T> { fn id() -> Option<T> { None } fn op(l: &Option<T>, r: &Option<T>) -> Option<T>; fn lazy_op(l: &Option<T>, r: &Option<T>) -> Option<T>; } pub struct MinOp<T: Ord> { phantom: PhantomData<T>, } impl<T: Ord + Clone + Add<Output=T>> Monoid<T> for MinOp<T> { #[inline] fn op(l: &Option<T>, r: &Option<T>) -> Option<T> { match (l.clone(), r.clone()) { (Some(l), Some(r)) => Some(l+r), (Some(l), None) => Some(l), (None, Some(r)) => Some(r), (None, None) => None, } } fn lazy_op(l: &Option<T>, r: &Option<T>) -> Option<T> { match (l.clone(), r.clone()) { (Some(l), Some(r)) => Some(r), (Some(l), None) => Some(l), (None, Some(r)) => Some(r), (None, None) => None, } } } pub struct SegmentTree<M: Monoid<T>, T: Clone> { phantom: PhantomData<M>, data: Vec<Option<T>>, lazy_data: Vec<Option<T>>, size: usize, size_p2: usize, } impl<M: Monoid<T>, T: Clone> SegmentTree<M, T> { pub fn from_vec(v: Vec<T>) -> SegmentTree<M, T> { let size = v.len(); let mut size_p2 = 1; while size_p2 < v.len() { size_p2 *= 2; } let mut data = vec![None; size_p2 * 2]; for (i, x) in v.into_iter().enumerate() { data[size_p2 + i] = Some(x); } for i in (0..size_p2).rev() { data[i] = M::op(&data[i * 2 + 0], &data[i * 2 + 1]); } let mut lazy_data = vec![None; size_p2 * 2]; SegmentTree { phantom: PhantomData, data: data, lazy_data: lazy_data, size: size, size_p2: size_p2, } } pub fn size(&self) -> usize { self.size } pub fn eval(&mut self, l: usize, r: usize, k: usize){ if self.lazy_data[k].is_some() { self.data[k] = M::lazy_op(&self.data[k], &self.lazy_data[k]); if r-l>1 { self.data[2*k+0] = M::lazy_op(&self.data[2*k+0], &self.lazy_data[k]); self.data[2*k+1] = M::lazy_op(&self.data[2*k+1], &self.lazy_data[k]); } self.lazy_data[k] = None; } } pub fn lazy_update(&mut self, a: usize, b: usize, l: usize, r: usize, k: usize, value: T) { // assert!(l <= r && r <= self.size); self.eval(l,r,k); if b<=l || r<=a {return;} if a<=l && r<=b { self.lazy_data[k] = Some(value); self.eval(l,r,k); } else{ self.lazy_update(a,b,l,(l+r)/2,2*k+0,value.clone()); self.lazy_update(a,b,(l+r)/2,r,2*k+1,value.clone()); self.data[k] = M::op(&self.data[2*k+0], &self.data[2*k+1]); } } pub fn update(&mut self, a: usize, b:usize, value: T){ self.lazy_update(a,b,0,self.size_p2,1,value); } pub fn lazy_query(&mut self, a: usize, b: usize, l: usize, r: usize, k: usize) -> Option<T> { self.eval(l,r,k); if a<=l && r<=b {return self.data[k].clone();} if b<=l || r<=a {return None;} let res1 = self.lazy_query(a, b, l, (l+r)/2, 2*k+0); let res2 = self.lazy_query(a, b, (l+r)/2, r, 2*k+1); M::op(&res1, &res2) } pub fn query(&mut self, l: usize, r: usize) -> Option<T> { self.lazy_query(l,r,0,self.size_p2,1) } } fn solve() { input! { n: usize, uv: [[usize;2];n-1], a: [usize; n], q: usize, x: [usize; q], } let mut edge = vec![vec![]; n]; for i in uv { let s = i[0]; let t = i[1]; edge[s].push(t); edge[t].push(s); } let mut que: VecDeque<[usize;2]> = VecDeque::new(); que.push_back([0,n+1]); let mut seen = vec![false; n]; let mut tour = vec![]; let mut L1 = vec![n+1;n]; let mut R1 = vec![n+1;n]; let mut L2 = vec![n+1;n]; let mut R2 = vec![n+1;n]; let mut P = vec![n+1;n]; while let Some(node) = que.pop_front() { let x = node[0]; let p = node[1]; seen[x] = true; tour.push(x); P[x] = p; for &i in &edge[x] { if seen[i] {continue;} que.push_back([i,x]); if L1[x]==n+1 {L1[x]=i}; R1[x] = i+1; let pp = P[x]; if pp != n+1 { if L2[pp]==n+1 {L2[pp]=i;} R2[pp]=i+1; } } } type RMQ<T> = SegmentTree<MinOp<T>, T>; let mut seg = RMQ::from_vec(tour.iter().map(|&x| a[x]).collect::<Vec<_>>()); for i in x { let mut sum = 0; if L1[i]!=n+1 { sum += seg.query(L1[i],R1[i]).unwrap(); seg.update(L1[i], R1[i], 0); } if L2[i]!=n+1 { sum += seg.query(L2[i],R2[i]).unwrap(); seg.update(L2[i], R2[i], 0); } if P[i]!=n+1 { sum += seg.query(P[i], P[i]+1).unwrap(); seg.update(P[i], P[i]+1, 0); sum += seg.query(L1[P[i]], R1[P[i]]).unwrap(); seg.update(L1[P[i]], R1[P[i]], 0); } if P[i]!=n+1 && P[P[i]]!=n+1 { sum += seg.query(P[P[i]], P[P[i]]+1).unwrap(); seg.update(P[P[i]], P[P[i]]+1, 0); } seg.update(i,i+1,sum); println!("{}", sum); } } 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(); }