Skip to main content

SegmentTreeMap

Struct SegmentTreeMap 

Source
pub struct SegmentTreeMap<M>
where M: Monoid,
{ n: usize, seg: FibHashMap<usize, M::T>, u: M::T, }

Fields§

§n: usize§seg: FibHashMap<usize, M::T>§u: M::T

Implementations§

Source§

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

Source

pub fn new(n: usize) -> Self

Source

fn get_ref(&self, k: usize) -> &M::T

Examples found in repository?
crates/competitive/src/data_structure/segment_tree_map.rs (line 65)
58    pub fn set(&mut self, k: usize, x: M::T) {
59        debug_assert!(k < self.n);
60        let mut k = k + self.n;
61        *self.seg.entry(k).or_insert(M::unit()) = x;
62        k /= 2;
63        while k > 0 {
64            *self.seg.entry(k).or_insert(M::unit()) =
65                M::operate(self.get_ref(2 * k), self.get_ref(2 * k + 1));
66            k /= 2;
67        }
68    }
69    pub fn update(&mut self, k: usize, x: M::T) {
70        debug_assert!(k < self.n);
71        let mut k = k + self.n;
72        let t = self.seg.entry(k).or_insert(M::unit());
73        *t = M::operate(t, &x);
74        k /= 2;
75        while k > 0 {
76            *self.seg.entry(k).or_insert(M::unit()) =
77                M::operate(self.get_ref(2 * k), self.get_ref(2 * k + 1));
78            k /= 2;
79        }
80    }
81    pub fn get(&self, k: usize) -> M::T {
82        debug_assert!(k < self.n);
83        self.seg.get(&(k + self.n)).cloned().unwrap_or_else(M::unit)
84    }
85    pub fn fold<R>(&self, range: R) -> M::T
86    where
87        R: RangeBounds<usize>,
88    {
89        let range = range.to_range();
90        debug_assert!(range.end <= self.n);
91        let mut l = range.start + self.n;
92        let mut r = range.end + self.n;
93        let mut vl = M::unit();
94        let mut vr = M::unit();
95        while l < r {
96            if l & 1 != 0 {
97                vl = M::operate(&vl, self.get_ref(l));
98                l += 1;
99            }
100            if r & 1 != 0 {
101                r -= 1;
102                vr = M::operate(self.get_ref(r), &vr);
103            }
104            l /= 2;
105            r /= 2;
106        }
107        M::operate(&vl, &vr)
108    }
109    fn partition_point_perfect<P>(
110        &self,
111        mut pos: usize,
112        mut acc: M::T,
113        mut pred: P,
114    ) -> (usize, M::T)
115    where
116        P: FnMut(&M::T) -> bool,
117    {
118        while pos < self.n {
119            pos <<= 1;
120            let nacc = M::operate(&acc, self.get_ref(pos));
121            if pred(&nacc) {
122                acc = nacc;
123                pos += 1;
124            }
125        }
126        (pos - self.n, acc)
127    }
128    fn rpartition_point_perfect<P>(
129        &self,
130        mut pos: usize,
131        mut acc: M::T,
132        mut pred: P,
133    ) -> (usize, M::T)
134    where
135        P: FnMut(&M::T) -> bool,
136    {
137        while pos < self.n {
138            pos = pos * 2 + 1;
139            let nacc = M::operate(self.get_ref(pos), &acc);
140            if pred(&nacc) {
141                acc = nacc;
142                pos -= 1;
143            }
144        }
145        (pos - self.n, acc)
146    }
147    pub fn partition_point_acc<P>(&self, left: usize, mut pred: P) -> usize
148    where
149        P: FnMut(&M::T) -> bool,
150    {
151        let mut l = left + self.n;
152        let r = 2 * self.n;
153        let mut k = 0usize;
154        let mut acc = M::unit();
155        while l < r >> k {
156            if l & 1 != 0 {
157                let nacc = M::operate(&acc, self.get_ref(l));
158                if !pred(&nacc) {
159                    return self.partition_point_perfect(l, acc, pred).0;
160                }
161                acc = nacc;
162                l += 1;
163            }
164            l >>= 1;
165            k += 1;
166        }
167        for k in (0..k).rev() {
168            let r = r >> k;
169            if r & 1 != 0 {
170                let nacc = M::operate(&acc, self.get_ref(r - 1));
171                if !pred(&nacc) {
172                    return self.partition_point_perfect(r - 1, acc, pred).0;
173                }
174                acc = nacc;
175            }
176        }
177        self.n
178    }
179    pub fn rpartition_point_acc<P>(&self, right: usize, mut pred: P) -> usize
180    where
181        P: FnMut(&M::T) -> bool,
182    {
183        let mut l = self.n;
184        let mut r = right + self.n;
185        let mut c = 0usize;
186        let mut k = 0usize;
187        let mut acc = M::unit();
188        while l >> k < r {
189            c <<= 1;
190            if l & (1 << k) != 0 {
191                l += 1 << k;
192                c += 1;
193            }
194            if r & 1 != 0 {
195                r -= 1;
196                let nacc = M::operate(self.get_ref(r), &acc);
197                if !pred(&nacc) {
198                    return self.rpartition_point_perfect(r, acc, pred).0 + 1;
199                }
200                acc = nacc;
201            }
202            r >>= 1;
203            k += 1;
204        }
205        for k in (0..k).rev() {
206            if c & 1 != 0 {
207                l -= 1 << k;
208                let l = l >> k;
209                let nacc = M::operate(self.get_ref(l), &acc);
210                if !pred(&nacc) {
211                    return self.rpartition_point_perfect(l, acc, pred).0 + 1;
212                }
213                acc = nacc;
214            }
215            c >>= 1;
216        }
217        0
218    }
Source

