pub struct BinaryIndexedTree<M>where
M: Monoid,{ /* private fields */ }
Implementations§
Source§impl<M> BinaryIndexedTree<M>where
M: Monoid,
impl<M> BinaryIndexedTree<M>where
M: Monoid,
Sourcepub fn new(n: usize) -> Self
pub fn new(n: usize) -> Self
Examples found in repository?
crates/library_checker/src/data_structure/point_add_range_sum.rs (line 13)
9pub fn point_add_range_sum_binary_indexed_tree(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64]);
13 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
14 for (i, a) in a.take(n).enumerate() {
15 bit.update(i, a);
16 }
17 for _ in 0..q {
18 match scanner.scan::<usize>() {
19 0 => {
20 scan!(scanner, p, x: i64);
21 bit.update(p, x);
22 }
23 1 => {
24 scan!(scanner, l, r);
25 writeln!(writer, "{}", bit.fold(l, r)).ok();
26 }
27 _ => panic!("unknown query"),
28 }
29 }
30}
More examples
crates/library_checker/src/tree/vertex_add_path_sum.rs (line 14)
9pub fn vertex_add_path_sum(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64; n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
13 let hld = HeavyLightDecomposition::new(0, &mut graph);
14 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
15 for (i, a) in a.iter().cloned().enumerate() {
16 bit.update(hld.vidx[i], a);
17 }
18 for _ in 0..q {
19 match scanner.scan::<usize>() {
20 0 => {
21 scan!(scanner, p, x: i64);
22 bit.update(hld.vidx[p], x);
23 }
24 1 => {
25 scan!(scanner, u, v);
26 writeln!(
27 writer,
28 "{}",
29 hld.query::<AdditiveOperation<_>, _>(u, v, false, |l, r| bit.fold(l, r))
30 )
31 .ok();
32 }
33 _ => panic!("unknown query"),
34 }
35 }
36}
crates/aizu_online_judge/src/grl/grl_5_d.rs (line 20)
9pub fn grl_5_d(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, c: [SizedCollect<usize>]);
13 let edges = c
14 .take(n)
15 .enumerate()
16 .flat_map(|(u, it)| it.into_iter().map(move |v| (u, v)))
17 .collect();
18 let graph = UndirectedSparseGraph::from_edges(n, edges);
19 let et = EulerTourForEdge::new(0, &graph);
20 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(et.length());
21
22 scan!(scanner, q);
23 for _ in 0..q {
24 match scanner.scan::<usize>() {
25 0 => {
26 scan!(scanner, v, w: i64);
27 let (l, r) = et.eidx[et.par[v]];
28 bit.update(l, w);
29 bit.update(r, -w);
30 }
31 1 => {
32 scan!(scanner, u);
33 let ans = if u > 0 {
34 bit.accumulate(et.eidx[et.par[u]].0)
35 } else {
36 0
37 };
38 writeln!(writer, "{}", ans).ok();
39 }
40 _ => panic!("unknown query"),
41 }
42 }
43}
pub fn from_slice(slice: &[M::T]) -> Self
Sourcepub fn accumulate0(&self, k: usize) -> M::T
pub fn accumulate0(&self, k: usize) -> M::T
fold [0, k)
Examples found in repository?
crates/competitive/src/data_structure/binary_indexed_tree.rs (line 74)
73 pub fn accumulate(&self, k: usize) -> M::T {
74 self.accumulate0(k + 1)
75 }
76 #[inline]
77 pub fn update(&mut self, k: usize, x: M::T) {
78 debug_assert!(k < self.n);
79 let mut k = k + 1;
80 while k <= self.n {
81 self.bit[k] = M::operate(&self.bit[k], &x);
82 k += k & (!k + 1);
83 }
84 }
85}
86
87impl<G: Group> BinaryIndexedTree<G> {
88 #[inline]
89 pub fn fold(&self, l: usize, r: usize) -> G::T {
90 debug_assert!(l <= self.n && r <= self.n);
91 G::operate(&G::inverse(&self.accumulate0(l)), &self.accumulate0(r))
92 }
Sourcepub fn accumulate(&self, k: usize) -> M::T
pub fn accumulate(&self, k: usize) -> M::T
fold [0, k]
Examples found in repository?
crates/aizu_online_judge/src/grl/grl_5_d.rs (line 34)
9pub fn grl_5_d(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, c: [SizedCollect<usize>]);
13 let edges = c
14 .take(n)
15 .enumerate()
16 .flat_map(|(u, it)| it.into_iter().map(move |v| (u, v)))
17 .collect();
18 let graph = UndirectedSparseGraph::from_edges(n, edges);
19 let et = EulerTourForEdge::new(0, &graph);
20 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(et.length());
21
22 scan!(scanner, q);
23 for _ in 0..q {
24 match scanner.scan::<usize>() {
25 0 => {
26 scan!(scanner, v, w: i64);
27 let (l, r) = et.eidx[et.par[v]];
28 bit.update(l, w);
29 bit.update(r, -w);
30 }
31 1 => {
32 scan!(scanner, u);
33 let ans = if u > 0 {
34 bit.accumulate(et.eidx[et.par[u]].0)
35 } else {
36 0
37 };
38 writeln!(writer, "{}", ans).ok();
39 }
40 _ => panic!("unknown query"),
41 }
42 }
43}
Sourcepub fn update(&mut self, k: usize, x: M::T)
pub fn update(&mut self, k: usize, x: M::T)
Examples found in repository?
More examples
crates/library_checker/src/data_structure/point_add_range_sum.rs (line 15)
9pub fn point_add_range_sum_binary_indexed_tree(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64]);
13 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
14 for (i, a) in a.take(n).enumerate() {
15 bit.update(i, a);
16 }
17 for _ in 0..q {
18 match scanner.scan::<usize>() {
19 0 => {
20 scan!(scanner, p, x: i64);
21 bit.update(p, x);
22 }
23 1 => {
24 scan!(scanner, l, r);
25 writeln!(writer, "{}", bit.fold(l, r)).ok();
26 }
27 _ => panic!("unknown query"),
28 }
29 }
30}
crates/library_checker/src/tree/vertex_add_path_sum.rs (line 16)
9pub fn vertex_add_path_sum(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64; n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
13 let hld = HeavyLightDecomposition::new(0, &mut graph);
14 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
15 for (i, a) in a.iter().cloned().enumerate() {
16 bit.update(hld.vidx[i], a);
17 }
18 for _ in 0..q {
19 match scanner.scan::<usize>() {
20 0 => {
21 scan!(scanner, p, x: i64);
22 bit.update(hld.vidx[p], x);
23 }
24 1 => {
25 scan!(scanner, u, v);
26 writeln!(
27 writer,
28 "{}",
29 hld.query::<AdditiveOperation<_>, _>(u, v, false, |l, r| bit.fold(l, r))
30 )
31 .ok();
32 }
33 _ => panic!("unknown query"),
34 }
35 }
36}
crates/aizu_online_judge/src/grl/grl_5_d.rs (line 28)
9pub fn grl_5_d(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, c: [SizedCollect<usize>]);
13 let edges = c
14 .take(n)
15 .enumerate()
16 .flat_map(|(u, it)| it.into_iter().map(move |v| (u, v)))
17 .collect();
18 let graph = UndirectedSparseGraph::from_edges(n, edges);
19 let et = EulerTourForEdge::new(0, &graph);
20 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(et.length());
21
22 scan!(scanner, q);
23 for _ in 0..q {
24 match scanner.scan::<usize>() {
25 0 => {
26 scan!(scanner, v, w: i64);
27 let (l, r) = et.eidx[et.par[v]];
28 bit.update(l, w);
29 bit.update(r, -w);
30 }
31 1 => {
32 scan!(scanner, u);
33 let ans = if u > 0 {
34 bit.accumulate(et.eidx[et.par[u]].0)
35 } else {
36 0
37 };
38 writeln!(writer, "{}", ans).ok();
39 }
40 _ => panic!("unknown query"),
41 }
42 }
43}
Source§impl<G: Group> BinaryIndexedTree<G>
impl<G: Group> BinaryIndexedTree<G>
Sourcepub fn fold(&self, l: usize, r: usize) -> G::T
pub fn fold(&self, l: usize, r: usize) -> G::T
Examples found in repository?
More examples
crates/library_checker/src/data_structure/point_add_range_sum.rs (line 25)
9pub fn point_add_range_sum_binary_indexed_tree(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64]);
13 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
14 for (i, a) in a.take(n).enumerate() {
15 bit.update(i, a);
16 }
17 for _ in 0..q {
18 match scanner.scan::<usize>() {
19 0 => {
20 scan!(scanner, p, x: i64);
21 bit.update(p, x);
22 }
23 1 => {
24 scan!(scanner, l, r);
25 writeln!(writer, "{}", bit.fold(l, r)).ok();
26 }
27 _ => panic!("unknown query"),
28 }
29 }
30}
crates/library_checker/src/tree/vertex_add_path_sum.rs (line 29)
9pub fn vertex_add_path_sum(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, n, q, a: [i64; n], (mut graph, _): @TreeGraphScanner::<usize, ()>::new(n));
13 let hld = HeavyLightDecomposition::new(0, &mut graph);
14 let mut bit = BinaryIndexedTree::<AdditiveOperation<_>>::new(n);
15 for (i, a) in a.iter().cloned().enumerate() {
16 bit.update(hld.vidx[i], a);
17 }
18 for _ in 0..q {
19 match scanner.scan::<usize>() {
20 0 => {
21 scan!(scanner, p, x: i64);
22 bit.update(hld.vidx[p], x);
23 }
24 1 => {
25 scan!(scanner, u, v);
26 writeln!(
27 writer,
28 "{}",
29 hld.query::<AdditiveOperation<_>, _>(u, v, false, |l, r| bit.fold(l, r))
30 )
31 .ok();
32 }
33 _ => panic!("unknown query"),
34 }
35 }
36}
pub fn set(&mut self, k: usize, x: G::T)
Source§impl<M: Monoid> BinaryIndexedTree<M>
impl<M: Monoid> BinaryIndexedTree<M>
pub fn lower_bound(&self, x: M::T) -> usize
Trait Implementations§
Source§impl<M> Clone for BinaryIndexedTree<M>where
M: Monoid,
impl<M> Clone for BinaryIndexedTree<M>where
M: Monoid,
Auto Trait Implementations§
impl<M> Freeze for BinaryIndexedTree<M>
impl<M> RefUnwindSafe for BinaryIndexedTree<M>
impl<M> Send for BinaryIndexedTree<M>
impl<M> Sync for BinaryIndexedTree<M>
impl<M> Unpin for BinaryIndexedTree<M>
impl<M> UnwindSafe for BinaryIndexedTree<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