From 8f0621089704e702b9f648da6a7fabf7287b9aa1 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Fri, 24 Jul 2026 17:52:47 +0900 Subject: [PATCH] complete 1660.rs 5242.rs 8406.rs 14024.rs --- storage/jungol/rs/completed/14024.rs | 55 ++++++++++++++++++++++++ storage/jungol/rs/completed/1660.rs | 35 ++++++++++++++++ storage/jungol/rs/completed/5242.rs | 12 ++++++ storage/jungol/rs/completed/8406.rs | 63 ++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 storage/jungol/rs/completed/14024.rs create mode 100644 storage/jungol/rs/completed/1660.rs create mode 100644 storage/jungol/rs/completed/5242.rs create mode 100644 storage/jungol/rs/completed/8406.rs diff --git a/storage/jungol/rs/completed/14024.rs b/storage/jungol/rs/completed/14024.rs new file mode 100644 index 0000000..8706383 --- /dev/null +++ b/storage/jungol/rs/completed/14024.rs @@ -0,0 +1,55 @@ +use std::{ + cmp::min, io::{read_to_string, stdin, stdout, Write}, +}; + +fn main() { + let temp = read_to_string(stdin()).unwrap(); + let mut iter = temp + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()); + + let (n, m, q) = ( + iter.next().unwrap(), + iter.next().unwrap(), + iter.next().unwrap(), + ); + + let mut lock = stdout().lock(); + + let mut bfs = (0..m).map(|_| iter.next().unwrap()).collect::>(); + bfs.sort(); + + let dist = |a: usize, b: usize| -> usize { + let d = if a > b { a - b } else { b - a }; + min(d, 2 * n - d) + }; + + for _ in 0..q { + let (mut x, mut y) = (iter.next().unwrap(), iter.next().unwrap()); + if x > y { + (x, y) = (y, x); + } + let delta = y - x; + + let mut r = min(delta, 2 * n - delta); + + let (px, py) = (x % n, y % n); + + let ix = bfs.partition_point(|&k| k < px); + let iy = bfs.partition_point(|&k| k < py); + + let cands = [(ix + m - 1) % m, ix % m, (iy + m - 1) % m, iy % m]; + + for &cand in &cands { + let k = bfs[cand]; + let u = k; + let v = k + n; + let d1 = dist(x, u) + dist(v, y) + 1; + let d2 = dist(x, v) + dist(u, y) + 1; + r = min(r, d1); + r = min(r, d2); + } + + writeln!(lock, "{}", r).unwrap(); + } +} diff --git a/storage/jungol/rs/completed/1660.rs b/storage/jungol/rs/completed/1660.rs new file mode 100644 index 0000000..aae49c1 --- /dev/null +++ b/storage/jungol/rs/completed/1660.rs @@ -0,0 +1,35 @@ +use std::io::{read_to_string, stdin}; + +fn countf2x(x: u64) -> u64 { + // f(2^x - 1) = 2^(x-1) * x; + if x == 0 { + 0 + } else { + 2u64.pow((x - 1) as u32) * x + } +} + +fn countf(n: u64) -> u64 { + let mut x = 32u64; + let mut t = 1u64 << 32u64; + if n == 0 { + return 0; + } + while t & n == 0 { + t >>= 1; + x -= 1; + } + + countf2x(x) + (n + 1 - t) + countf(n - t) +} + +fn main() { + let temp = read_to_string(stdin()).unwrap(); + let mut iter = temp + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()); + + let n = iter.next().unwrap() as u64; + + println!("{}", countf(n)); +} diff --git a/storage/jungol/rs/completed/5242.rs b/storage/jungol/rs/completed/5242.rs new file mode 100644 index 0000000..774d4c5 --- /dev/null +++ b/storage/jungol/rs/completed/5242.rs @@ -0,0 +1,12 @@ +use std::io::{read_to_string, stdin}; + +fn main() { + let temp = read_to_string(stdin()).unwrap(); + let mut iter = temp + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()); + + let (l, r) = (iter.next().unwrap(), iter.next().unwrap()); + + println!("{}", if l | r == 1 { "True" } else { "False" }); +} diff --git a/storage/jungol/rs/completed/8406.rs b/storage/jungol/rs/completed/8406.rs new file mode 100644 index 0000000..b429cfd --- /dev/null +++ b/storage/jungol/rs/completed/8406.rs @@ -0,0 +1,63 @@ +use std::io::{read_to_string, stdin}; + +struct ReligionState { + parents: Vec, + counts: Vec, +} + +impl ReligionState { + fn find(&self, x: usize) -> usize { + let mut node = x; + while (node != self.parents[node]) { + node = self.parents[node]; + } + return node; + } + + fn sames(&self, x: usize) -> usize { + return self.counts[self.find(x)]; + } + + fn unite(&mut self, x: usize, y: usize) { + let rx = self.find(x); + let ry = self.find(y); + + if rx < ry { + self.parents[ry] = rx; + self.counts[rx] += self.counts[ry]; + } else if rx > ry { + self.parents[rx] = ry; + self.counts[ry] += self.counts[rx] + } else { + } + } +} + +fn main() { + let temp = read_to_string(stdin()).unwrap(); + let mut iter = temp + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()); + + let n = iter.next().unwrap(); + let q = iter.next().unwrap(); + + let mut parents = (0..n).map(|i| i).collect::>(); + + let mut S = ReligionState { + parents, + counts: (0..n).map(|_| 1).collect(), + }; + + for _ in 0..q { + let query = iter.next().unwrap(); + if (query == 1) { + let (x, y) = (iter.next().unwrap() - 1, iter.next().unwrap() - 1); + + S.unite(x, y); + } else { + let x = iter.next().unwrap() - 1; + println!("{}", S.sames(x)); + } + } +}