// ---------- begin Lowest Common Ancestor ---------- struct LCA { graph: Vec>, path_root: Vec, path_parent: Vec, index: Vec } 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> { size: usize, a: Vec>, fold: F, } #[allow(dead_code)] impl T> PURQ { pub fn new(len: usize, fold: F) -> PURQ { 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, b: &Option) -> Option { 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 = None; let mut q: Option = 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::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($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(); }