complete 1660.rs 5242.rs 8406.rs 14024.rs

This commit is contained in:
2026-07-24 17:52:47 +09:00
parent 5b49d47300
commit 8f06210897
4 changed files with 165 additions and 0 deletions

View File

@@ -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::<usize>().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::<Vec<_>>();
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();
}
}

View File

@@ -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::<usize>().unwrap());
let n = iter.next().unwrap() as u64;
println!("{}", countf(n));
}

View File

@@ -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::<usize>().unwrap());
let (l, r) = (iter.next().unwrap(), iter.next().unwrap());
println!("{}", if l | r == 1 { "True" } else { "False" });
}

View File

@@ -0,0 +1,63 @@
use std::io::{read_to_string, stdin};
struct ReligionState {
parents: Vec<usize>,
counts: Vec<usize>,
}
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::<usize>().unwrap());
let n = iter.next().unwrap();
let q = iter.next().unwrap();
let mut parents = (0..n).map(|i| i).collect::<Vec<_>>();
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));
}
}
}