1
1
use crate :: internal_bit:: ceil_pow2;
2
2
3
+ // TODO Should I split monoid-related traits to another module?
3
4
pub trait Monoid {
4
5
type S : Copy ;
5
- const IDENTITY : Self :: S ;
6
+ fn identity ( ) -> Self :: S ;
6
7
fn binary_operation ( a : Self :: S , b : Self :: S ) -> Self :: S ;
7
8
}
8
9
9
10
impl < M : Monoid > Segtree < M > {
10
11
pub fn new ( n : usize ) -> Segtree < M > {
11
- vec ! [ M :: IDENTITY ; n] . into ( )
12
+ vec ! [ M :: identity ( ) ; n] . into ( )
12
13
}
13
14
}
14
15
impl < M : Monoid > From < Vec < M :: S > > for Segtree < M > {
15
16
fn from ( v : Vec < M :: S > ) -> Self {
16
17
let n = v. len ( ) ;
17
18
let log = ceil_pow2 ( n as u32 ) as usize ;
18
19
let size = 1 << log;
19
- let mut d = vec ! [ M :: IDENTITY ; 2 * size] ;
20
+ let mut d = vec ! [ M :: identity ( ) ; 2 * size] ;
20
21
d[ size..( size + n) ] . clone_from_slice ( & v) ;
21
22
let mut ret = Segtree { n, size, log, d } ;
22
- for i in ( 1 ..n ) . rev ( ) {
23
+ for i in ( 1 ..size ) . rev ( ) {
23
24
ret. update ( i) ;
24
25
}
25
26
ret
@@ -42,8 +43,8 @@ impl<M: Monoid> Segtree<M> {
42
43
43
44
pub fn prod ( & self , mut l : usize , mut r : usize ) -> M :: S {
44
45
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 ( ) ;
47
48
l += self . size ;
48
49
r += self . size ;
49
50
@@ -72,12 +73,12 @@ impl<M: Monoid> Segtree<M> {
72
73
F : Fn ( M :: S ) -> bool ,
73
74
{
74
75
assert ! ( l <= self . n) ;
75
- assert ! ( f( M :: IDENTITY ) ) ;
76
+ assert ! ( f( M :: identity ( ) ) ) ;
76
77
if l == self . n {
77
78
return self . n ;
78
79
}
79
80
l += self . size ;
80
- let mut sm = M :: IDENTITY ;
81
+ let mut sm = M :: identity ( ) ;
81
82
while {
82
83
// do
83
84
while l % 2 == 0 {
@@ -110,12 +111,12 @@ impl<M: Monoid> Segtree<M> {
110
111
F : Fn ( M :: S ) -> bool ,
111
112
{
112
113
assert ! ( r <= self . n) ;
113
- assert ! ( f( M :: IDENTITY ) ) ;
114
+ assert ! ( f( M :: identity ( ) ) ) ;
114
115
if r == 0 {
115
116
return 0 ;
116
117
}
117
118
r += self . size ;
118
- let mut sm = M :: IDENTITY ;
119
+ let mut sm = M :: identity ( ) ;
119
120
while {
120
121
// do
121
122
r -= 1 ;
0 commit comments