From eae05f40a5a6c64950543b888c11f2654f67fab2 Mon Sep 17 00:00:00 2001 From: koba-e964 <3303362+koba-e964@users.noreply.github.com> Date: Sun, 28 Apr 2024 09:52:50 +0900 Subject: [PATCH] Add atcoder/abc351/a.rs atcoder/abc351/b.rs atcoder/abc351/c.rs atcoder/abc351/d.rs atcoder/abc351/e.rs atcoder/abc351/f.rs atcoder/abc351/remain.txt --- atcoder/abc351/a.rs | 40 +++++++++++++ atcoder/abc351/b.rs | 49 ++++++++++++++++ atcoder/abc351/c.rs | 47 ++++++++++++++++ atcoder/abc351/d.rs | 115 ++++++++++++++++++++++++++++++++++++++ atcoder/abc351/e.rs | 72 ++++++++++++++++++++++++ atcoder/abc351/f.rs | 108 +++++++++++++++++++++++++++++++++++ atcoder/abc351/remain.txt | 1 + 7 files changed, 432 insertions(+) create mode 100644 atcoder/abc351/a.rs create mode 100644 atcoder/abc351/b.rs create mode 100644 atcoder/abc351/c.rs create mode 100644 atcoder/abc351/d.rs create mode 100644 atcoder/abc351/e.rs create mode 100644 atcoder/abc351/f.rs create mode 100644 atcoder/abc351/remain.txt diff --git a/atcoder/abc351/a.rs b/atcoder/abc351/a.rs new file mode 100644 index 00000000..c09d45a5 --- /dev/null +++ b/atcoder/abc351/a.rs @@ -0,0 +1,40 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + input! { + a: [i32; 9], + b: [i32; 8], + } + let asum: i32 = a.iter().sum(); + let bsum: i32 = b.iter().sum(); + println!("{}", asum - bsum + 1); +} diff --git a/atcoder/abc351/b.rs b/atcoder/abc351/b.rs new file mode 100644 index 00000000..8c3f6128 --- /dev/null +++ b/atcoder/abc351/b.rs @@ -0,0 +1,49 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, chars) => { + read_value!($next, String).chars().collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + input! { + n: usize, + a: [chars; n], + b: [chars; n], + } + for i in 0..n { + for j in 0..n { + if a[i][j] != b[i][j] { + println!("{} {}", i + 1, j + 1); + return; + } + } + } +} diff --git a/atcoder/abc351/c.rs b/atcoder/abc351/c.rs new file mode 100644 index 00000000..d26db6fc --- /dev/null +++ b/atcoder/abc351/c.rs @@ -0,0 +1,47 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn main() { + input! { + n: usize, + a: [i32; n], + } + let mut s = vec![]; + for a in a { + s.push(a); + while s.len() >= 2 && s[s.len() - 2] == s[s.len() - 1] { + s.pop(); + let x = s.pop().unwrap(); + s.push(x + 1); + } + } + println!("{}", s.len()); +} diff --git a/atcoder/abc351/d.rs b/atcoder/abc351/d.rs new file mode 100644 index 00000000..713a0adf --- /dev/null +++ b/atcoder/abc351/d.rs @@ -0,0 +1,115 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, chars) => { + read_value!($next, String).chars().collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +// Union-Find tree. +// Verified by https://atcoder.jp/contests/pakencamp-2019-day3/submissions/9253305 +struct UnionFind { disj: Vec, rank: Vec } + +impl UnionFind { + fn new(n: usize) -> Self { + let disj = (0..n).collect(); + UnionFind { disj: disj, rank: vec![1; n] } + } + fn root(&mut self, x: usize) -> usize { + if x != self.disj[x] { + let par = self.disj[x]; + let r = self.root(par); + self.disj[x] = r; + } + self.disj[x] + } + fn unite(&mut self, x: usize, y: usize) { + let mut x = self.root(x); + let mut y = self.root(y); + if x == y { return } + if self.rank[x] > self.rank[y] { + std::mem::swap(&mut x, &mut y); + } + self.disj[x] = y; + self.rank[y] += self.rank[x]; + } + #[allow(unused)] + fn is_same_set(&mut self, x: usize, y: usize) -> bool { + self.root(x) == self.root(y) + } + #[allow(unused)] + fn size(&mut self, x: usize) -> usize { + let x = self.root(x); + self.rank[x] + } +} + +fn main() { + input! { + h: usize, w: usize, + s: [chars; h], + } + let mut ma = 1; + let mut uf = UnionFind::new(5 * h * w); + let dxy = [(0i32, 1i32), (1, 0), (0, -1), (-1, 0)]; + for x in 0..h { + for y in 0..w { + if s[x][y] == '#' { continue; } + let mut has = false; + for &(dx, dy) in &dxy { + let nx = x.wrapping_add(dx as usize); + let ny = y.wrapping_add(dy as usize); + if nx >= h || ny >= w { continue; } + if s[nx][ny] == '#' { + has = true; + } + } + if has { continue; } + let v = x * w + y; + for dir in 0..4 { + let (dx, dy) = dxy[dir]; + let nx = x.wrapping_add(dx as usize); + let ny = y.wrapping_add(dy as usize); + if nx >= h || ny >= w { continue; } + uf.unite(5 * v + dir, 5 * (nx * w + ny) + ((dir + 2) % 4)); + uf.unite(5 * v + 4, 5 * v + dir); + } + } + } + let mut size = vec![vec![]; 5 * h * w]; + for i in 0..5 * h * w { + size[uf.root(i)].push(i / 5); + } + for i in 0..5 * h * w { + size[i].sort_unstable(); + size[i].dedup(); + ma = ma.max(size[i].len()); + } + println!("{}", ma); +} diff --git a/atcoder/abc351/e.rs b/atcoder/abc351/e.rs new file mode 100644 index 00000000..52c94821 --- /dev/null +++ b/atcoder/abc351/e.rs @@ -0,0 +1,72 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +fn g(a: Vec) -> i64 { + let n = a.len() as i64; + let mut tot = 0; + for i in 0..n { + tot += a[i as usize] * (2 * i - n + 1); + } + tot +} + +fn f(a: &[(i64, i64)]) -> i64 { + if a.is_empty() { + return 0; + } + let mut bx = vec![]; + let mut by = vec![]; + let c = (a[0].0 + a[0].1) & 1; + for &(x, y) in a { + bx.push((x + y - c) / 2); + by.push((x - y + c) / 2); + } + bx.sort(); by.sort(); + g(bx) + g(by) +} + +fn main() { + input! { + n: usize, + xy: [(i64, i64); n], + } + let mut even = vec![]; + let mut odd = vec![]; + for i in 0..n { + if (xy[i].0 + xy[i].1) % 2 == 0 { + even.push(xy[i]); + } else { + odd.push(xy[i]); + } + } + println!("{}", f(&even) + f(&odd)); +} diff --git a/atcoder/abc351/f.rs b/atcoder/abc351/f.rs new file mode 100644 index 00000000..1b2e5075 --- /dev/null +++ b/atcoder/abc351/f.rs @@ -0,0 +1,108 @@ +// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 +macro_rules! input { + ($($r:tt)*) => { + let stdin = std::io::stdin(); + let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); + let mut next = move || -> String{ + bytes.by_ref().map(|r|r.unwrap() as char) + .skip_while(|c|c.is_whitespace()) + .take_while(|c|!c.is_whitespace()) + .collect() + }; + input_inner!{next, $($r)*} + }; +} + +macro_rules! input_inner { + ($next:expr) => {}; + ($next:expr,) => {}; + ($next:expr, $var:ident : $t:tt $($r:tt)*) => { + let $var = read_value!($next, $t); + input_inner!{$next $($r)*} + }; +} + +macro_rules! read_value { + ($next:expr, [ $t:tt ; $len:expr ]) => { + (0..$len).map(|_| read_value!($next, $t)).collect::>() + }; + ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); +} + +// Segment Tree. This data structure is useful for fast folding on intervals of an array +// whose elements are elements of monoid I. Note that constructing this tree requires the identity +// element of I and the operation of I. +// Verified by: yukicoder No. 2220 (https://yukicoder.me/submissions/841554) +struct SegTree { + n: usize, + orign: usize, + dat: Vec, + op: BiOp, + e: I, +} + +impl SegTree + where BiOp: Fn(I, I) -> I, + I: Copy { + pub fn new(n_: usize, op: BiOp, e: I) -> Self { + let mut n = 1; + while n < n_ { n *= 2; } // n is a power of 2 + SegTree {n: n, orign: n_, dat: vec![e; 2 * n - 1], op: op, e: e} + } + // ary[k] <- v + pub fn update(&mut self, idx: usize, v: I) { + debug_assert!(idx < self.orign); + let mut k = idx + self.n - 1; + self.dat[k] = v; + while k > 0 { + k = (k - 1) / 2; + self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]); + } + } + // [a, b) (half-inclusive) + // http://proc-cpuinfo.fixstars.com/2017/07/optimize-segment-tree/ + #[allow(unused)] + pub fn query(&self, rng: std::ops::Range) -> I { + let (mut a, mut b) = (rng.start, rng.end); + debug_assert!(a <= b); + debug_assert!(b <= self.orign); + let mut left = self.e; + let mut right = self.e; + a += self.n - 1; + b += self.n - 1; + while a < b { + if (a & 1) == 0 { + left = (self.op)(left, self.dat[a]); + } + if (b & 1) == 0 { + right = (self.op)(self.dat[b - 1], right); + } + a = a / 2; + b = (b - 1) / 2; + } + (self.op)(left, right) + } +} + +fn main() { + input! { + n: usize, + a: [i64; n], + } + let mut coo = a.clone(); + coo.sort(); coo.dedup(); + let m = coo.len(); + let mut stc = SegTree::new(m, |x, y| x + y, 0i64); + let mut st = SegTree::new(m, |x, y| x + y, 0i64); + let mut ans = 0; + for i in (0..n).rev() { + let a = a[i]; + let idx = coo.binary_search(&a).unwrap(); + let val = st.query(idx..m); + let cnt = stc.query(idx..m); + ans += val - cnt * a; + st.update(idx, st.query(idx..idx + 1) + a); + stc.update(idx, stc.query(idx..idx + 1) + 1); + } + println!("{ans}"); +} diff --git a/atcoder/abc351/remain.txt b/atcoder/abc351/remain.txt new file mode 100644 index 00000000..01058d84 --- /dev/null +++ b/atcoder/abc351/remain.txt @@ -0,0 +1 @@ +g