pub fn set(&mut self, k: usize, x: M::T)

Source

pub fn update(&mut self, k: usize, x: M::T)

Source

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

Source

pub fn fold<R>(&self, range: R) -> M::T
where R: RangeBounds<usize>,

Source

fn partition_point_perfect<P>( &self, pos: usize, acc: M::T, pred: P, ) -> (usize, M::T)
where P: FnMut(&M::T) -> bool,

Examples found in repository?
crates/competitive/src/data_structure/segment_tree_map.rs (line 159)
147    pub fn partition_point_acc<P>(&self, left: usize, mut pred: P) -> usize
148    where
149        P: FnMut(&M::T) -> bool,
150    {
151        let mut l = left + self.n;
152        let r = 2 * self.n;
153        let mut k = 0usize;
154        let mut acc = M::unit();
155        while l < r >> k {
156            if l & 1 != 0 {
157                let nacc = M::operate(&acc, self.get_ref(l));
158                if !pred(&nacc) {
159                    return self.partition_point_perfect(l, acc, pred).0;
160                }
161                acc = nacc;
162                l += 1;
163            }
164            l >>= 1;
165            k += 1;
166        }
167        for k in (0..k).rev() {
168            let r = r >> k;
169            if r & 1 != 0 {
170                let nacc = M::operate(&acc, self.get_ref(r - 1));
171                if !pred(&nacc) {
172                    return self.partition_point_perfect(r - 1, acc, pred).0;
173                }
174                acc = nacc;
175            }
176        }
177        self.n
178    }
Source

fn rpartition_point_perfect<P>( &self, pos: usize, acc: M::T, pred: P, ) -> (usize, M::T)
where P: FnMut(&M::T) -> bool,

Examples found in repository?
crates/competitive/src/data_structure/segment_tree_map.rs (line 198)
179    pub fn rpartition_point_acc<P>(&self, right: usize, mut pred: P) -> usize
180    where
181        P: FnMut(&M::T) -> bool,
182    {
183        let mut l = self.n;
184        let mut r = right + self.n;
185        let mut c = 0usize;
186        let mut k = 0usize;
187        let mut acc = M::unit();
188        while l >> k < r {
189            c <<= 1;
190            if l & (1 << k) != 0 {
191                l += 1 << k;
192                c += 1;
193            }
194            if r & 1 != 0 {
195                r -= 1;
196                let nacc = M::operate(self.get_ref(r), &acc);
197                if !pred(&nacc) {
198                    return self.rpartition_point_perfect(r, acc, pred).0 + 1;
199                }
200                acc = nacc;
201            }
202            r >>= 1;
203            k += 1;
204        }
205        for k in (0..k).rev() {
206            if c & 1 != 0 {
207                l -= 1 << k;
208                let l = l >> k;
209                let nacc = M::operate(self.get_ref(l), &acc);
210                if !pred(&nacc) {
211                    return self.rpartition_point_perfect(l, acc, pred).0 + 1;
212                }
213                acc = nacc;
214            }
215            c >>= 1;
216        }
217        0
218    }
Source

pub fn partition_point_acc<P>(&self, left: usize, pred: P) -> usize
where P: FnMut(&M::T) -> bool,

Source

pub fn rpartition_point_acc<P>(&self, right: usize, pred: P) -> usize
where P: FnMut(&M::T) -> bool,

Source§

impl<M> SegmentTreeMap<M>
where M: AbelianMonoid,

Source

pub fn fold_all(&self) -> M::T

Trait Implementations§

Source§

impl<M> Clone for SegmentTreeMap<M>
where M: Monoid,

Source§

fn clone(&self) -> Self

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<M> Debug for SegmentTreeMap<M>
where M: Monoid<T: Debug>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToArrayVecScalar for T

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

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

fn try_from(value: U) -> Result<T, !>

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.