pub struct LazySegmentTree<T: LazyNode> { /* private fields */ }
Expand description

Lazy segment tree with range queries and range updates. It uses O(n) space, assuming that each node uses O(1) space.

Implementations

Builds lazy segment tree from slice, each element of the slice will correspond to a leaf of the segment tree. It has time complexity of O(n*log(n)), assuming that combine has constant time complexity.

Updates the range [i,j] with value. It will panic if i or j is not in [0,n]. It has time complexity of O(log(n)), assuming that combine, update_lazy_value and update_lazy_value have constant time complexity.

Returns the result from the range [left,right]. It returns None if and only if range is empty. It will panic if left or right are not in [0,n). It has time complexity of O(log(n)), assuming that combine, update_lazy_value and update_lazy_value have constant time complexity.

A method that finds the smallest prefix1 u such that predicate(u.value(), value) is true. The following must be true:

  • predicate is monotonic over prefixes2.
  • g will satisfy the following, given segments [i,j] and [i,k] with j<k we have that predicate([i,k].value(),value) implies predicate([j+1,k].value(),g([i,j].value(),value)).

These are two examples, the first is finding the smallest prefix which sums at least some value.

let predicate = |left_value:&usize, value:&usize|{*left_value>=*value}; // Is the sum greater or equal to value?
let g = |left_node:&usize,value:usize|{value-*left_node}; // Subtract the sum of the prefix.
let seg_tree = LazySegmentTree::build(&nodes); // [0,1,2,3,4,5,6,7,8,9] with Sum<usize> nodes
let index = seg_tree.lower_bound(predicate, g, 3); // Will return 2 as sum([0,1,2])>=3

The second is finding the position of the smallest value greater or equal to some value.

let predicate = |left_value:&usize, value:&usize|{*left_value>=*value}; // Is the maximum greater or equal to value?
let g = |_left_node:&usize,value:usize|{value}; // Do nothing
let seg_tree = LazySegmentTree::build(&nodes); // [0,1,2,3,4,5,6,7,8,9] with Max<usize> nodes
let index = seg_tree.lower_bound(predicate, g, 3); // Will return 3 as 3>=3

  1. A prefix is a segment of the form [0,i]

  2. Given two prefixes u and v if u is contained in v then predicate(u.value(), value) implies predicate(v.value(), value)

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.