Skip to content

Commit b27d10d

Browse files
Added sample for J - Segment Tree
1 parent dc64b31 commit b27d10d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

examples/practice2_j_segment_tree.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use ac_library_rs::{Max, Segtree};
2+
use std::io::Read;
3+
4+
fn main() {
5+
let mut buf = String::new();
6+
std::io::stdin().read_to_string(&mut buf).unwrap();
7+
let mut input = buf.split_whitespace();
8+
9+
let n: usize = input.next().unwrap().parse().unwrap();
10+
let q: usize = input.next().unwrap().parse().unwrap();
11+
let mut segtree = Segtree::<Max<u32>>::new(n+1);
12+
for i in 1..=n {
13+
segtree.set(i, input.next().unwrap().parse().unwrap());
14+
}
15+
for _ in 0..q {
16+
match input.next().unwrap().parse().unwrap() {
17+
1 => {
18+
let x = input.next().unwrap().parse().unwrap();
19+
let v = input.next().unwrap().parse().unwrap();
20+
segtree.set(x, v);
21+
}
22+
2 => {
23+
let l = input.next().unwrap().parse().unwrap();
24+
let r: usize = input.next().unwrap().parse().unwrap();
25+
println!("{}", segtree.prod(l, r+1));
26+
}
27+
3 => {
28+
let x = input.next().unwrap().parse().unwrap();
29+
let v = input.next().unwrap().parse().unwrap();
30+
println!("{}", segtree.max_right(x, |a| a < v))
31+
}
32+
_ => {}
33+
}
34+
}
35+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use modint::{
2727
ModInt998244353, Modulus, RemEuclidU32, StaticModInt,
2828
};
2929
pub use scc::SccGraph;
30-
pub use segtree::{Monoid, Segtree};
30+
pub use segtree::{Monoid, Segtree, Max};
3131
pub use string::{
3232
lcp_array, lcp_array_arbitrary, suffix_array, suffix_array_arbitrary, suffix_array_manual,
3333
z_algorithm, z_algorithm_arbitrary,

0 commit comments

Comments
 (0)