Skip to main content

Doubling

Struct Doubling 

Source
pub struct Doubling<M>
where M: Monoid,
{ size: usize, table: Vec<(usize, M::T)>, }

Fields§

§size: usize§table: Vec<(usize, M::T)>

Implementations§

Source§

impl<M> Doubling<M>
where M: Monoid,

Source

pub fn new(size: usize, f: impl Fn(usize) -> (usize, M::T)) -> Self

Source

pub fn double(&mut self)

Examples found in repository?
crates/competitive/src/algorithm/doubling.rs (line 54)
37    pub fn kth(&mut self, mut pos: usize, mut k: usize) -> (usize, M::T) {
38        let mut x = M::unit();
39        for chunk in self.table.chunks_exact(self.size) {
40            if k & 1 == 1 {
41                let &(to, ref val) = &chunk[pos];
42                if to == !0 {
43                    return (!0, M::unit());
44                }
45                x = M::operate(&x, val);
46                pos = to;
47            }
48            k >>= 1;
49            if k == 0 {
50                break;
51            }
52        }
53        while k > 0 {
54            self.double();
55            if k & 1 == 1 {
56                let base = self.table.len() - self.size;
57                let &(to, ref val) = &self.table[base + pos];
58                if to == !0 {
59                    return (!0, M::unit());
60                }
61                x = M::operate(&x, val);
62                pos = to;
63            }
64            k >>= 1;
65        }
66        (pos, x)
67    }
Source

pub fn kth(&mut self, pos: usize, k: usize) -> (usize, M::T)

Source

pub fn kth_multiple( &self, queries: impl IntoIterator<Item = (usize, usize)>, ) -> Vec<(usize, M::T)>

queries: (pos, k) Return: (pos, acc)

Source

pub fn find_last( &self, pos: usize, pred: impl FnMut(usize, &M::T) -> bool, ) -> (usize, (usize, M::T))

Return: (k, (pos, acc))

Examples found in repository?
crates/competitive/src/algorithm/doubling.rs (line 153)
148    pub fn find_first(
149        &self,
150        pos: usize,
151        mut pred: impl FnMut(usize, &M::T) -> bool,
152    ) -> Option<(usize, (usize, M::T))> {
153        let (mut k, (mut pos, mut x)) = self.find_last(pos, |k, x| !pred(k, x));
154        k += 1;
155        M::operate_assign(&mut x, &self.table[pos].1);
156        pos = self.table[pos].0;
157        if pred(pos, &x) {
158            Some((k, (pos, x)))
159        } else {
160            None
161        }
162    }
Source

pub fn find_first( &self, pos: usize, pred: impl FnMut(usize, &M::T) -> bool, ) -> Option<(usize, (usize, M::T))>

Return: (k, (pos, acc))

Auto Trait Implementations§

§

impl<M> Freeze for Doubling<M>

§

impl<M> RefUnwindSafe for Doubling<M>
where <M as Magma>::T: RefUnwindSafe,

§

impl<M> Send for Doubling<M>
where <M as Magma>::T: Send,

§

impl<M> Sync for Doubling<M>
where <M as Magma>::T: Sync,

§

impl<M> Unpin for Doubling<M>
where <M as Magma>::T: Unpin,

§

impl<M> UnsafeUnpin for Doubling<M>

§

impl<M> UnwindSafe for Doubling<M>
where <M as Magma>::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> 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> ToArrayVecScalar for T

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.