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
impl CartesianTree
Sourcepub fn new<T>(a: &[T]) -> Selfwhere
T: PartialOrd,
pub fn new<T>(a: &[T]) -> Selfwhere
T: PartialOrd,
Examples found in repository?
More 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 }pub fn with_ranges(&self, f: impl FnMut(usize, Range<usize>))
Trait Implementations§
Source§impl Clone for CartesianTree
impl Clone for CartesianTree
Auto Trait Implementations§
impl Freeze for CartesianTree
impl RefUnwindSafe for CartesianTree
impl Send for CartesianTree
impl Sync for CartesianTree
impl Unpin for CartesianTree
impl UnsafeUnpin for CartesianTree
impl UnwindSafe for CartesianTree
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