@@ -2,8 +2,9 @@ use crate::internal_bit::ceil_pow2;
2
2
use crate :: internal_type_traits:: { BoundedAbove , BoundedBelow , One , Zero } ;
3
3
use std:: cmp:: { max, min} ;
4
4
use std:: convert:: Infallible ;
5
+ use std:: iter:: FromIterator ;
5
6
use std:: marker:: PhantomData ;
6
- use std:: ops:: { Add , Bound , Mul , RangeBounds } ;
7
+ use std:: ops:: { Add , BitAnd , BitOr , BitXor , Bound , Mul , Not , RangeBounds } ;
7
8
8
9
// TODO Should I split monoid-related traits to another module?
9
10
pub trait Monoid {
68
69
}
69
70
}
70
71
72
+ pub struct BitwiseOr < S > ( Infallible , PhantomData < fn ( ) -> S > ) ;
73
+ impl < S > Monoid for BitwiseOr < S >
74
+ where
75
+ S : Copy + BitOr < Output = S > + Zero ,
76
+ {
77
+ type S = S ;
78
+ fn identity ( ) -> Self :: S {
79
+ S :: zero ( )
80
+ }
81
+ fn binary_operation ( a : & Self :: S , b : & Self :: S ) -> Self :: S {
82
+ * a | * b
83
+ }
84
+ }
85
+
86
+ pub struct BitwiseAnd < S > ( Infallible , PhantomData < fn ( ) -> S > ) ;
87
+ impl < S > Monoid for BitwiseAnd < S >
88
+ where
89
+ S : Copy + BitAnd < Output = S > + Not < Output = S > + Zero ,
90
+ {
91
+ type S = S ;
92
+ fn identity ( ) -> Self :: S {
93
+ !S :: zero ( )
94
+ }
95
+ fn binary_operation ( a : & Self :: S , b : & Self :: S ) -> Self :: S {
96
+ * a & * b
97
+ }
98
+ }
99
+
100
+ pub struct BitwiseXor < S > ( Infallible , PhantomData < fn ( ) -> S > ) ;
101
+ impl < S > Monoid for BitwiseXor < S >
102
+ where
103
+ S : Copy + BitXor < Output = S > + Zero ,
104
+ {
105
+ type S = S ;
106
+ fn identity ( ) -> Self :: S {
107
+ S :: zero ( )
108
+ }
109
+ fn binary_operation ( a : & Self :: S , b : & Self :: S ) -> Self :: S {
110
+ * a ^ * b
111
+ }
112
+ }
113
+
71
114
impl < M : Monoid > Default for Segtree < M > {
72
115
fn default ( ) -> Self {
73
116
Segtree :: new ( 0 )
@@ -84,7 +127,27 @@ impl<M: Monoid> From<Vec<M::S>> for Segtree<M> {
84
127
let log = ceil_pow2 ( n as u32 ) as usize ;
85
128
let size = 1 << log;
86
129
let mut d = vec ! [ M :: identity( ) ; 2 * size] ;
87
- d[ size..( size + n) ] . clone_from_slice ( & v) ;
130
+ d[ size..] [ ..n] . clone_from_slice ( & v) ;
131
+ let mut ret = Segtree { n, size, log, d } ;
132
+ for i in ( 1 ..size) . rev ( ) {
133
+ ret. update ( i) ;
134
+ }
135
+ ret
136
+ }
137
+ }
138
+ impl < M : Monoid > FromIterator < M :: S > for Segtree < M > {
139
+ fn from_iter < T : IntoIterator < Item = M :: S > > ( iter : T ) -> Self {
140
+ let iter = iter. into_iter ( ) ;
141
+ let n = iter. size_hint ( ) . 0 ;
142
+ let log = ceil_pow2 ( n as u32 ) as usize ;
143
+ let size = 1 << log;
144
+ let mut d = Vec :: with_capacity ( size * 2 ) ;
145
+ d. extend (
146
+ std:: iter:: repeat_with ( M :: identity)
147
+ . take ( size)
148
+ . chain ( iter)
149
+ . chain ( std:: iter:: repeat_with ( M :: identity) . take ( size - n) ) ,
150
+ ) ;
88
151
let mut ret = Segtree { n, size, log, d } ;
89
152
for i in ( 1 ..size) . rev ( ) {
90
153
ret. update ( i) ;
@@ -107,6 +170,10 @@ impl<M: Monoid> Segtree<M> {
107
170
self . d [ p + self . size ] . clone ( )
108
171
}
109
172
173
+ pub fn get_slice ( & self ) -> & [ M :: S ] {
174
+ & self . d [ self . size ..] [ ..self . n ]
175
+ }
176
+
110
177
pub fn prod < R > ( & self , range : R ) -> M :: S
111
178
where
112
179
R : RangeBounds < usize > ,
0 commit comments