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 fn cum<T: Default + std::ops::AddAssign + Copy>( | |
| nums: impl IntoIterator<Item = T>, | |
| ) -> impl Iterator<Item = T> { | |
| nums.into_iter().scan(T::default(), |state, item| { | |
| *state += item; | |
| Some(*state) | |
| }) | |
| } | |
| pub fn cum2<T: Default + std::ops::AddAssign + Copy, NNS, NS>(nums: NNS) -> Vec<Vec<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
| #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | |
| pub struct AltSum { | |
| sum: i64, | |
| len: usize, | |
| } | |
| impl AltSum { | |
| pub fn empty() -> Self { | |
| Self { sum: 0, len: 0 } | |
| } |
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
| export type Paradoxical<A, R> = (f: Paradoxical<A, R>) => (a: A) => R; | |
| export const Y = <A, R>(f: (g: (a: A) => R) => (a: A) => R): ((a: A) => R) => | |
| ((x: Paradoxical<A, R>) => f((lazy) => x(x)(lazy)))( | |
| (x: Paradoxical<A, R>) => f((lazy) => x(x)(lazy)), | |
| ); |
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 fn inversions(nums: &[usize]) -> Vec<usize> { | |
| let len = nums.len(); | |
| let mut counts = SegTree::<Sum>::new(len); | |
| let mut inversions = Vec::with_capacity(len); | |
| for (i, &num) in nums.into_iter().enumerate() { | |
| let Sum(inv) = counts.query(0..num); | |
| inversions.push(i - inv); | |
| counts.insert(num, Sum(1)); | |
| } | |
| inversions |
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 fn longest_increasing_subsequence<T: Ord>(nums: impl IntoIterator<Item = T>) -> Vec<usize> { | |
| let mut nums = nums.into_iter(); | |
| let Some(first) = nums.next() else { | |
| return vec![]; | |
| }; | |
| let mut len_by_using = vec![1]; | |
| let mut sorted = vec![first]; | |
| for num in nums { | |
| let lower_bound = sorted.binary_search(&num).unwrap_or_else(|idx| idx); | |
| if lower_bound == sorted.len() { |
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
| #[inline] | |
| pub fn merge<T, L, R>(left: L, right: R) -> Merge<T, L::IntoIter, R::IntoIter> | |
| where | |
| T: PartialOrd, | |
| L: IntoIterator<Item = T>, | |
| R: IntoIterator<Item = T>, | |
| { | |
| Merge { | |
| left: left.into_iter().peekable(), | |
| right: right.into_iter().peekable(), |
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 fn strongly_connected_components(graph: &[Vec<usize>]) -> Vec<Vec<usize>> { | |
| let vertexes = graph.len(); | |
| let inv_graph = { | |
| let mut inv_graph = vec![vec![]; vertexes]; | |
| for (from, adj) in graph.into_iter().enumerate() { | |
| for &to in adj { | |
| inv_graph[to].push(from); | |
| } | |
| } | |
| inv_graph |
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::HashSet; | |
| /// 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)); | |
| /// } | |
| /// ``` |
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, Hash)] | |
| pub struct Field { | |
| // both of width and height must ≤ i64::MAX for safety | |
| width: u64, | |
| height: u64, | |
| } | |
| impl Field { | |
| pub fn new(width: u64, height: u64) -> Self { | |
| assert!( |
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(Clone, PartialEq, Eq, Hash)] | |
| struct Poly<const W: usize, const H: usize> { | |
| field: [[bool; W]; H], | |
| } | |
| impl<const W: usize, const H: usize> std::fmt::Debug for Poly<W, H> { | |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| writeln!(f, "\"\"\"")?; | |
| for y in 0..H { | |
| for x in 0..W { |