Skip to content

Commit

Permalink
wip example
Browse files Browse the repository at this point in the history
  • Loading branch information
mizar committed Mar 26, 2023
1 parent fd82892 commit 6cece28
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 82 deletions.
52 changes: 33 additions & 19 deletions examples/library-checker-convolution-mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
#[macro_use]
extern crate proconio as _;

// https://judge.yosupo.jp/problem/convolution_mod
use ac_library_rs::{convolution, modint::ModInt998244353 as Mint};
use std::fmt;
use std::io::prelude::*;

#[allow(clippy::many_single_char_names)]
fn main() {
input! {
n: usize,
m: usize,
a: [Mint; n],
b: [Mint; m],
}
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 a = input
.by_ref()
.take(n)
.map(str::parse)
.filter_map(Result::ok)
.collect::<Vec<Mint>>();
let b = input
.by_ref()
.take(m)
.map(str::parse)
.filter_map(Result::ok)
.collect::<Vec<Mint>>();

print_oneline(convolution::convolution(&a, &b));
}

fn print_oneline<I: IntoIterator<Item = T>, T: fmt::Display>(values: I) {
println!(
"{}",
values
.into_iter()
.map(|v| v.to_string())
.collect::<Vec<_>>()
.join(" "),
)
fn print_oneline<I: IntoIterator<Item = T>, T: std::fmt::Display>(values: I) {
let mut iter = values.into_iter();
let iter_len = iter.size_hint().0;
let mut buf = Vec::with_capacity(iter_len * 10);
if let Some(v) = iter.next() {
buf.extend(v.to_string().bytes());
};
for v in iter {
buf.extend(std::iter::once(b' '));
buf.extend(v.to_string().bytes());
}
Write::write_all(&mut std::io::stdout(), &buf).unwrap();
}
40 changes: 24 additions & 16 deletions examples/library-checker-static-range-sum.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
#[macro_use]
extern crate proconio as _;
#[macro_use]
extern crate proconio_derive as _;

// https://judge.yosupo.jp/problem/static_range_sum
use ac_library_rs::fenwicktree::FenwickTree;
use std::io::{prelude::*, BufWriter};

#[allow(clippy::many_single_char_names)]
#[allow(clippy::needless_collect)]
#[fastout]
fn main() {
input! {
n: usize,
q: usize,
r#as: [u64; n],
lrs: [(usize, usize); q],
}
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let n = input.next().unwrap().parse().unwrap();
let q: usize = input.next().unwrap().parse().unwrap();

let mut fenwick = FenwickTree::new(n, 0);
for (i, a) in r#as.into_iter().enumerate() {
let mut fenwick = FenwickTree::<u64>::new(n);
for (i, a) in input
.by_ref()
.take(n)
.map(str::parse)
.filter_map(Result::ok)
.enumerate()
{
fenwick.add(i, a);
}
for (l, r) in lrs {
println!("{}", fenwick.sum(l, r));
for _ in 0..q {
let l = input.next().unwrap().parse().unwrap();
let r = input.next().unwrap().parse().unwrap();
writeln!(&mut out, "{}", fenwick.sum(l, r)).unwrap();
}
}
28 changes: 17 additions & 11 deletions examples/library-checker-sum-of-floor-of-linear.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#[macro_use]
extern crate proconio as _;
#[macro_use]
extern crate proconio_derive as _;

// https://judge.yosupo.jp/problem/sum_of_floor_of_linear
use ac_library_rs::math;
use std::io::{prelude::*, BufWriter};

#[fastout]
#[allow(clippy::many_single_char_names)]
fn main() {
input! {
nmabs: [(u64, u64, u64, u64)],
}
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let t: usize = input.next().unwrap().parse().unwrap();

for (n, m, a, b) in nmabs {
println!("{}", math::floor_sum(n, m, a, b));
for _ in 0..t {
let n: u64 = input.next().unwrap().parse().unwrap();
let m: u64 = input.next().unwrap().parse().unwrap();
let a: u64 = input.next().unwrap().parse().unwrap();
let b: u64 = input.next().unwrap().parse().unwrap();
writeln!(&mut out, "{}", math::floor_sum(n, m, a, b)).unwrap();
}
}
29 changes: 20 additions & 9 deletions examples/library-checker-unionfind.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
#[macro_use]
extern crate proconio as _;

// https://atcoder.jp/contests/practice2/tasks/practice2_a
use ac_library_rs::dsu::Dsu;
use std::io::{prelude::*, BufWriter};

#[allow(clippy::many_single_char_names)]
fn main() {
input! {
n: usize,
queries: [(u8, usize, usize)],
}
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let n: usize = input.next().unwrap().parse().unwrap();
let q: usize = input.next().unwrap().parse().unwrap();

let mut dsu = Dsu::new(n);
for (kind, u, v) in queries {

for _ in 0..q {
let kind: u8 = input.next().unwrap().parse().unwrap();
let u: usize = input.next().unwrap().parse().unwrap();
let v: usize = input.next().unwrap().parse().unwrap();
match kind {
0 => {
dsu.merge(u, v);
}
1 => println!("{}", u8::from(dsu.same(u, v))),
1 => {
writeln!(&mut out, "{}", u8::from(dsu.same(u, v))).unwrap();
}
_ => unreachable!(),
}
}
Expand Down
10 changes: 7 additions & 3 deletions examples/practice2_d_maxflow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// https://atcoder.jp/contests/practice2/tasks/practice2_d
use ac_library_rs::MfGraph;
use std::io::Read;
use std::io::{prelude::*, BufWriter};

#[allow(clippy::many_single_char_names)]
#[allow(clippy::needless_range_loop)]
Expand All @@ -10,6 +11,9 @@ fn main() {
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let n: usize = input.next().unwrap().parse().unwrap();
let m: usize = input.next().unwrap().parse().unwrap();
let mut s = vec![vec![false; N]; N];
Expand Down Expand Up @@ -40,7 +44,7 @@ fn main() {
}
}
}
println!("{}", graph.flow(0, 1));
writeln!(&mut out, "{}", graph.flow(0, 1)).ok();
let mut res: Vec<Vec<_>> = (1..=n)
.map(|i| (1..=m).map(|j| if s[i][j] { '.' } else { '#' }).collect())
.collect();
Expand All @@ -59,6 +63,6 @@ fn main() {
}
}
for s in res {
println!("{}", s.iter().collect::<String>());
writeln!(&mut out, "{}", s.iter().collect::<String>()).unwrap();
}
}
19 changes: 12 additions & 7 deletions examples/practice2_j_segment_tree.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// https://atcoder.jp/contests/practice2/tasks/practice2_j
use ac_library_rs::{Max, Segtree};
use std::io::Read;
use std::io::{prelude::*, BufWriter};
use std::iter::FromIterator;

#[allow(clippy::many_single_char_names)]
fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let n: usize = input.next().unwrap().parse().unwrap();
let q: usize = input.next().unwrap().parse().unwrap();
let mut segtree = Segtree::<Max<i32>>::new(n + 1);
for i in 1..=n {
segtree.set(i, input.next().unwrap().parse().unwrap());
}
let mut segtree = Segtree::<Max<i32>>::from_iter(
std::iter::once(0).chain((1..=n).map(|_| input.next().unwrap().parse().unwrap())),
);
for _ in 0..q {
match input.next().unwrap().parse().unwrap() {
1 => {
Expand All @@ -22,12 +27,12 @@ fn main() {
2 => {
let l = input.next().unwrap().parse().unwrap();
let r: usize = input.next().unwrap().parse().unwrap();
println!("{}", segtree.prod(l, r + 1));
writeln!(&mut out, "{}", segtree.prod(l, r + 1)).ok();
}
3 => {
let x = input.next().unwrap().parse().unwrap();
let v = input.next().unwrap().parse().unwrap();
println!("{}", segtree.max_right(x, |a| a < &v))
writeln!(&mut out, "{}", segtree.max_right(x, |a| a < &v)).unwrap();
}
_ => {}
}
Expand Down
17 changes: 9 additions & 8 deletions examples/practice2_k_range_affine_range_sum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// https://atcoder.jp/contests/practice2/tasks/practice2_k
use ac_library_rs::{LazySegtree, MapMonoid, ModInt998244353, Monoid};
use std::io::Read;
use std::io::{prelude::*, BufWriter};
use std::iter::FromIterator;

type Mint = ModInt998244353;
struct Sum;
Expand Down Expand Up @@ -39,14 +41,13 @@ fn main() {
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let n = input.next().unwrap().parse().unwrap();
let q = input.next().unwrap().parse().unwrap();
let mut segtree: LazySegtree<Affine> = input
.by_ref()
.take(n)
.map(|s| (s.parse().unwrap(), 1))
.collect::<Vec<_>>()
.into();
let mut segtree: LazySegtree<Affine> =
LazySegtree::from_iter(input.by_ref().take(n).map(|s| (s.parse().unwrap(), 1)));
for _ in 0..q {
match input.next().unwrap().parse().unwrap() {
0 => {
Expand All @@ -59,7 +60,7 @@ fn main() {
1 => {
let l = input.next().unwrap().parse().unwrap();
let r = input.next().unwrap().parse().unwrap();
println!("{}", segtree.prod(l, r).0);
writeln!(&mut out, "{}", segtree.prod(l, r).0).unwrap();
}
_ => {}
}
Expand Down
26 changes: 17 additions & 9 deletions examples/practice2_l_lazy_segment_tree.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// https://atcoder.jp/contests/practice2/tasks/practice2_l
#![allow(clippy::many_single_char_names)]
use ac_library_rs::{LazySegtree, MapMonoid, Monoid};
use std::io::Read;
use std::iter;
use std::io::{prelude::*, BufWriter};
use std::iter::FromIterator;

struct M;
impl Monoid for M {
Expand Down Expand Up @@ -35,28 +36,35 @@ impl MapMonoid for F {
}
}

#[allow(clippy::many_single_char_names)]
fn main() {
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf).unwrap();
let mut input = buf.split_whitespace();

let out = std::io::stdout();
let mut out = BufWriter::new(out.lock());

let n = input.next().unwrap().parse().unwrap();
let q = input.next().unwrap().parse().unwrap();
let mut segtree: LazySegtree<F> = iter::once((0, 0, 0))
.chain(input.by_ref().take(n).map(|s| match s {
let mut segtree: LazySegtree<F> = LazySegtree::from_iter(std::iter::once((0, 0, 0)).chain(
input.by_ref().take(n).map(|s| match s {
"0" => (1, 0, 0),
"1" => (0, 1, 0),
_ => panic!(),
}))
.collect::<Vec<_>>()
.into();
}),
));
for _ in 0..q {
let t = input.next().unwrap().parse().unwrap();
let l = input.next().unwrap().parse().unwrap();
let r: usize = input.next().unwrap().parse().unwrap();
match t {
1 => segtree.apply_range(l, r + 1, true),
2 => println!("{}", segtree.prod(l, r + 1).2),
1 => {
segtree.apply_range(l, r + 1, true);
}
2 => {
writeln!(&mut out, "{}", segtree.prod(l, r + 1).2).unwrap();
}
_ => {}
}
}
Expand Down

0 comments on commit 6cece28

Please sign in to comment.