結果
問題 | No.1030 だんしんぐぱーりない |
ユーザー | akakimidori |
提出日時 | 2020-04-17 22:07:47 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 92 ms / 2,000 ms |
コード長 | 7,164 bytes |
コンパイル時間 | 11,863 ms |
コンパイル使用メモリ | 395,316 KB |
実行使用メモリ | 23,884 KB |
最終ジャッジ日時 | 2024-10-03 13:12:16 |
合計ジャッジ時間 | 16,592 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 0 ms
6,820 KB |
testcase_01 | AC | 0 ms
6,820 KB |
testcase_02 | AC | 0 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 67 ms
18,820 KB |
testcase_06 | AC | 54 ms
16,828 KB |
testcase_07 | AC | 32 ms
9,988 KB |
testcase_08 | AC | 34 ms
12,024 KB |
testcase_09 | AC | 57 ms
19,020 KB |
testcase_10 | AC | 23 ms
6,816 KB |
testcase_11 | AC | 53 ms
15,236 KB |
testcase_12 | AC | 58 ms
15,744 KB |
testcase_13 | AC | 48 ms
15,240 KB |
testcase_14 | AC | 64 ms
18,088 KB |
testcase_15 | AC | 26 ms
8,668 KB |
testcase_16 | AC | 56 ms
14,204 KB |
testcase_17 | AC | 59 ms
18,188 KB |
testcase_18 | AC | 70 ms
18,936 KB |
testcase_19 | AC | 36 ms
9,100 KB |
testcase_20 | AC | 43 ms
11,412 KB |
testcase_21 | AC | 46 ms
14,688 KB |
testcase_22 | AC | 44 ms
11,996 KB |
testcase_23 | AC | 50 ms
13,144 KB |
testcase_24 | AC | 38 ms
10,924 KB |
testcase_25 | AC | 45 ms
13,764 KB |
testcase_26 | AC | 27 ms
6,912 KB |
testcase_27 | AC | 32 ms
8,064 KB |
testcase_28 | AC | 59 ms
15,768 KB |
testcase_29 | AC | 48 ms
14,428 KB |
testcase_30 | AC | 40 ms
11,688 KB |
testcase_31 | AC | 40 ms
11,668 KB |
testcase_32 | AC | 54 ms
17,232 KB |
testcase_33 | AC | 58 ms
17,648 KB |
testcase_34 | AC | 23 ms
7,632 KB |
testcase_35 | AC | 88 ms
23,756 KB |
testcase_36 | AC | 90 ms
23,752 KB |
testcase_37 | AC | 91 ms
23,884 KB |
testcase_38 | AC | 92 ms
23,636 KB |
testcase_39 | AC | 91 ms
23,748 KB |
testcase_40 | AC | 1 ms
6,820 KB |
testcase_41 | AC | 0 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 stack = vec![0]; while let Some(v) = stack.pop() { for &u in lca.graph[v].iter() { dp[u] = std::cmp::max(dp[u], dp[v]); stack.push(u); } } let mut seg = segment_tree::PURQ::new(k, |a, b| lca.query(*a, *b)); for i in 0..k { seg.update_tmp(i, a[i]); } seg.update_all(); for (t, x, y) in ask { if t == 1 { let x = x - 1; let y = y - 1; seg.update(x, y); } else { let l = x - 1; let r = y; let v = seg.find(l, r); let ans = dp[v]; writeln!(out, "{}", ans).ok(); } } } fn main() { run(); }