Skip to main content

CartesianTree

Struct CartesianTree 

Source
pub struct CartesianTree {
    pub root: usize,
    pub parents: Vec<usize>,
    pub children: Vec<[usize; 2]>,
}

Fields§

§root: usize§parents: Vec<usize>§children: Vec<[usize; 2]>

Implementations§

Source§

impl CartesianTree

Source

pub fn new<T>(a: &[T]) -> Self
where T: PartialOrd,

Examples found in repository?
crates/library_checker/src/tree/cartesian_tree.rs (line 8)
5pub fn cartesian_tree(reader: impl Read, writer: impl Write) {
6    prepare_io!(reader, writer);
7    sc!(n, a: [i32; n]);
8    let ct = CartesianTree::new(&a);
9    pp!(@it ct.parents.iter().map(|&p| if p == !0 { ct.root } else { p }));
10}
More examples
Hide additional examples
crates/competitive/src/tree/heavy_light_decomposition.rs (line 394)
355    pub fn build_fold<M: Monoid>(&self, values: &[M::T]) -> HeavyLightPathFold<'_, M> {
356        assert_eq!(values.len(), self.len());
357        let mut fold = HeavyLightPathFold {
358            tree: self,
359            nodes: self
360                .order
361                .iter()
362                .map(|&v| PathFoldNode {
363                    parent: u32::MAX,
364                    children: [u32::MAX; 2],
365                    priority: 0,
366                    value: values[v].clone(),
367                    aggregate: [values[v].clone(), values[v].clone()],
368                    prefix: [values[v].clone(), values[v].clone()],
369                })
370                .collect(),
371        };
372        let mut start = 0;
373        let mut priorities = Vec::new();
374        let mut stack = Vec::new();
375        while start < self.len() {
376            let mut end = start + 1;
377            while end < self.len() && self.nodes[self.order[end]].head as usize == start {
378                end += 1;
379            }
380            priorities.clear();
381            let mut sum = 0usize;
382            for i in start..end {
383                let weight = self.subtree_size(self.order[i])
384                    - if i + 1 < end {
385                        self.subtree_size(self.order[i + 1])
386                    } else {
387                        0
388                    };
389                let priority = (sum ^ (sum + weight)).ilog2();
390                sum += weight;
391                fold.nodes[i].priority = priority;
392                priorities.push(std::cmp::Reverse(priority));
393            }
394            let cartesian = CartesianTree::new(&priorities);
395            for i in start..end {
396                let parent = cartesian.parents[i - start];
397                fold.nodes[i].parent = if parent == usize::MAX {
398                    u32::MAX
399                } else {
400                    (parent + start) as u32
401                };
402                fold.nodes[i].children = cartesian.children[i - start].map(|v| {
403                    if v == usize::MAX {
404                        u32::MAX
405                    } else {
406                        (v + start) as u32
407                    }
408                });
409            }
410            stack.clear();
411            stack.push(cartesian.root + start);
412            let mut i = 0;
413            while i < stack.len() {
414                stack.extend(
415                    fold.nodes[stack[i]]
416                        .children
417                        .into_iter()
418                        .filter(|&v| v != u32::MAX)
419                        .map(|v| v as usize),
420                );
421                i += 1;
422            }
423            for &i in stack.iter().rev() {
424                fold.pull(i);
425            }
426            start = end;
427        }
428        fold
429    }
Source

pub fn with_ranges(&self, f: impl FnMut(usize, Range<usize>))

Trait Implementations§

Source§

impl Clone for CartesianTree

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CartesianTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToArrayVecScalar for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.