pub struct HeavyLightDecomposition {
pub par: Vec<usize>,
pub vidx: Vec<usize>,
/* private fields */
}
Fields§
§par: Vec<usize>
§vidx: Vec<usize>
Implementations§
Source§impl HeavyLightDecomposition
impl HeavyLightDecomposition
Sourcepub fn new(root: usize, graph: &mut UndirectedSparseGraph) -> Self
pub fn new(root: usize, graph: &mut UndirectedSparseGraph) -> Self
Examples found in repository?
More examples
crates/library_checker/src/datastructure/vertex_add_path_sum.rs (line 13)
9pub fn vertex_add_path_sum(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64; n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
13 let hld = HeavyLightDecomposition::new(0, &mut graph);
14 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
15 for (i, a) in a.iter().cloned().enumerate() {
16 bit.update(hld.vidx[i], a);
17 }
18 for _ in 0..q {
19 match scanner.scan::<usize>() {
20 0 => {
21 scan!(scanner, p, x: i64);
22 bit.update(hld.vidx[p], x);
23 }
24 1 => {
25 scan!(scanner, u, v);
26 writeln!(
27 writer,
28 "{}",
29 hld.query::<AdditiveOperation<_>, _>(u, v, false, |l, r| bit.fold(l, r))
30 )
31 .ok();
32 }
33 _ => panic!("unknown query"),
34 }
35 }
36}
crates/aizu_online_judge/src/grl/grl_5_e.rs (line 22)
12pub fn grl_5_e(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n, c: [SizedCollect<usize>]);
16 let edges = c
17 .take(n)
18 .enumerate()
19 .flat_map(|(u, it)| it.into_iter().map(move |v| (u, v)))
20 .collect();
21 let mut graph = UndirectedSparseGraph::from_edges(n, edges);
22 let hld = HeavyLightDecomposition::new(0, &mut graph);
23 type M = (AdditiveOperation<u64>, AdditiveOperation<u64>);
24 let mut seg = LazySegmentTree::<RangeSumRangeAdd<_>>::from_vec(vec![(0u64, 1u64); n]);
25
26 scan!(scanner, q);
27 for _ in 0..q {
28 match scanner.scan::<usize>() {
29 0 => {
30 scan!(scanner, v, w: u64);
31 hld.update(0, v, true, |l, r| seg.update(l..r, w));
32 }
33 1 => {
34 scan!(scanner, u);
35 let ans = hld.query::<M, _>(0, u, true, |l, r| seg.fold(l..r)).0;
36 writeln!(writer, "{}", ans).ok();
37 }
38 _ => panic!("unknown query"),
39 }
40 }
41}
crates/library_checker/src/datastructure/vertex_set_path_composite.rs (line 16)
12pub fn vertex_set_path_composite(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n, q, ab: [(MInt998244353, MInt998244353); n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
16 let hld = HeavyLightDecomposition::new(0, &mut graph);
17 let mut nab = vec![(MInt998244353::default(), MInt998244353::default()); n];
18 for i in 0..n {
19 nab[hld.vidx[i]] = ab[i];
20 }
21 let mut seg1 = SegmentTree::<LinearOperation<_>>::from_vec(nab.clone());
22 let mut seg2 = SegmentTree::<ReverseOperation<LinearOperation<_>>>::from_vec(nab);
23 for _ in 0..q {
24 match scanner.scan::<usize>() {
25 0 => {
26 scan!(scanner, p, cd: (MInt998244353, MInt998244353));
27 seg1.set(hld.vidx[p], cd);
28 seg2.set(hld.vidx[p], cd);
29 }
30 1 => {
31 scan!(scanner, u, v, x: MInt998244353);
32 let (a, b) = hld.query_noncom::<LinearOperation<_>, _, _>(
33 u,
34 v,
35 false,
36 |l, r| seg1.fold(l..r),
37 |l, r| seg2.fold(l..r),
38 );
39 writeln!(writer, "{}", a * x + b).ok();
40 }
41 _ => panic!("unknown query"),
42 }
43 }
44}
Sourcepub fn update<F: FnMut(usize, usize)>(
&self,
u: usize,
v: usize,
is_edge: bool,
f: F,
)
pub fn update<F: FnMut(usize, usize)>( &self, u: usize, v: usize, is_edge: bool, f: F, )
Examples found in repository?
crates/aizu_online_judge/src/grl/grl_5_e.rs (line 31)
12pub fn grl_5_e(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n, c: [SizedCollect<usize>]);
16 let edges = c
17 .take(n)
18 .enumerate()
19 .flat_map(|(u, it)| it.into_iter().map(move |v| (u, v)))
20 .collect();
21 let mut graph = UndirectedSparseGraph::from_edges(n, edges);
22 let hld = HeavyLightDecomposition::new(0, &mut graph);
23 type M = (AdditiveOperation<u64>, AdditiveOperation<u64>);
24 let mut seg = LazySegmentTree::<RangeSumRangeAdd<_>>::from_vec(vec![(0u64, 1u64); n]);
25
26 scan!(scanner, q);
27 for _ in 0..q {
28 match scanner.scan::<usize>() {
29 0 => {
30 scan!(scanner, v, w: u64);
31 hld.update(0, v, true, |l, r| seg.update(l..r, w));
32 }
33 1 => {
34 scan!(scanner, u);
35 let ans = hld.query::<M, _>(0, u, true, |l, r| seg.fold(l..r)).0;
36 writeln!(writer, "{}", ans).ok();
37 }
38 _ => panic!("unknown query"),
39 }
40 }
41}
Sourcepub fn query<M: Monoid, F: FnMut(usize, usize) -> M::T>(
&self,
u: usize,
v: usize,
is_edge: bool,
f: F,
) -> M::T
pub fn query<M: Monoid, F: FnMut(usize, usize) -> M::T>( &self, u: usize, v: usize, is_edge: bool, f: F, ) -> M::T
Examples found in repository?
crates/library_checker/src/datastructure/vertex_add_path_sum.rs (line 29)
9pub fn vertex_add_path_sum(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64; n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
13 let hld = HeavyLightDecomposition::new(0, &mut graph);
14 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
15 for (i, a) in a.iter().cloned().enumerate() {
16 bit.update(hld.vidx[i], a);
17 }
18 for _ in 0..q {
19 match scanner.scan::<usize>() {
20 0 => {
21 scan!(scanner, p, x: i64);
22 bit.update(hld.vidx[p], x);
23 }
24 1 => {
25 scan!(scanner, u, v);
26 writeln!(
27 writer,
28 "{}",
29 hld.query::<AdditiveOperation<_>, _>(u, v, false, |l, r| bit.fold(l, r))
30 )
31 .ok();
32 }
33 _ => panic!("unknown query"),
34 }
35 }
36}
More examples
crates/aizu_online_judge/src/grl/grl_5_e.rs (line 35)
12pub fn grl_5_e(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n, c: [SizedCollect<usize>]);
16 let edges = c
17 .take(n)
18 .enumerate()
19 .flat_map(|(u, it)| it.into_iter().map(move |v| (u, v)))
20 .collect();
21 let mut graph = UndirectedSparseGraph::from_edges(n, edges);
22 let hld = HeavyLightDecomposition::new(0, &mut graph);
23 type M = (AdditiveOperation<u64>, AdditiveOperation<u64>);
24 let mut seg = LazySegmentTree::<RangeSumRangeAdd<_>>::from_vec(vec![(0u64, 1u64); n]);
25
26 scan!(scanner, q);
27 for _ in 0..q {
28 match scanner.scan::<usize>() {
29 0 => {
30 scan!(scanner, v, w: u64);
31 hld.update(0, v, true, |l, r| seg.update(l..r, w));
32 }
33 1 => {
34 scan!(scanner, u);
35 let ans = hld.query::<M, _>(0, u, true, |l, r| seg.fold(l..r)).0;
36 writeln!(writer, "{}", ans).ok();
37 }
38 _ => panic!("unknown query"),
39 }
40 }
41}
Sourcepub fn query_noncom<M: Monoid, F1: FnMut(usize, usize) -> M::T, F2: FnMut(usize, usize) -> M::T>(
&self,
u: usize,
v: usize,
is_edge: bool,
f1: F1,
f2: F2,
) -> M::T
pub fn query_noncom<M: Monoid, F1: FnMut(usize, usize) -> M::T, F2: FnMut(usize, usize) -> M::T>( &self, u: usize, v: usize, is_edge: bool, f1: F1, f2: F2, ) -> M::T
Examples found in repository?
crates/library_checker/src/datastructure/vertex_set_path_composite.rs (lines 32-38)
12pub fn vertex_set_path_composite(reader: impl Read, mut writer: impl Write) {
13 let s = read_all_unchecked(reader);
14 let mut scanner = Scanner::new(&s);
15 scan!(scanner, n, q, ab: [(MInt998244353, MInt998244353); n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
16 let hld = HeavyLightDecomposition::new(0, &mut graph);
17 let mut nab = vec![(MInt998244353::default(), MInt998244353::default()); n];
18 for i in 0..n {
19 nab[hld.vidx[i]] = ab[i];
20 }
21 let mut seg1 = SegmentTree::<LinearOperation<_>>::from_vec(nab.clone());
22 let mut seg2 = SegmentTree::<ReverseOperation<LinearOperation<_>>>::from_vec(nab);
23 for _ in 0..q {
24 match scanner.scan::<usize>() {
25 0 => {
26 scan!(scanner, p, cd: (MInt998244353, MInt998244353));
27 seg1.set(hld.vidx[p], cd);
28 seg2.set(hld.vidx[p], cd);
29 }
30 1 => {
31 scan!(scanner, u, v, x: MInt998244353);
32 let (a, b) = hld.query_noncom::<LinearOperation<_>, _, _>(
33 u,
34 v,
35 false,
36 |l, r| seg1.fold(l..r),
37 |l, r| seg2.fold(l..r),
38 );
39 writeln!(writer, "{}", a * x + b).ok();
40 }
41 _ => panic!("unknown query"),
42 }
43 }
44}
Auto Trait Implementations§
impl Freeze for HeavyLightDecomposition
impl RefUnwindSafe for HeavyLightDecomposition
impl Send for HeavyLightDecomposition
impl Sync for HeavyLightDecomposition
impl Unpin for HeavyLightDecomposition
impl UnwindSafe for HeavyLightDecomposition
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more