From 01821323c700a51914a4f2d29e0f5f11e287705d Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Tue, 15 Sep 2020 21:33:03 +0900 Subject: [PATCH 01/10] Add a sample code for A - Disjoint Set Union --- examples/practice2_a_disjoint_set_union.rs | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 examples/practice2_a_disjoint_set_union.rs diff --git a/examples/practice2_a_disjoint_set_union.rs b/examples/practice2_a_disjoint_set_union.rs new file mode 100644 index 0000000..e92df64 --- /dev/null +++ b/examples/practice2_a_disjoint_set_union.rs @@ -0,0 +1,23 @@ +use ac_library_rs::Dsu; +use std::io::Read; + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n = input.next().unwrap().parse().unwrap(); + let mut dsu = Dsu::new(n); + for _ in 0..input.next().unwrap().parse().unwrap() { + let t = input.next().unwrap().parse().unwrap(); + let u = input.next().unwrap().parse().unwrap(); + let v = input.next().unwrap().parse().unwrap(); + match t { + 0 => { + dsu.merge(u, v); + } + 1 => println!("{}", dsu.same(u, v) as i32), + _ => {} + } + } +} From c83ee726bf89953d4d977eb072e7607d1901e676 Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Tue, 15 Sep 2020 22:38:25 +0900 Subject: [PATCH 02/10] Add a sample code for B - Fenwick Tree --- examples/practice2_b_fenwick_tree.rs | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/practice2_b_fenwick_tree.rs diff --git a/examples/practice2_b_fenwick_tree.rs b/examples/practice2_b_fenwick_tree.rs new file mode 100644 index 0000000..f6641bd --- /dev/null +++ b/examples/practice2_b_fenwick_tree.rs @@ -0,0 +1,31 @@ +use ac_library_rs::FenwickTree; +use std::io::Read; + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n = input.next().unwrap().parse().unwrap(); + let q = input.next().unwrap().parse().unwrap(); + let mut tree = FenwickTree::::new(n, 0); + for i in 0..n { + let a: u64 = input.next().unwrap().parse().unwrap(); + tree.add(i, a); + } + for _ in 0..q { + match input.next().unwrap().parse().unwrap() { + 0 => { + let p = input.next().unwrap().parse().unwrap(); + let x: u64 = input.next().unwrap().parse().unwrap(); + tree.add(p, x); + } + 1 => { + let l = input.next().unwrap().parse().unwrap(); + let r = input.next().unwrap().parse().unwrap(); + println!("{}", tree.sum(l, r)); + } + _ => {} + } + } +} From 091376b88cc40220fca7c749b13ff58abf3a93d5 Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Wed, 16 Sep 2020 01:12:58 +0900 Subject: [PATCH 03/10] Add a sample code for C - Floor Sum --- examples/practice2_c_floor_sum.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 examples/practice2_c_floor_sum.rs diff --git a/examples/practice2_c_floor_sum.rs b/examples/practice2_c_floor_sum.rs new file mode 100644 index 0000000..682038a --- /dev/null +++ b/examples/practice2_c_floor_sum.rs @@ -0,0 +1,16 @@ +use ac_library_rs::floor_sum; +use std::io::Read; + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + for _ in 0..input.next().unwrap().parse().unwrap() { + let n = input.next().unwrap().parse().unwrap(); + let m = input.next().unwrap().parse().unwrap(); + let a = input.next().unwrap().parse().unwrap(); + let b = input.next().unwrap().parse().unwrap(); + println!("{}", floor_sum(n, m, a, b)); + } +} From a4904ab2f610096d975961f14481597096c3b183 Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Wed, 16 Sep 2020 02:53:50 +0900 Subject: [PATCH 04/10] Add a sample code for E - MinCostFlow --- examples/practice2_e_mincostflow.rs | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/practice2_e_mincostflow.rs diff --git a/examples/practice2_e_mincostflow.rs b/examples/practice2_e_mincostflow.rs new file mode 100644 index 0000000..15a665a --- /dev/null +++ b/examples/practice2_e_mincostflow.rs @@ -0,0 +1,42 @@ +use ac_library_rs::MinCostFlowGraph; +use std::io::Read; + +const MAX: i64 = 1_000_000_000; + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n = input.next().unwrap().parse().unwrap(); + let k = input.next().unwrap().parse().unwrap(); + let a: Vec> = (0..n) + .map(|_| input.by_ref().take(n).map(|s| s.parse().unwrap()).collect()) + .collect(); + + let mut graph = MinCostFlowGraph::new(102); + for i in 0..n { + for j in 0..n { + graph.add_edge(i, 50 + j, 1, MAX - a[i][j]); + } + } + for i in 0..n { + graph.add_edge(100, i, k, 0); + graph.add_edge(50 + i, 101, k, 0); + } + graph.add_edge(100, 101, n as i64 * k, MAX); + + let (max_flow, min_cost) = graph.flow(100, 101, n as i64 * k); + println!("{}", max_flow * MAX - min_cost); + + (0..n) + .map(|i| { + (0..n) + .map(|j| match graph.get_edge(i * n + j).flow { + 1 => 'X', + _ => '.', + }) + .collect() + }) + .for_each(|s: String| println!("{}", s)); +} From e52eaf4393a0143f85db234c34f90897db095f4b Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Wed, 16 Sep 2020 13:56:43 +0900 Subject: [PATCH 05/10] Add a sample code for G - SCC --- examples/practice2_g_scc.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/practice2_g_scc.rs diff --git a/examples/practice2_g_scc.rs b/examples/practice2_g_scc.rs new file mode 100644 index 0000000..d4b281c --- /dev/null +++ b/examples/practice2_g_scc.rs @@ -0,0 +1,26 @@ +use ac_library_rs::SccGraph; +use std::io::Read; + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n = input.next().unwrap().parse().unwrap(); + let m = input.next().unwrap().parse().unwrap(); + let mut graph = SccGraph::new(n); + for _ in 0..m { + let a = input.next().unwrap().parse().unwrap(); + let b = input.next().unwrap().parse().unwrap(); + graph.add_edge(a, b); + } + let scc = graph.scc(); + println!("{}", scc.len()); + for cc in scc { + print!("{}", cc.len()); + for v in cc { + print!(" {}", v); + } + println!(); + } +} From 3513bd4432bbec1f8f41e5e47387a4b8a550b727 Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Wed, 16 Sep 2020 15:01:35 +0900 Subject: [PATCH 06/10] Add a sample code for H - Two SAT --- examples/practice2_h_two_sat.rs | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/practice2_h_two_sat.rs diff --git a/examples/practice2_h_two_sat.rs b/examples/practice2_h_two_sat.rs new file mode 100644 index 0000000..07f492e --- /dev/null +++ b/examples/practice2_h_two_sat.rs @@ -0,0 +1,35 @@ +use ac_library_rs::TwoSat; +use std::io::Read; + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n = input.next().unwrap().parse().unwrap(); + let d = input.next().unwrap().parse().unwrap(); + let xs = (0..2 * n) + .map(|_| input.next().unwrap().parse().unwrap()) + .collect::>(); + + let mut sat = TwoSat::new(2*n); + for i in 0..2 * n { + sat.add_clause(i, i % 2 == 0, i ^ 1, i % 2 == 0); + } + for (i, x) in xs.iter().enumerate() { + for (j, y) in xs[..i].iter().enumerate() { + if (x - y).abs() < d { + sat.add_clause(i, false, j, false); + } + } + } + if sat.satisfiable() { + println!("Yes"); + let ans = sat.answer(); + for i in 0..n { + println!("{}", xs[2 * i + ans[2 * i + 1] as usize]); + } + } else { + println!("No"); + } +} From d01c6ac69ec5b08baf6eeb57a29b31a14866b73b Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:05:15 +0900 Subject: [PATCH 07/10] Add a sample code for I - Number of Substrings --- examples/practice2_i_number_of_substrings.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 examples/practice2_i_number_of_substrings.rs diff --git a/examples/practice2_i_number_of_substrings.rs b/examples/practice2_i_number_of_substrings.rs new file mode 100644 index 0000000..579346f --- /dev/null +++ b/examples/practice2_i_number_of_substrings.rs @@ -0,0 +1,16 @@ +use ac_library_rs::{lcp_array, suffix_array}; +use std::io::Read; +use std::iter; + +fn main() { + let mut s = String::new(); + std::io::stdin().read_to_string(&mut s).unwrap(); + let s = s.trim(); + let suffix_array = suffix_array(s); + let ans: u64 = iter::once(0) + .chain(lcp_array(s, &suffix_array)) + .zip(suffix_array) + .map(|(c, i)| (s.len() - i - c) as u64) + .sum(); + println!("{}", ans); +} From 57d2af7b87f6bf82a69e5298c7bf43f50cd24bc0 Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:06:39 +0900 Subject: [PATCH 08/10] cargo fmt & clippy --- examples/practice2_e_mincostflow.rs | 1 + examples/practice2_h_two_sat.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/practice2_e_mincostflow.rs b/examples/practice2_e_mincostflow.rs index 15a665a..cf3bb86 100644 --- a/examples/practice2_e_mincostflow.rs +++ b/examples/practice2_e_mincostflow.rs @@ -3,6 +3,7 @@ use std::io::Read; const MAX: i64 = 1_000_000_000; +#[allow(clippy::needless_range_loop)] fn main() { let mut buf = String::new(); std::io::stdin().read_to_string(&mut buf).unwrap(); diff --git a/examples/practice2_h_two_sat.rs b/examples/practice2_h_two_sat.rs index 07f492e..ec251c8 100644 --- a/examples/practice2_h_two_sat.rs +++ b/examples/practice2_h_two_sat.rs @@ -12,7 +12,7 @@ fn main() { .map(|_| input.next().unwrap().parse().unwrap()) .collect::>(); - let mut sat = TwoSat::new(2*n); + let mut sat = TwoSat::new(2 * n); for i in 0..2 * n { sat.add_clause(i, i % 2 == 0, i ^ 1, i % 2 == 0); } From 22078b5fc3b35015519a81c68bbb127c2faff119 Mon Sep 17 00:00:00 2001 From: TonalidadeHidrica <47710717+TonalidadeHidrica@users.noreply.github.com> Date: Sat, 3 Oct 2020 01:41:11 +0900 Subject: [PATCH 09/10] Add link to problem statement for each sample code Thanks @matsu7874. Co-authored-by: Kentaro Matsumoto --- examples/practice2_a_disjoint_set_union.rs | 1 + examples/practice2_b_fenwick_tree.rs | 1 + examples/practice2_c_floor_sum.rs | 1 + examples/practice2_e_mincostflow.rs | 1 + examples/practice2_g_scc.rs | 1 + examples/practice2_h_two_sat.rs | 1 + examples/practice2_i_number_of_substrings.rs | 1 + 7 files changed, 7 insertions(+) diff --git a/examples/practice2_a_disjoint_set_union.rs b/examples/practice2_a_disjoint_set_union.rs index e92df64..ea25de5 100644 --- a/examples/practice2_a_disjoint_set_union.rs +++ b/examples/practice2_a_disjoint_set_union.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_a use ac_library_rs::Dsu; use std::io::Read; diff --git a/examples/practice2_b_fenwick_tree.rs b/examples/practice2_b_fenwick_tree.rs index f6641bd..2a003f6 100644 --- a/examples/practice2_b_fenwick_tree.rs +++ b/examples/practice2_b_fenwick_tree.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_b use ac_library_rs::FenwickTree; use std::io::Read; diff --git a/examples/practice2_c_floor_sum.rs b/examples/practice2_c_floor_sum.rs index 682038a..78f5657 100644 --- a/examples/practice2_c_floor_sum.rs +++ b/examples/practice2_c_floor_sum.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_c use ac_library_rs::floor_sum; use std::io::Read; diff --git a/examples/practice2_e_mincostflow.rs b/examples/practice2_e_mincostflow.rs index cf3bb86..c8a237c 100644 --- a/examples/practice2_e_mincostflow.rs +++ b/examples/practice2_e_mincostflow.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_e use ac_library_rs::MinCostFlowGraph; use std::io::Read; diff --git a/examples/practice2_g_scc.rs b/examples/practice2_g_scc.rs index d4b281c..aa67431 100644 --- a/examples/practice2_g_scc.rs +++ b/examples/practice2_g_scc.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_g use ac_library_rs::SccGraph; use std::io::Read; diff --git a/examples/practice2_h_two_sat.rs b/examples/practice2_h_two_sat.rs index ec251c8..b80b597 100644 --- a/examples/practice2_h_two_sat.rs +++ b/examples/practice2_h_two_sat.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_h use ac_library_rs::TwoSat; use std::io::Read; diff --git a/examples/practice2_i_number_of_substrings.rs b/examples/practice2_i_number_of_substrings.rs index 579346f..50806f1 100644 --- a/examples/practice2_i_number_of_substrings.rs +++ b/examples/practice2_i_number_of_substrings.rs @@ -1,3 +1,4 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_i use ac_library_rs::{lcp_array, suffix_array}; use std::io::Read; use std::iter; From 3390137fa1b37b7a1730e63762ce41d83a4cf81e Mon Sep 17 00:00:00 2001 From: Mizar Date: Wed, 29 Mar 2023 17:05:46 +0200 Subject: [PATCH 10/10] Add a sample code for F - Convolution --- examples/practice2_f_convolution.rs | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/practice2_f_convolution.rs diff --git a/examples/practice2_f_convolution.rs b/examples/practice2_f_convolution.rs new file mode 100644 index 0000000..7cb3276 --- /dev/null +++ b/examples/practice2_f_convolution.rs @@ -0,0 +1,39 @@ +// Check Problem Statement via https://atcoder.jp/contests/practice2/tasks/practice2_f +use ac_library_rs::{convolution, modint::ModInt998244353 as Mint}; +use std::io::prelude::*; + +pub fn main() { + let mut buf = String::new(); + std::io::stdin().read_to_string(&mut buf).unwrap(); + let mut input = buf.split_whitespace(); + + let n: usize = input.next().unwrap().parse().unwrap(); + let m: usize = input.next().unwrap().parse().unwrap(); + let a: Vec = input + .by_ref() + .take(n) + .map(str::parse) + .map(Result::unwrap) + .collect(); + let b: Vec = input + .by_ref() + .take(m) + .map(str::parse) + .map(Result::unwrap) + .collect(); + + print_oneline(convolution::convolution(&a, &b)); +} + +fn print_oneline, T: std::fmt::Display>(values: I) { + let out = std::io::stdout(); + let mut out = std::io::BufWriter::new(out.lock()); + for (i, v) in values.into_iter().enumerate() { + if i == 0 { + write!(&mut out, "{}", v).unwrap(); + } else { + write!(&mut out, " {}", v).unwrap(); + } + } + writeln!(&mut out).unwrap(); +}