Open tipstar0125 opened 10 months ago
区間反転更新・区間転倒数取得
#[derive(Clone, Copy)]
struct Node {
zero: usize,
one: usize,
inv: usize,
}
struct InvMonoid;
impl Monoid for InvMonoid {
type S = Node;
fn identity() -> Self::S {
Node {
zero: 0,
one: 0,
inv: 0,
}
}
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
Node {
zero: a.zero + b.zero,
one: a.one + b.one,
inv: a.inv + b.inv + a.one * b.zero,
}
}
}
impl MapMonoid for InvMonoid {
type M = InvMonoid;
type F = bool;
fn identity_map() -> Self::F {
false
}
fn mapping(&f: &bool, &x: &Node) -> Node {
if f {
Node {
zero: x.one,
one: x.zero,
inv: x.zero * x.one - x.inv,
}
} else {
x
}
}
fn composition(&f: &bool, &g: &bool) -> bool {
f ^ g
}
}
区間反転更新・区間和取得
問題:https://mojacoder.app/users/loop0919/problems/flip-puzzle 解答1:https://mojacoder.app/users/loop0919/problems/flip-puzzle/submissions/5a9c1301-74b1-438e-b605-21c784ec6887 解答2:https://mojacoder.app/users/loop0919/problems/flip-puzzle/submissions/29deabb3-10d3-40e1-b939-a0fd51b13b99
#[derive(Clone, Copy)]
struct Node {
one: usize,
size: usize,
}
struct M;
impl Monoid for M {
type S = Node;
fn identity() -> Self::S {
Node { one: 0, size: 0 }
}
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
Node {
one: a.one + b.one,
size: a.size + b.size,
}
}
}
impl MapMonoid for M {
type M = M;
type F = bool;
fn identity_map() -> Self::F {
false
}
fn mapping(&f: &bool, &x: &Node) -> Node {
if f {
Node {
one: x.size - x.one,
size: x.size,
}
} else {
x
}
}
fn composition(&f: &bool, &g: &bool) -> bool {
f ^ g
}
}
#[derive(Clone, Copy)]
struct Node {
one: usize,
zero: usize,
}
struct M;
impl Monoid for M {
type S = Node;
fn identity() -> Self::S {
Node { one: 0, zero: 0 }
}
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
Node {
one: a.one + b.one,
zero: a.zero + b.zero,
}
}
}
impl MapMonoid for M {
type M = M;
type F = bool;
fn identity_map() -> Self::F {
false
}
fn mapping(&f: &bool, &x: &Node) -> Node {
if f {
Node {
one: x.zero,
zero: x.one,
}
} else {
x
}
}
fn composition(&f: &bool, &g: &bool) -> bool {
f ^ g
}
}
問題:https://atcoder.jp/contests/practice2/tasks/practice2_l 解答:https://atcoder.jp/contests/practice2/submissions/49333619 参考1:https://drken1215.hatenablog.com/entry/2023/11/18/030700 参考2:https://betrue12.hateblo.jp/entry/2020/09/22/194541 モノイドチートシート:https://betrue12.hateblo.jp/entry/2020/09/23/005940 ACL移植:https://github.com/tipstar0125/atcoder/issues/43#issuecomment-1891491608