結果
問題 | No.1790 Subtree Deletion |
ユーザー | akakimidori |
提出日時 | 2021-12-19 00:07:56 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 92 ms / 3,000 ms |
コード長 | 10,579 bytes |
コンパイル時間 | 15,203 ms |
コンパイル使用メモリ | 377,360 KB |
実行使用メモリ | 24,192 KB |
最終ジャッジ日時 | 2024-09-15 14:19:01 |
合計ジャッジ時間 | 18,059 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 92 ms
24,064 KB |
testcase_04 | AC | 91 ms
24,064 KB |
testcase_05 | AC | 84 ms
24,120 KB |
testcase_06 | AC | 83 ms
24,192 KB |
testcase_07 | AC | 85 ms
24,084 KB |
testcase_08 | AC | 8 ms
5,376 KB |
testcase_09 | AC | 75 ms
23,680 KB |
testcase_10 | AC | 85 ms
23,872 KB |
testcase_11 | AC | 88 ms
23,872 KB |
testcase_12 | AC | 74 ms
20,840 KB |
testcase_13 | AC | 62 ms
19,004 KB |
testcase_14 | AC | 19 ms
7,168 KB |
コンパイルメッセージ
warning: type alias `Map` is never used --> src/main.rs:320:6 | 320 | type Map<K, V> = BTreeMap<K, V>; | ^^^ | = note: `#[warn(dead_code)]` on by default warning: type alias `Deque` is never used --> src/main.rs:322:6 | 322 | type Deque<T> = VecDeque<T>; | ^^^^^
ソースコード
// ---------- begin SegmentTree Point update Range query ---------- mod segment_tree { pub struct PURQ<T, F> { size: usize, data: Vec<T>, e: T, op: F, } #[allow(dead_code)] impl<T, F> PURQ<T, F> where T: Clone, F: Fn(&T, &T) -> T, { pub fn new(size: usize, e: T, op: F) -> PURQ<T, F> { let size = size.next_power_of_two(); PURQ { size, data: vec![e.clone(); 2 * size], e: e, op: op, } } pub fn update(&mut self, x: usize, v: T) { assert!(x < self.size); let mut x = x + self.size; let data = &mut self.data; data[x] = v; x >>= 1; while x > 0 { data[x] = (self.op)(&data[2 * x], &data[2 * x + 1]); x >>= 1; } } pub fn update_tmp(&mut self, x: usize, v: T) { assert!(x < self.size); self.data[x + self.size] = v; } pub fn update_all(&mut self) { let data = &mut self.data; for k in (1..self.size).rev() { data[k] = (self.op)(&data[2 * k], &data[2 * k + 1]); } } pub fn find(&self, l: usize, r: usize) -> T { assert!(l <= r && r <= self.size); if l == r { return self.e.clone(); } let mut p = self.e.clone(); let mut q = self.e.clone(); let mut l = l + self.size; let mut r = r + self.size; let data = &self.data; while l < r { if l & 1 == 1 { p = (self.op)(&p, &data[l]); l += 1; } if r & 1 == 1 { r -= 1; q = (self.op)(&data[r], &q); } l >>= 1; r >>= 1; } (self.op)(&p, &q) } } } // ---------- end SegmentTree Point update Range query ---------- // ---------- begin Heavy-Light decomposition ---------- pub struct HLD { size: usize, edge: Vec<(usize, usize)>, child: Vec<Vec<usize>>, path_root: Vec<usize>, parent: Vec<usize>, left: Vec<usize>, right: Vec<usize>, inverse: Vec<usize>, } impl HLD { pub fn new(size: usize) -> Self { assert!(size <= 10usize.pow(8)); HLD { size: size, edge: Vec::with_capacity(size - 1), child: Vec::new(), path_root: Vec::new(), parent: Vec::new(), left: Vec::new(), right: Vec::new(), inverse: Vec::new(), } } pub fn add_edge(&mut self, a: usize, b: usize) { assert!(a != b && a < self.size && b < self.size); self.edge.push((a, b)); } pub fn build(&mut self, root: usize) { assert!(self.edge.len() + 1 == self.size); let size = self.size; let mut cnt = vec![0; size]; for &(a, b) in self.edge.iter() { cnt[a] += 1; cnt[b] += 1; } let mut child = cnt .into_iter() .map(|c| Vec::with_capacity(c)) .collect::<Vec<_>>(); for &(a, b) in self.edge.iter() { child[a].push(b); child[b].push(a); } let mut parent = vec![size; size]; let mut q = Vec::with_capacity(size); q.push(root); parent[root] = root; for i in 0..size { let v = q[i]; for u in child[v].clone() { assert!(parent[u] == size); parent[u] = v; let k = child[u].iter().position(|e| *e == v).unwrap(); child[u].remove(k); q.push(u); } } let mut sum = vec![1; size]; for &v in q.iter().rev() { let child = &mut child[v]; if !child.is_empty() { let mut max = (0, 0); for (i, &u) in child.iter().enumerate() { sum[v] += sum[u]; max = std::cmp::max(max, (sum[u], i)); } child.swap(0, max.1); } } let mut path_root = (0..size).collect::<Vec<_>>(); let mut left = vec![0; size]; let mut right = vec![0; size]; let mut dfs = vec![(root, false)]; let mut id = 0; while let Some((v, end)) = dfs.pop() { if end { right[v] = id; continue; } left[v] = id; id += 1; dfs.push((v, true)); let child = &child[v]; if !child.is_empty() { for &u in child.iter().skip(1) { path_root[u] = u; dfs.push((u, false)); } let u = child[0]; path_root[u] = path_root[v]; dfs.push((u, false)); } } let mut inverse = vec![size; size]; for (i, l) in left.iter().enumerate() { inverse[*l] = i; } self.child = child; self.parent = parent; self.left = left; self.right = right; self.path_root = path_root; self.inverse = inverse; } pub fn lca(&self, mut a: usize, mut b: usize) -> usize { assert!(a < self.size && b < self.size); let path = &self.path_root; let parent = &self.parent; let index = &self.left; while path[a] != path[b] { if index[a] > index[b] { a = parent[path[a]]; } else { b = parent[path[b]]; } } (index[a], a).min((index[b], b)).1 } pub fn path( &self, src: usize, dst: usize, up: &mut Vec<(usize, usize)>, down: &mut Vec<(usize, usize)>, ) { assert!(src < self.size && dst < self.size); up.clear(); down.clear(); let path = &self.path_root; let parent = &self.parent; let index = &self.left; let mut x = src; let mut y = dst; while path[x] != path[y] { if index[x] > index[y] { let p = path[x]; assert!(p == path[p]); up.push((index[p], index[x] + 1)); x = parent[p]; } else { let p = path[y]; assert!(p == path[p]); down.push((index[p], index[y] + 1)); y = parent[p]; } } if index[x] <= index[y] { down.push((index[x], index[y] + 1)); } else { up.push((index[y], index[x] + 1)); } down.reverse(); } pub fn sub_tree(&self, v: usize) -> (usize, usize) { assert!(v < self.size); (self.left[v], self.right[v]) } pub fn parent(&self, v: usize) -> Option<usize> { assert!(v < self.size); let p = self.parent[v]; if p == v { None } else { Some(p) } } // s -> t へのパスの2番目の頂点を返す pub fn next(&self, s: usize, t: usize) -> usize { assert!(s < self.size && t < self.size && s != t); let (a, b) = self.sub_tree(s); let (c, d) = self.sub_tree(t); if !(a <= c && d <= b) { return self.parent[s]; } let mut pos = t; let mut pre = t; while self.path_root[s] != self.path_root[pos] { pre = self.path_root[pos]; pos = self.parent[pre]; } if s == pos { pre } else { self.child[s][0] } } pub fn vertex(&self, x: usize) -> usize { assert!(x < self.size); self.inverse[x] } } // ---------- end Heavy-Light decomposition ---------- // ---------- begin input macro ---------- // reference: 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 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, bytes) => { read_value!($iter, String).bytes().collect::<Vec<u8>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- use std::io::Write; use std::collections::*; type Map<K, V> = BTreeMap<K, V>; type Set<T> = BTreeSet<T>; type Deque<T> = VecDeque<T>; fn run() { input! { n: usize, e: [(usize1, usize1, u64); n - 1], q: usize, ask: [(u8, usize1); q], } let mut hld = HLD::new(n); for &(a, b, _) in e.iter() { hld.add_edge(a, b); } hld.build(0); let mut seg = segment_tree::PURQ::new(n, 0, |a, b| *a ^ *b); for &(a, b, c) in e.iter() { let a = hld.sub_tree(a).0; let b = hld.sub_tree(b).0; let x = a.max(b); seg.update(x, c); } let mut set = (1..n).collect::<Set<_>>(); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for (op, x) in ask { let (l, r) = hld.sub_tree(x); if op == 1 { let d = set.range(l..r).cloned().collect::<Vec<_>>(); for d in d { seg.update(d, 0); set.remove(&d); } } else { let ans = seg.find(l + 1, r); writeln!(out, "{}", ans).ok(); } } } fn main() { run(); }