Skip to main content

SegmentTree

Struct SegmentTree 

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

モノイド (T, op, identity) 上のセグメント木です。

添字は 0-indexed、区間は半開区間 [left, right) です。op は結合則を満たし、 identity はその単位元である必要があります。op が非可換でも、区間内の左から右の 順序で集約します。構築は O(N)、点更新・区間取得・ 二分探索は O(log N) です。

§Examples

use atcoder::data_structure::segment_tree::SegmentTree;

let mut tree = SegmentTree::from_vec([1, 2, 3, 4], |a, b| a + b, 0);
assert_eq!(tree.get(1, 4), 9);
tree.set(2, 10);
assert_eq!(tree.get(0, 3), 13);

Implementations§

Source§

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

Source

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

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

op は結合則を満たし、identity は左右両方の単位元である必要があります。

Source

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

values の配列から木を構築します。計算量・メモリ量は O(N) です。

op は結合則を満たし、identity は左右両方の単位元である必要があります。

Source

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

from_vec と同じです。values の配列から木を構築します。

Source

pub const fn len(&self) -> usize

要素数を返します。

Source

pub const fn is_empty(&self) -> bool

要素がなければ true を返します。

Source

pub fn set(&mut self, index: usize, value: T)

0 始まりの index 番目を value に更新します。計算量は O(log N) です。

index >= len の場合は panic します。

Source

pub fn update(&mut self, index: usize, f: impl FnOnce(T) -> T)

0 始まりの index 番目の現在値を f に渡し、戻り値で更新します。

f は1回だけ呼び出し、更新後は祖先の集約値も再計算します。 計算量は f の実行時間を除いて O(log N) です。index >= len なら f を呼び出す前に panic します。

use atcoder::data_structure::segment_tree::SegmentTree;

let mut seg = SegmentTree::from_vec([1, 2, 3], |a, b| a + b, 0);
seg.update(1, |v| v + 5);
seg.update(2, |v| v.max(10));
assert_eq!(seg.at(1), 7);
assert_eq!(seg.get(0, 3), 18);
Source

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

0 始まりの index 番目の値を返します。index >= len なら panic します。 計算量は O(1) です。

Source

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

半開区間 [left, right) の集約値を返します。

添字は 0 始まりです。left > right または right > len の場合は panic します。 空区間には単位元を返します。計算量は O(log N) です。

Source

pub fn max_right<P>(&self, left: usize, predicate: P) -> usize
where P: Fn(T) -> bool,

predicate(get(left, right)) が真である最大の right を返します。

predicate(identity) は真で、右端を伸ばしたとき述語が真から偽へ高々一度だけ 変化する必要があります。集約は op の左から右の順で行います。 返り値は left..right が条件を満たす最大の right です。left > len なら panic します。

Source

pub fn min_left<P>(&self, right: usize, predicate: P) -> usize
where P: Fn(T) -> bool,

predicate(get(left, right)) が真である最小の left を返します。

predicate(identity) は真で、左端を左へ動かしたとき述語が真から偽へ高々一度だけ 変化する必要があります。集約は op の左から右の順で行います。 返り値は left..right が条件を満たす最小の left です。right > len なら panic します。

Source§

impl<T: Debug, F> SegmentTree<T, F>

Source

pub fn debug_values(&self)

葉のうち、元配列に対応する要素だけをデバッグ出力します。 出力先や書式は crate::debug! マクロの設定に従います。

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> SegmentTree<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
Source§

impl<T, F> Index<usize> for SegmentTree<T, F>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<T, F> UnwindSafe for SegmentTree<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.