Struct HeavyLightDecomposition

Source
pub struct HeavyLightDecomposition {
    pub par: Vec<usize>,
    pub vidx: Vec<usize>,
    /* private fields */
}

Fields§

§par: Vec<usize>§vidx: Vec<usize>

Implementations§

Source§

impl HeavyLightDecomposition

Source

pub fn new(root: usize, graph: &mut UndirectedSparseGraph) -> Self

Examples found in repository?
crates/library_checker/src/graph/lca.rs (line 29)
23pub fn lca_hld(reader: impl Read, mut writer: impl Write) {
24    let s = read_all_unchecked(reader);
25    let mut scanner = Scanner::new(&s);
26    scan!(scanner, n, q, p: [usize]);
27    let edges = p.take(n - 1).enumerate().map(|(i, p)| (i + 1, p)).collect();
28    let mut graph = UndirectedSparseGraph::from_edges(n, edges);
29    let hld = HeavyLightDecomposition::new(0, &mut graph);
30    for (u, v) in scanner.iter::<(usize, usize)>().take(q) {
31        writeln!(writer, "{}", hld.lca(u, v)).ok();
32    }
33}
More examples
Hide additional 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}
Source

pub fn lca(&self, u: usize, v: usize) -> usize

Examples found in repository?
crates/library_checker/src/graph/lca.rs (line 31)
23pub fn lca_hld(reader: impl Read, mut writer: impl Write) {
24    let s = read_all_unchecked(reader);
25    let mut scanner = Scanner::new(&s);
26    scan!(scanner, n, q, p: [usize]);
27    let edges = p.take(n - 1).enumerate().map(|(i, p)| (i + 1, p)).collect();
28    let mut graph = UndirectedSparseGraph::from_edges(n, edges);
29    let hld = HeavyLightDecomposition::new(0, &mut graph);
30    for (u, v) in scanner.iter::<(usize, usize)>().take(q) {
31        writeln!(writer, "{}", hld.lca(u, v)).ok();
32    }
33}
Source

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}
Source

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
Hide additional 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}
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.