Skip to content

Commit 707b56d

Browse files
Added example for Practice2-L
1 parent 4878021 commit 707b56d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![allow(clippy::many_single_char_names)]
2+
use ac_library_rs::{LazySegtree, MapMonoid, Monoid};
3+
use std::io::Read;
4+
use std::iter;
5+
6+
struct M;
7+
impl Monoid for M {
8+
type S = (u64, u64, u64);
9+
fn identity() -> Self::S {
10+
(0, 0, 0)
11+
}
12+
fn binary_operation((a, b, c): Self::S, (d, e, f): Self::S) -> Self::S {
13+
(a + d, b + e, c + f + b * d)
14+
}
15+
}
16+
struct F;
17+
impl MapMonoid for F {
18+
type M = M;
19+
type F = bool;
20+
21+
fn identity_map() -> Self::F {
22+
false
23+
}
24+
fn mapping(f: Self::F, (a, b, c): <M as Monoid>::S) -> <M as Monoid>::S {
25+
if f {
26+
// (a + b) * (a + b - 1) / 2 - a * (a - 1) / 2 - b * (b - 1) / 2 - c
27+
// = a * b - c
28+
(b, a, a * b - c)
29+
} else {
30+
(a, b, c)
31+
}
32+
}
33+
fn composition(f: Self::F, g: Self::F) -> Self::F {
34+
f ^ g
35+
}
36+
}
37+
38+
fn main() {
39+
let mut buf = String::new();
40+
std::io::stdin().read_to_string(&mut buf).unwrap();
41+
let mut input = buf.split_whitespace();
42+
43+
let n = input.next().unwrap().parse().unwrap();
44+
let q = input.next().unwrap().parse().unwrap();
45+
let mut segtree: LazySegtree<F> = iter::once((0, 0, 0))
46+
.chain(input.by_ref().take(n).map(|s| match s {
47+
"0" => (1, 0, 0),
48+
"1" => (0, 1, 0),
49+
_ => panic!(),
50+
}))
51+
.collect::<Vec<_>>()
52+
.into();
53+
for _ in 0..q {
54+
let t = input.next().unwrap().parse().unwrap();
55+
let l = input.next().unwrap().parse().unwrap();
56+
let r: usize = input.next().unwrap().parse().unwrap();
57+
match t {
58+
1 => segtree.apply_range(l, r + 1, true),
59+
2 => println!("{}", segtree.prod(l, r + 1).2),
60+
_ => {}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)