Skip to content

Commit 331ea67

Browse files
Merge branch 'feature/segtree' into feature/lazysegtree
2 parents f54acaf + d4bb776 commit 331ea67

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

examples/practice2_j_segment_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn main() {
2727
3 => {
2828
let x = input.next().unwrap().parse().unwrap();
2929
let v = input.next().unwrap().parse().unwrap();
30-
println!("{}", segtree.max_right(x, |a| a < v))
30+
println!("{}", segtree.max_right(x, |a| a < &v))
3131
}
3232
_ => {}
3333
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub use modint::{
2929
ModInt998244353, Modulus, RemEuclidU32, StaticModInt,
3030
};
3131
pub use scc::SccGraph;
32-
pub use segtree::{Max, Monoid, Segtree};
32+
pub use segtree::{Additive, Max, Min, Monoid, Multiplicative, Segtree};
3333
pub use string::{
3434
lcp_array, lcp_array_arbitrary, suffix_array, suffix_array_arbitrary, suffix_array_manual,
3535
z_algorithm, z_algorithm_arbitrary,

src/segtree.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ where
4040
}
4141
}
4242

43-
pub struct Sum<S>(Infallible, PhantomData<fn() -> S>);
44-
impl<S> Monoid for Sum<S>
43+
pub struct Additive<S>(Infallible, PhantomData<fn() -> S>);
44+
impl<S> Monoid for Additive<S>
4545
where
4646
S: Copy + Add<Output = S> + Zero,
4747
{
@@ -54,8 +54,8 @@ where
5454
}
5555
}
5656

57-
pub struct Product<S>(Infallible, PhantomData<fn() -> S>);
58-
impl<S> Monoid for Product<S>
57+
pub struct Multiplicative<S>(Infallible, PhantomData<fn() -> S>);
58+
impl<S> Monoid for Multiplicative<S>
5959
where
6060
S: Copy + Mul<Output = S> + One,
6161
{
@@ -136,10 +136,10 @@ impl<M: Monoid> Segtree<M> {
136136

137137
pub fn max_right<F>(&self, mut l: usize, f: F) -> usize
138138
where
139-
F: Fn(M::S) -> bool,
139+
F: Fn(&M::S) -> bool,
140140
{
141141
assert!(l <= self.n);
142-
assert!(f(M::identity()));
142+
assert!(f(&M::identity()));
143143
if l == self.n {
144144
return self.n;
145145
}
@@ -150,11 +150,11 @@ impl<M: Monoid> Segtree<M> {
150150
while l % 2 == 0 {
151151
l >>= 1;
152152
}
153-
if !f(M::binary_operation(&sm, &self.d[l])) {
153+
if !f(&M::binary_operation(&sm, &self.d[l])) {
154154
while l < self.size {
155155
l *= 2;
156156
let res = M::binary_operation(&sm, &self.d[l]);
157-
if f(res.clone()) {
157+
if f(&res) {
158158
sm = res;
159159
l += 1;
160160
}
@@ -174,10 +174,10 @@ impl<M: Monoid> Segtree<M> {
174174

175175
pub fn min_left<F>(&self, mut r: usize, f: F) -> usize
176176
where
177-
F: Fn(M::S) -> bool,
177+
F: Fn(&M::S) -> bool,
178178
{
179179
assert!(r <= self.n);
180-
assert!(f(M::identity()));
180+
assert!(f(&M::identity()));
181181
if r == 0 {
182182
return 0;
183183
}
@@ -189,11 +189,11 @@ impl<M: Monoid> Segtree<M> {
189189
while r > 1 && r % 2 == 1 {
190190
r >>= 1;
191191
}
192-
if !f(M::binary_operation(&self.d[r], &sm)) {
192+
if !f(&M::binary_operation(&self.d[r], &sm)) {
193193
while r < self.size {
194194
r = 2 * r + 1;
195195
let res = M::binary_operation(&self.d[r], &sm);
196-
if f(res.clone()) {
196+
if f(&res) {
197197
sm = res;
198198
r -= 1;
199199
}
@@ -285,12 +285,12 @@ mod tests {
285285
base.iter().max().copied().unwrap_or(i32::min_value())
286286
);
287287
for k in 0..=10 {
288-
let f = |x| x < k;
288+
let f = |&x: &i32| x < k;
289289
for i in 0..=n {
290290
assert_eq!(
291291
Some(segtree.max_right(i, f)),
292292
(i..=n)
293-
.filter(|&j| f(base[i..j]
293+
.filter(|&j| f(&base[i..j]
294294
.iter()
295295
.max()
296296
.copied()
@@ -302,7 +302,7 @@ mod tests {
302302
assert_eq!(
303303
Some(segtree.min_left(j, f)),
304304
(0..=j)
305-
.filter(|&i| f(base[i..j]
305+
.filter(|&i| f(&base[i..j]
306306
.iter()
307307
.max()
308308
.copied()

0 commit comments

Comments
 (0)