11use crate :: internal_bit:: ceil_pow2;
22
3+ // TODO Should I split monoid-related traits to another module?
34pub trait Monoid {
45 type S : Copy ;
5- const IDENTITY : Self :: S ;
6+ fn identity ( ) -> Self :: S ;
67 fn binary_operation ( a : Self :: S , b : Self :: S ) -> Self :: S ;
78}
89
910impl < M : Monoid > Segtree < M > {
1011 pub fn new ( n : usize ) -> Segtree < M > {
11- vec ! [ M :: IDENTITY ; n] . into ( )
12+ vec ! [ M :: identity ( ) ; n] . into ( )
1213 }
1314}
1415impl < M : Monoid > From < Vec < M :: S > > for Segtree < M > {
1516 fn from ( v : Vec < M :: S > ) -> Self {
1617 let n = v. len ( ) ;
1718 let log = ceil_pow2 ( n as u32 ) as usize ;
1819 let size = 1 << log;
19- let mut d = vec ! [ M :: IDENTITY ; 2 * size] ;
20+ let mut d = vec ! [ M :: identity ( ) ; 2 * size] ;
2021 d[ size..( size + n) ] . clone_from_slice ( & v) ;
2122 let mut ret = Segtree { n, size, log, d } ;
22- for i in ( 1 ..n ) . rev ( ) {
23+ for i in ( 1 ..size ) . rev ( ) {
2324 ret. update ( i) ;
2425 }
2526 ret
@@ -42,8 +43,8 @@ impl<M: Monoid> Segtree<M> {
4243
4344 pub fn prod ( & self , mut l : usize , mut r : usize ) -> M :: S {
4445 assert ! ( l <= r && r <= self . n) ;
45- let mut sml = M :: IDENTITY ;
46- let mut smr = M :: IDENTITY ;
46+ let mut sml = M :: identity ( ) ;
47+ let mut smr = M :: identity ( ) ;
4748 l += self . size ;
4849 r += self . size ;
4950
@@ -72,12 +73,12 @@ impl<M: Monoid> Segtree<M> {
7273 F : Fn ( M :: S ) -> bool ,
7374 {
7475 assert ! ( l <= self . n) ;
75- assert ! ( f( M :: IDENTITY ) ) ;
76+ assert ! ( f( M :: identity ( ) ) ) ;
7677 if l == self . n {
7778 return self . n ;
7879 }
7980 l += self . size ;
80- let mut sm = M :: IDENTITY ;
81+ let mut sm = M :: identity ( ) ;
8182 while {
8283 // do
8384 while l % 2 == 0 {
@@ -110,12 +111,12 @@ impl<M: Monoid> Segtree<M> {
110111 F : Fn ( M :: S ) -> bool ,
111112 {
112113 assert ! ( r <= self . n) ;
113- assert ! ( f( M :: IDENTITY ) ) ;
114+ assert ! ( f( M :: identity ( ) ) ) ;
114115 if r == 0 {
115116 return 0 ;
116117 }
117118 r += self . size ;
118- let mut sm = M :: IDENTITY ;
119+ let mut sm = M :: identity ( ) ;
119120 while {
120121 // do
121122 r -= 1 ;
0 commit comments