Skip to main content

DualSegmentTree

Struct DualSegmentTree 

Source
pub struct DualSegmentTree<T, F> { /* private fields */ }
Expand description

半開区間への作用と一点取得を扱う双対セグメント木です。

添字は 0-indexed、区間は [left, right) です。compose(old, new) は古い作用の後に 新しい作用を適用した合成を返し、結合則と単位元 identity を満たす必要があります。 作用が非可換でも、この順序を守れば扱えます。更新と取得はいずれも O(log N) です。

§Examples

use atcoder::data_structure::dual_segment_tree::DualSegmentTree;

let mut tree = DualSegmentTree::new(|a, b| a + b, 4, 0);
tree.apply(1, 4, 3);
tree.apply(2, 3, 2);
assert_eq!(tree.get(2), 5);

Implementations§

Source§

impl<T: Copy, F: Fn(T, T) -> T> DualSegmentTree<T, F>

Source

pub fn new(compose: F, len: usize, identity: T) -> Self

長さ len、各点の初期値が identity の木を作ります。計算量・メモリ量は O(N) です。

compose(old, new) は、点に蓄積済みの作用 old の後に新しい作用 new を 適用した合成を返す関数です。identity はこの関数の単位元である必要があります。

Source

pub fn from_vec(values: impl Into<Vec<T>>, compose: F, identity: T) -> Self

各点の初期値を指定して木を構築します。計算量・メモリ量は O(N) です。

values[i] が添字 i の初期値になります。compose と identity の条件は new と同じです。

Source

pub fn from(compose: F, values: Vec<T>, identity: T) -> Self

from_vec と同じです。各点の初期値を values で指定します。

Source

pub fn apply(&mut self, left: usize, right: usize, value: T)

半開区間 [left, right) の各点に value を適用します。

添字は 0 始まりです。範囲が 0..=len を外れる、または left > right の場合は panic します。空区間なら何もしません。各点では既存の作用の後に value を適用します。 計算量は O(log N) です。

Source

pub fn get(&self, index: usize) -> T

index 番目の値を返します。添字は 0 始まりで、index >= len なら panic します。 蓄積された作用を古いものから新しいものの順に合成します。計算量は O(log N) です。

Source

pub fn at(&self, index: usize) -> T

index 番目の値を返します。添字は 0 始まりです。

get と同じで、index >= len なら panic します。計算量は O(log N) です。

Trait Implementations§

Source§

impl<T: Clone, F: Clone> Clone for DualSegmentTree<T, F>

Source§

fn clone(&self) -> DualSegmentTree<T, F>

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

Auto Trait Implementations§

§

impl<T, F> Freeze for DualSegmentTree<T, F>
where F: Freeze, T: Freeze,

§

impl<T, F> RefUnwindSafe for DualSegmentTree<T, F>

§

impl<T, F> Send for DualSegmentTree<T, F>
where F: Send, T: Send,

§

impl<T, F> Sync for DualSegmentTree<T, F>
where F: Sync, T: Sync,

§

impl<T, F> Unpin for DualSegmentTree<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnsafeUnpin for DualSegmentTree<T, F>
where F: UnsafeUnpin, T: UnsafeUnpin,

§

impl<T, F> UnwindSafe for DualSegmentTree<T, F>
where F: UnwindSafe, T: UnwindSafe,

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.