Struct nekolib::ds::vec_segtree::VecSegtree
source · pub struct VecSegtree<M>where
M: Monoid,
M::Set: Clone,{ /* private fields */ }
Expand description
Vec
ベースのセグ木。
非再帰実装かつ配列サイズを $2n$ とするセグ木。 モノイドを対象として要素の取得・更新および任意区間のモノイド積を処理する。 加えて、モノイド積を引数とする述語に対して、それが真となる区間の境界を求められる。
Complexity
演算 | 時間計算量 |
---|---|
new , from | $\Theta(n)$ |
index | $\Theta(1)$ |
set_value | $\Theta(\log(n))$ |
区間 $[l, r)$ の fold | $\Theta(\log(r-l))$ |
fold_bisect , fold_bisect_rev | $O(\log(n))$ |
Examples
use nekolib::ds::VecSegtree;
use nekolib::traits::{Fold, FoldBisect, FoldBisectRev, GetMut};
use nekolib::utils::OpAdd;
let mut vs: VecSegtree<OpAdd<i32>> = vec![2, 4, 7, 3, 5].into();
assert_eq!(vs.fold(1..3), 11);
assert_eq!(vs.fold(..), 21);
*vs.get_mut(2).unwrap() = 1; // [2, 4, 1, 3, 5]
assert_eq!(vs.fold(1..3), 5);
assert_eq!(vs.fold(1..=3), 8);
assert_eq!(vs.fold_bisect(1, |&x| x < 4), (1_usize, 0));
assert_eq!(vs.fold_bisect(1, |&x| x <= 4), (2_usize, 4));
assert_eq!(vs.fold_bisect(1, |&x| x < 13), (4_usize, 8));
assert_eq!(vs.fold_bisect(1, |&x| x <= 13), (5_usize, 13));
assert_eq!(vs.fold(..), 15);
assert_eq!(vs.fold_bisect_rev(5, |&x| x <= 0), (5_usize, 0));
assert_eq!(vs.fold_bisect_rev(5, |&x| x < 15), (1_usize, 13));
assert_eq!(vs.fold_bisect_rev(5, |&x| x <= 15), (0_usize, 15));
let l = 1;
let pred = |&x: &i32| x <= 12;
let (r, x) = vs.fold_bisect(l, pred);
assert_eq!(vs.fold(l..r), x);
assert!(pred(&x));
assert!(r == vs.len() || !pred(&vs.fold(l..r + 1)));
let r = 5;
let pred = |&x: &i32| x <= 12;
let (l, x) = vs.fold_bisect_rev(r, pred);
assert_eq!(vs.fold(l..r), x);
assert!(pred(&x));
assert!(l == 0 || !pred(&vs.fold(l - 1..r)));
Implementations§
Trait Implementations§
source§impl<M> Clone for VecSegtree<M>where
M: Monoid + Clone,
M::Set: Clone + Clone,
impl<M> Clone for VecSegtree<M>where M: Monoid + Clone, M::Set: Clone + Clone,
source§fn clone(&self) -> VecSegtree<M>
fn clone(&self) -> VecSegtree<M>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<M, B> Fold<B> for VecSegtree<M>where
M: Monoid,
M::Set: Clone,
B: RangeBounds<usize>,
impl<M, B> Fold<B> for VecSegtree<M>where M: Monoid, M::Set: Clone, B: RangeBounds<usize>,
source§impl<M> FoldBisect for VecSegtree<M>where
M: Monoid,
M::Set: Clone,
impl<M> FoldBisect for VecSegtree<M>where M: Monoid, M::Set: Clone,
source§impl<M> FoldBisectRev for VecSegtree<M>where
M: Monoid,
M::Set: Clone,
impl<M> FoldBisectRev for VecSegtree<M>where M: Monoid, M::Set: Clone,
source§impl<M> From<(Vec<<M as Magma>::Set, Global>, M)> for VecSegtree<M>where
M: Monoid,
M::Set: Clone,
impl<M> From<(Vec<<M as Magma>::Set, Global>, M)> for VecSegtree<M>where M: Monoid, M::Set: Clone,
source§impl<M> From<Vec<<M as Magma>::Set, Global>> for VecSegtree<M>where
M: Monoid + Default,
M::Set: Clone,
impl<M> From<Vec<<M as Magma>::Set, Global>> for VecSegtree<M>where M: Monoid + Default, M::Set: Clone,
source§impl<M> From<VecSegtree<M>> for Vec<M::Set>where
M: Monoid,
M::Set: Clone,
impl<M> From<VecSegtree<M>> for Vec<M::Set>where M: Monoid, M::Set: Clone,
source§fn from(v: VecSegtree<M>) -> Self
fn from(v: VecSegtree<M>) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl<M> RefUnwindSafe for VecSegtree<M>where M: RefUnwindSafe, <M as Magma>::Set: RefUnwindSafe,
impl<M> Send for VecSegtree<M>where M: Send, <M as Magma>::Set: Send,
impl<M> Sync for VecSegtree<M>where M: Sync, <M as Magma>::Set: Sync,
impl<M> Unpin for VecSegtree<M>where M: Unpin, <M as Magma>::Set: Unpin,
impl<M> UnwindSafe for VecSegtree<M>where M: UnwindSafe, <M as Magma>::Set: UnwindSafe,
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