結果
問題 | No.1030 だんしんぐぱーりない |
ユーザー | akakimidori |
提出日時 | 2020-04-18 00:12:16 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 107 ms / 2,000 ms |
コード長 | 7,650 bytes |
コンパイル時間 | 13,396 ms |
コンパイル使用メモリ | 402,196 KB |
実行使用メモリ | 31,400 KB |
最終ジャッジ日時 | 2024-10-03 16:26:00 |
合計ジャッジ時間 | 16,397 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 77 ms
23,028 KB |
testcase_06 | AC | 58 ms
24,072 KB |
testcase_07 | AC | 37 ms
13,416 KB |
testcase_08 | AC | 40 ms
19,488 KB |
testcase_09 | AC | 62 ms
27,144 KB |
testcase_10 | AC | 25 ms
8,300 KB |
testcase_11 | AC | 60 ms
22,324 KB |
testcase_12 | AC | 61 ms
17,412 KB |
testcase_13 | AC | 52 ms
19,656 KB |
testcase_14 | AC | 71 ms
25,424 KB |
testcase_15 | AC | 35 ms
15,592 KB |
testcase_16 | AC | 61 ms
16,380 KB |
testcase_17 | AC | 67 ms
22,676 KB |
testcase_18 | AC | 80 ms
26,628 KB |
testcase_19 | AC | 41 ms
11,024 KB |
testcase_20 | AC | 49 ms
13,080 KB |
testcase_21 | AC | 50 ms
18,372 KB |
testcase_22 | AC | 50 ms
13,272 KB |
testcase_23 | AC | 62 ms
17,120 KB |
testcase_24 | AC | 48 ms
18,068 KB |
testcase_25 | AC | 55 ms
21,272 KB |
testcase_26 | AC | 36 ms
8,576 KB |
testcase_27 | AC | 41 ms
11,480 KB |
testcase_28 | AC | 71 ms
19,384 KB |
testcase_29 | AC | 52 ms
16,476 KB |
testcase_30 | AC | 45 ms
12,588 KB |
testcase_31 | AC | 48 ms
13,888 KB |
testcase_32 | AC | 64 ms
24,480 KB |
testcase_33 | AC | 72 ms
21,796 KB |
testcase_34 | AC | 28 ms
11,100 KB |
testcase_35 | AC | 107 ms
31,344 KB |
testcase_36 | AC | 104 ms
31,268 KB |
testcase_37 | AC | 104 ms
31,348 KB |
testcase_38 | AC | 104 ms
31,400 KB |
testcase_39 | AC | 106 ms
31,388 KB |
testcase_40 | AC | 1 ms
6,816 KB |
testcase_41 | AC | 1 ms
6,816 KB |
ソースコード
// ---------- begin Lowest Common Ancestor ---------- struct LCA { graph: Vec<Vec<usize>>, path_root: Vec<usize>, path_parent: Vec<usize>, index: Vec<usize> } impl LCA { fn new(n: usize) -> Self { LCA { graph: vec![vec![]; n], path_root: vec![], path_parent: vec![], index: vec![], } } fn add_edge(&mut self, a: usize, b: usize) { self.graph[a].push(b); self.graph[b].push(a); } fn build(&mut self, root: usize) { let mut q = vec![]; let mut stack = vec![(root, root)]; let graph = &mut self.graph; while let Some((v, p)) = stack.pop() { q.push(v); if let Some(k) = graph[v].iter().position(|u| *u == p) { graph[v].swap_remove(k); } for &u in graph[v].iter() { stack.push((u, v)); } } let n = graph.len(); let mut size = vec![1; n]; for &v in q.iter().rev() { if graph[v].is_empty() { continue; } let mut max = (0, 0); for (i, &u) in graph[v].iter().enumerate() { size[v] += size[u]; if size[u] > max.0 { max = (size[u], i); } } graph[v].swap(0, max.1); } let mut path_root = vec![root; n]; let mut path_parent = vec![root; n]; let mut index = vec![n; n]; let mut stack = vec![root]; let mut k = 0; while let Some(v) = stack.pop() { index[v] = k; k += 1; if graph[v].is_empty() { continue; } for &u in graph[v].iter().skip(1) { path_root[u] = u; path_parent[u] = v; stack.push(u); } let u = graph[v][0]; path_root[u] = path_root[v]; path_parent[u] = path_parent[v]; stack.push(u); } self.path_root = path_root; self.path_parent = path_parent; self.index = index; } fn query(&self, mut a: usize, mut b: usize) -> usize { let path = &self.path_root; let parent = &self.path_parent; let index = &self.index; while path[a] != path[b] { if index[a] < index[b] { b = parent[b]; } else { a = parent[a]; } } if index[a] < index[b] {a} else {b} } } // ---------- end Lowest Common Ancestor ---------- // ---------- begin SegmentTree Point update Range query ---------- mod segment_tree { pub struct PURQ<T: Clone, F: Fn(&T, &T) -> T> { size: usize, a: Vec<Option<T>>, fold: F, } #[allow(dead_code)] impl<T: Clone, F: Fn(&T, &T) -> T> PURQ<T, F> { pub fn new(len: usize, fold: F) -> PURQ<T, F> { let size = len.next_power_of_two(); PURQ { size: size, a: vec![None; 2 * size], fold: fold, } } pub fn update(&mut self, x: usize, v: T) { let mut k = self.size + x; self.a[k] = Some(v); k >>= 1; while k > 0 { self.a[k] = self.merge(&self.a[2 * k], &self.a[2 * k + 1]); k >>= 1; } } fn merge(&self, a: &Option<T>, b: &Option<T>) -> Option<T> { match (a.as_ref(), b.as_ref()) { (Some(a), Some(b)) => Some((self.fold)(a, b)), (Some(a), None) => Some(a.clone()), (None, Some(b)) => Some(b.clone()), (None, None) => None, } } pub fn update_tmp(&mut self, x: usize, v: T) { self.a[x + self.size] = Some(v); } pub fn update_all(&mut self) { for k in (1..(self.size)).rev() { self.a[k] = self.merge(&self.a[2 * k], &self.a[2 * k + 1]); } } pub fn find(&self, mut l: usize, mut r: usize) -> T { let mut p: Option<T> = None; let mut q: Option<T> = None; let a = &self.a; l += self.size; r += self.size; while l < r { if (l & 1) == 1 { p = self.merge(&p, &a[l]); l += 1; } if (r & 1) == 1 { r -= 1; q = self.merge(&a[r], &q); } l >>= 1; r >>= 1; } self.merge(&p, &q).unwrap() } } } // ---------- end SegmentTree Point update Range query ---------- //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, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // use std::io::Write; fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); input! { n: usize, k: usize, q: usize, c: [u32; n], a: [usize1; k], e: [(usize1, usize1); n - 1], ask: [(usize, usize, usize); q], } let mut lca = LCA::new(n); for (a, b) in e { lca.add_edge(a, b); } lca.build(0); let mut dp = c.clone(); let mut id = 0; let mut left = vec![0; n]; let mut right = vec![0; n]; let mut stack = vec![(0, 0)]; while let Some((op, v)) = stack.pop() { if op == 1 { right[v] = id; continue; } stack.push((1, v)); left[v] = id; id += 1; for &u in lca.graph[v].iter() { dp[u] = std::cmp::max(dp[u], dp[v]); stack.push((0, u)); } } type T = ((usize, usize), (usize, usize)); let mut seg = segment_tree::PURQ::new(k, |a: &T, b: &T| { let x = std::cmp::min(a.0, b.0); let y = std::cmp::max(a.1, b.1); (x, y) }); for i in 0..k { let v = a[i]; seg.update_tmp(i, ((left[v], v), (right[v], v))); } seg.update_all(); for (t, x, y) in ask { if t == 1 { let x = x - 1; let y = y - 1; seg.update(x, ((left[y], y), (right[y], y))); } else { let l = x - 1; let r = y; let (l, r) = seg.find(l, r); let ans = dp[lca.query(l.1, r.1)]; writeln!(out, "{}", ans).ok(); } } } fn main() { run(); }