|
| 1 | +use ac_library_rs::{LazySegtree, MapMonoid, ModInt998244353, Monoid}; |
| 2 | +use std::io::Read; |
| 3 | + |
| 4 | +type Mint = ModInt998244353; |
| 5 | +struct Sum; |
| 6 | +impl Monoid for Sum { |
| 7 | + type S = (Mint, usize); |
| 8 | + |
| 9 | + fn identity() -> Self::S { |
| 10 | + (0.into(), 0) |
| 11 | + } |
| 12 | + |
| 13 | + fn binary_operation((a, n): Self::S, (b, m): Self::S) -> Self::S { |
| 14 | + (a + b, n + m) |
| 15 | + } |
| 16 | +} |
| 17 | +struct Affine; |
| 18 | +impl MapMonoid for Affine { |
| 19 | + type M = Sum; |
| 20 | + type F = (Mint, Mint); |
| 21 | + |
| 22 | + fn identity_map() -> Self::F { |
| 23 | + (1.into(), 0.into()) |
| 24 | + } |
| 25 | + |
| 26 | + fn mapping((a, b): Self::F, (x, n): <Self::M as Monoid>::S) -> <Self::M as Monoid>::S { |
| 27 | + (a * x + b * Mint::new(n), n) |
| 28 | + } |
| 29 | + |
| 30 | + // a(cx + d) + b = (ac)x + (ad+b) |
| 31 | + fn composition((a, b): Self::F, (c, d): Self::F) -> Self::F { |
| 32 | + (a * c, a * d + b) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[allow(clippy::many_single_char_names)] |
| 37 | +fn main() { |
| 38 | + let mut buf = String::new(); |
| 39 | + std::io::stdin().read_to_string(&mut buf).unwrap(); |
| 40 | + let mut input = buf.split_whitespace(); |
| 41 | + |
| 42 | + let n = input.next().unwrap().parse().unwrap(); |
| 43 | + let q = input.next().unwrap().parse().unwrap(); |
| 44 | + let mut segtree: LazySegtree<Affine> = input |
| 45 | + .by_ref() |
| 46 | + .take(n) |
| 47 | + .map(|s| (s.parse().unwrap(), 1)) |
| 48 | + .collect::<Vec<_>>() |
| 49 | + .into(); |
| 50 | + for _ in 0..q { |
| 51 | + match input.next().unwrap().parse().unwrap() { |
| 52 | + 0 => { |
| 53 | + let l = input.next().unwrap().parse().unwrap(); |
| 54 | + let r = input.next().unwrap().parse().unwrap(); |
| 55 | + let b = input.next().unwrap().parse().unwrap(); |
| 56 | + let c = input.next().unwrap().parse().unwrap(); |
| 57 | + segtree.apply_range(l, r, (b, c)); |
| 58 | + } |
| 59 | + 1 => { |
| 60 | + let l = input.next().unwrap().parse().unwrap(); |
| 61 | + let r = input.next().unwrap().parse().unwrap(); |
| 62 | + println!("{}", segtree.prod(l, r).0); |
| 63 | + } |
| 64 | + _ => {} |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments