|
| 1 | +// verification-helper: PROBLEM https://judge.yosupo.jp/problem/vertex_set_path_composite |
| 2 | + |
| 3 | +use proconio::input; |
| 4 | +use programming_team_code_rust::data_structures::seg_tree::SegTree; |
| 5 | +use programming_team_code_rust::graphs::hld::HLD; |
| 6 | + |
| 7 | +fn main() { |
| 8 | + input! { |
| 9 | + n: usize, |
| 10 | + q: usize, |
| 11 | + } |
| 12 | + |
| 13 | + let a = (0..n) |
| 14 | + .map(|_| { |
| 15 | + input! { |
| 16 | + c: usize, |
| 17 | + d: usize |
| 18 | + } |
| 19 | + (c, d) |
| 20 | + }) |
| 21 | + .collect::<Vec<(usize, usize)>>(); |
| 22 | + |
| 23 | + input! { |
| 24 | + edges: [(usize, usize); n - 1], |
| 25 | + } |
| 26 | + |
| 27 | + let mut adj = vec![vec![]; n]; |
| 28 | + for &(u, v) in edges.iter() { |
| 29 | + adj[u].push(v); |
| 30 | + adj[v].push(u); |
| 31 | + } |
| 32 | + |
| 33 | + let hld = HLD::new(&mut adj, false); |
| 34 | + |
| 35 | + let mut input_a = vec![(0, 0); n]; |
| 36 | + for (i, &elem) in a.iter().enumerate() { |
| 37 | + input_a[hld.tin[i]] = elem; |
| 38 | + } |
| 39 | + |
| 40 | + let mut st_forwards = SegTree::<(usize, usize)>::build_on_array( |
| 41 | + &input_a, |
| 42 | + move |x, y| (x.0 * y.0 % 998244353, (y.0 * x.1 + y.1) % 998244353), |
| 43 | + (1, 0), |
| 44 | + ); |
| 45 | + let mut st_backwards = SegTree::<(usize, usize)>::build_on_array( |
| 46 | + &input_a, |
| 47 | + move |x, y| (x.0 * y.0 % 998244353, (x.0 * y.1 + x.1) % 998244353), |
| 48 | + (1, 0), |
| 49 | + ); |
| 50 | + |
| 51 | + for _ in 0..q { |
| 52 | + input! { |
| 53 | + t: usize |
| 54 | + } |
| 55 | + |
| 56 | + match t { |
| 57 | + 0 => { |
| 58 | + input! { |
| 59 | + u: usize, |
| 60 | + c: usize, |
| 61 | + d: usize |
| 62 | + } |
| 63 | + st_forwards.set(hld.tin[u], (c, d)); |
| 64 | + st_backwards.set(hld.tin[u], (c, d)); |
| 65 | + } |
| 66 | + _ => { |
| 67 | + input! { |
| 68 | + u: usize, |
| 69 | + v: usize, |
| 70 | + x: usize |
| 71 | + } |
| 72 | + let (mut u_anc_val, mut v_anc_val) = (st_forwards.unit, st_backwards.unit); |
| 73 | + hld.path(u, v, |range, u_anc| { |
| 74 | + if u_anc { |
| 75 | + u_anc_val = (st_forwards.op)(&u_anc_val, &st_backwards.query(range)); |
| 76 | + } else { |
| 77 | + v_anc_val = (st_forwards.op)(&st_forwards.query(range), &v_anc_val); |
| 78 | + } |
| 79 | + }); |
| 80 | + let res = (st_forwards.op)(&u_anc_val, &v_anc_val); |
| 81 | + println!("{}", (res.0 * x + res.1) % 998244353); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments