pub struct BinaryIndexedTree2D<M>where
M: Monoid,{
h: usize,
w: usize,
bit: Vec<M::T>,
}Fields§
§h: usize§w: usize§bit: Vec<M::T>Implementations§
Source§impl<M> BinaryIndexedTree2D<M>where
M: Monoid,
impl<M> BinaryIndexedTree2D<M>where
M: Monoid,
pub fn new(h: usize, w: usize) -> Self
Sourcepub fn accumulate0(&self, i: usize, j: usize) -> M::T
pub fn accumulate0(&self, i: usize, j: usize) -> M::T
fold [0, i) x [0, j)
Examples found in repository?
crates/competitive/src/data_structure/binary_indexed_tree_2d.rs (line 70)
69 pub fn accumulate(&self, i: usize, j: usize) -> M::T {
70 self.accumulate0(i + 1, j + 1)
71 }
72 #[inline]
73 pub fn update(&mut self, i: usize, j: usize, x: M::T) {
74 assert!(i < self.h && j < self.w);
75 let mut a = i + 1;
76 let stride = self.w + 1;
77 while a <= self.h {
78 let mut b = j + 1;
79 while b <= self.w {
80 // SAFETY: the method validates the leaf, and Fenwick ancestors stay within the
81 // allocated table.
82 M::operate_assign(unsafe { self.bit.get_unchecked_mut(a * stride + b) }, &x);
83 b += b & (!b + 1);
84 }
85 a += a & (!a + 1);
86 }
87 }
88}
89
90impl<G> BinaryIndexedTree2D<G>
91where
92 G: Group,
93{
94 #[inline]
95 /// 0-indexed [i1, i2) x [j1, j2)
96 pub fn fold(&self, i1: usize, j1: usize, i2: usize, j2: usize) -> G::T {
97 let mut res = self.accumulate0(i1, j1);
98 G::rinv_operate_assign(&mut res, &self.accumulate0(i1, j2));
99 G::rinv_operate_assign(&mut res, &self.accumulate0(i2, j1));
100 G::operate_assign(&mut res, &self.accumulate0(i2, j2));
101 res
102 }Sourcepub fn accumulate(&self, i: usize, j: usize) -> M::T
pub fn accumulate(&self, i: usize, j: usize) -> M::T
fold [0, i] x [0, j]
Source§impl<G> BinaryIndexedTree2D<G>where
G: Group,
impl<G> BinaryIndexedTree2D<G>where
G: Group,
Trait Implementations§
Source§impl<M> Clone for BinaryIndexedTree2D<M>where
M: Monoid,
impl<M> Clone for BinaryIndexedTree2D<M>where
M: Monoid,
Auto Trait Implementations§
impl<M> Freeze for BinaryIndexedTree2D<M>
impl<M> RefUnwindSafe for BinaryIndexedTree2D<M>
impl<M> Send for BinaryIndexedTree2D<M>
impl<M> Sync for BinaryIndexedTree2D<M>
impl<M> Unpin for BinaryIndexedTree2D<M>
impl<M> UnsafeUnpin for BinaryIndexedTree2D<M>
impl<M> UnwindSafe for BinaryIndexedTree2D<M>
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