Struct seg_tree::segment_tree::LazyPersistentSegmentTree
source · [−]pub struct LazyPersistentSegmentTree<T: PersistentNode + LazyNode> { /* private fields */ }
Expand description
Lazy persistent segment tree, it saves every version of itself, it has range queries and range updates.
It uses O(n+q*log(n))
space, where q
is the amount of updates, and assuming that each node uses O(1)
space.
Implementations
sourceimpl<T> LazyPersistentSegmentTree<T> where
T: PersistentNode + LazyNode + Clone,
impl<T> LazyPersistentSegmentTree<T> where
T: PersistentNode + LazyNode + Clone,
sourcepub fn build(values: &[T]) -> Self
pub fn build(values: &[T]) -> Self
Builds a lazy persistent 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.
sourcepub fn query(&mut self, version: usize, left: usize, right: usize) -> Option<T>
pub fn query(&mut self, version: usize, left: usize, right: usize) -> Option<T>
Returns the result from the range [left,right]
from the version of the segment tree.
It returns None if and only if range is empty.
It will panic if left or right are not in [0,n), or if version is not in [0,versions).
It has time complexity of O(log(n))
, assuming that combine, update_lazy_value and update_lazy_value have constant time complexity.
sourcepub fn update(
&mut self,
version: usize,
left: usize,
right: usize,
value: <T as Node>::Value
)
pub fn update(
&mut self,
version: usize,
left: usize,
right: usize,
value: <T as Node>::Value
)
It will panic if p is not in [0,n)
, or if version is not in [0,versions).
It has time complexity of O(log(n))
, assuming that combine, update_lazy_value and update_lazy_value have constant time complexity.
sourcepub fn versions(&self) -> usize
pub fn versions(&self) -> usize
Return the amount of different versions the current segment tree has.
sourcepub fn lower_bound<F>(
&self,
version: usize,
predicate: F,
g: fn(_: &<T as Node>::Value, _: <T as Node>::Value) -> <T as Node>::Value,
value: <T as Node>::Value
) -> usize where
F: Fn(&<T as Node>::Value, &<T as Node>::Value) -> bool,
pub fn lower_bound<F>(
&self,
version: usize,
predicate: F,
g: fn(_: &<T as Node>::Value, _: <T as Node>::Value) -> <T as Node>::Value,
value: <T as Node>::Value
) -> usize where
F: Fn(&<T as Node>::Value, &<T as Node>::Value) -> bool,
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]
withj<k
we have thatpredicate([i,k].value(),value)
impliespredicate([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 = LazyPersistentSegmentTree::build(&nodes); // [0,1,2,3,4,5,6,7,8,9] with Sum<usize> nodes
let index = seg_tree.lower_bound(0, 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 = LazyPersistentSegmentTree::build(&nodes); // [0,1,2,3,4,5,6,7,8,9] with Max<usize> nodes
let index = seg_tree.lower_bound(0, predicate, g, 3); // Will return 3 as 3>=3
Auto Trait Implementations
impl<T> RefUnwindSafe for LazyPersistentSegmentTree<T> where
T: RefUnwindSafe,
impl<T> Send for LazyPersistentSegmentTree<T> where
T: Send,
impl<T> Sync for LazyPersistentSegmentTree<T> where
T: Sync,
impl<T> Unpin for LazyPersistentSegmentTree<T> where
T: Unpin,
impl<T> UnwindSafe for LazyPersistentSegmentTree<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more