This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Monoid must satisfy: | |
| /// ```rs | |
| /// fn test<M: Monoid>(m: M, n: M, l: M) { | |
| /// assert_eq!(m.op(M::ID), m); | |
| /// assert_eq!(M::ID.op(m), m); | |
| /// assert_eq!(m.op(n.op(l)), m.op(n).op(l)); | |
| /// } | |
| /// ``` | |
| pub trait Monoid: Copy + Eq { | |
| const ID: Self; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait ChMax: Sized + PartialOrd { | |
| fn ch_max(&mut self, other: Self) { | |
| if *self < other { | |
| *self = other; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// It represents parent if positive, size of tree if negative | |
| #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] | |
| struct UnionFindIndex(isize); | |
| impl UnionFindIndex { | |
| fn index(index: usize) -> Self { | |
| debug_assert!(index < (std::isize::MAX as usize)); | |
| Self(index as isize) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use num::{Integer, One, Zero}; | |
| pub trait GcdNum: Copy + Integer + One + Zero + std::ops::SubAssign {} | |
| pub fn gcd_ext<T: GcdNum>( | |
| a: T, | |
| b: T, | |
| ) -> (T, T) { | |
| fn inner<T: GcdNum>( | |
| a: T, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Monoid must satisfy: | |
| /// ```rs | |
| /// fn test<M: Monoid>(m: M, n: M, l: M) { | |
| /// assert_eq!(m.op(M::ID), m); | |
| /// assert_eq!(M::ID.op(m), m); | |
| /// assert_eq!(m.op(n.op(l)), m.op(n).op(l)); | |
| /// } | |
| /// ``` | |
| pub trait Monoid: Copy + Eq { | |
| const ID: Self; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | |
| struct ChebyshevPoint(i64, i64); | |
| impl ChebyshevPoint { | |
| fn from_manhattan(x: i64, y: i64) -> Self { | |
| Self(x - y, x + y) | |
| } | |
| fn manhattan_dist(self, other: Self) -> u64 { | |
| self.0.abs_diff(other.0).max(self.1.abs_diff(other.1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// A monoid with inverse and zero element. | |
| pub trait InvZeroMonoid: From<u64> + Copy + Sized { | |
| /// for all x; x.op(ZERO) == ZERO | |
| const ZERO: Self; | |
| /// for all x; x.op(ONE) == x | |
| const ONE: Self; | |
| /// for all x, y, z; x.op(y).op(z) == x.op(y.op(z)) | |
| fn op(self, rhs: Self) -> Self; | |
| /// for all x; if x != ZERO then x.op(x.inv()) == ONE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::{ | |
| collections::BinaryHeap, | |
| ops::*, | |
| }; | |
| struct SlopeTrick<T> { | |
| left: BinaryHeap<T>, | |
| right: BinaryHeap<T>, | |
| bias: T, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct Digits { | |
| num: u64, | |
| radix: u64, | |
| } | |
| impl Digits { | |
| pub fn new(num: u64, radix: u64) -> Self { | |
| assert!(0 < radix, "radix must be greater than zero"); | |
| Self { num, radix } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use num::{One, Zero}; | |
| /// An integer mod n. | |
| #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
| pub struct ModInt<const MOD: u64>(u64); | |
| impl<const MOD: u64> ModInt<MOD> { | |
| pub fn as_u64(self) -> u64 { | |
| self.0 | |
| } |