結果
| 問題 |
No.2786 RMQ on Grid Path
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2024-06-15 13:02:03 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 173 ms / 6,000 ms |
| コード長 | 4,389 bytes |
| コンパイル時間 | 13,919 ms |
| コンパイル使用メモリ | 381,388 KB |
| 実行使用メモリ | 31,232 KB |
| 最終ジャッジ日時 | 2024-06-15 13:02:25 |
| 合計ジャッジ時間 | 21,465 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 35 |
ソースコード
use std::io::Write;
fn run() {
input! {
h: usize,
w: usize,
a: [[u32; w]; h],
q: usize,
ask: [(usize1, usize1, usize1, usize1); q],
}
let pos = |x, y| x * w + y;
let mut e = vec![];
for i in 0..h {
for j in 0..w {
if i > 0 {
let d = a[i - 1][j].max(a[i][j]);
e.push((pos(i - 1, j), pos(i, j), d));
}
if j > 0 {
let d = a[i][j - 1].max(a[i][j]);
e.push((pos(i, j - 1), pos(i, j), d));
}
}
}
e.sort_by_key(|e| e.2);
let mut dsu = DSU::new(h * w);
let mut dp = vec![250000 + 1; h * w];
for (a, b, w) in e {
if let Some((_p, c)) = dsu.unite(a, b) {
dp[c] = w;
}
}
let out = std::io::stdout();
let mut out = std::io::BufWriter::new(out.lock());
for (a, b, c, d) in ask {
let mut x = pos(a, b);
let mut y = pos(c, d);
let mut ans = 0;
while x != y {
if dp[x] > dp[y] {
std::mem::swap(&mut x, &mut y);
}
ans = dp[x];
x = dsu.parent(x).unwrap();
}
writeln!(out, "{}", ans).ok();
}
}
fn main() {
run();
}
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let s = {
use std::io::Read;
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
s
};
let mut iter = s.split_whitespace();
input_inner!{iter, $($r)*}
};
}
#[macro_export]
macro_rules! input_inner {
($iter:expr) => {};
($iter:expr, ) => {};
($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
let $var = read_value!($iter, $t);
input_inner!{$iter $($r)*}
};
}
#[macro_export]
macro_rules! read_value {
($iter:expr, ( $($t:tt),* )) => {
( $(read_value!($iter, $t)),* )
};
($iter:expr, [ $t:tt ; $len:expr ]) => {
(0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
};
($iter:expr, chars) => {
read_value!($iter, String).chars().collect::<Vec<char>>()
};
($iter:expr, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
// ---------- end input macro ----------
//---------- begin union_find ----------
pub struct DSU {
p: Vec<i32>,
}
impl DSU {
pub fn new(n: usize) -> DSU {
assert!(n < std::i32::MAX as usize);
DSU { p: vec![-1; n] }
}
pub fn init(&mut self) {
self.p.iter_mut().for_each(|p| *p = -1);
}
pub fn root(&self, mut x: usize) -> usize {
assert!(x < self.p.len());
while self.p[x] >= 0 {
x = self.p[x] as usize;
}
x
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.p.len() && y < self.p.len());
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.p.len() && y < self.p.len());
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
std::mem::swap(&mut x, &mut y);
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
pub fn parent(&self, x: usize) -> Option<usize> {
assert!(x < self.p.len());
let p = self.p[x];
if p >= 0 {
Some(p as usize)
} else {
None
}
}
pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
where
F: FnMut(usize),
{
while let Some(p) = self.parent(x) {
f(x);
x = p;
}
x
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.p.len());
let r = self.root(x);
(-self.p[r]) as usize
}
}
//---------- end union_find ----------
akakimidori