AtCoder Beginner Contest 168 F - . (Single Dot)
問題
解法
まずはサイズの小さい問題を考える。入力として与えられる全ての線分の座標 (x, y) が 0<=x,y<=1000 を満たすとすると、max(x) * max(y) の 2 次元配列を持って、その上で幅優先探索などをして、線分をまたがずに到達できるマスの数を数えれば良いことになる。マス (i, j) は直線 x=i, x=i+1, y=j, y=j+1 に囲まれたマスを表すとする。(i, j) と (i, j+1) の間に線分があるかどうかは、y=j+1 で x1<=j&&j+1<=x2 を満たす線分が存在するかどうかを調べれば良い。これは、線分を y 座標ごとにまとめて、(x1,x2) をソートしておくと二分探索で j<=x1 を満たす線分とその1つ前の線分を取り出して調べることで判定できる。これで隣接するセルに移動できるかどうかが判定できるようになった。よって問題は、(0, 0) を出発して、線分をまたがずに隣接するセルを渡り歩き、到達したセルの数を数えれば良い。
実際の問題を考える。線分の座標の範囲は広いが、値として現れる個数は高々 N 個なので、座標圧縮して後で戻すことにする。この時、セル (i,j) の面積は (x[i+1]-x[i]) * (y[i+1]-y[i]) で求められるので、最後の復元も簡単にできる。無限に行くことができるので、x=-INF, x=INF, y=-INF, y=INF の直線が存在して、この直線のところまで来たら INF を出力することにする。これは実装上では i=0 などのセルに到達できれば INF を出力すれば良い。
コード
use std::collections::VecDeque; const INF: i64 = 1 << 50; fn main() { let (r, w) = (std::io::stdin(), std::io::stdout()); let mut sc = IO::new(r.lock(), w.lock()); let n: usize = sc.read(); let m: usize = sc.read(); let mut vx = vec![-INF, 0, INF]; let mut vy = vec![-INF, 0, INF]; let mut x_lines = vec![]; for _ in 0..n { let x1: i64 = sc.read(); let x2: i64 = sc.read(); let y: i64 = sc.read(); x_lines.push((x1, x2, y)); vx.push(x1); vx.push(x2); vy.push(y); } let mut y_lines = vec![]; for _ in 0..m { let x: i64 = sc.read(); let y1: i64 = sc.read(); let y2: i64 = sc.read(); y_lines.push((y1, y2, x)); vy.push(y1); vy.push(y2); vx.push(x); } vx.sort(); vx.dedup(); vy.sort(); vy.dedup(); let h = vx.len(); let w = vy.len(); let mut x_wall = vec![vec![]; w]; for &(x1, x2, y) in x_lines.iter() { let x1 = vx.binary_search(&x1).unwrap(); let x2 = vx.binary_search(&x2).unwrap(); let y = vy.binary_search(&y).unwrap(); x_wall[y].push((x1, x2)); } for x_wall in x_wall.iter_mut() { x_wall.sort(); } let mut y_wall = vec![vec![]; h]; for &(y1, y2, x) in y_lines.iter() { let y1 = vy.binary_search(&y1).unwrap(); let y2 = vy.binary_search(&y2).unwrap(); let x = vx.binary_search(&x).unwrap(); y_wall[x].push((y1, y2)); } for y_wall in y_wall.iter_mut() { y_wall.sort(); } let mut board = vec![vec![false; w - 1]; h - 1]; let si = vx.binary_search(&0).unwrap(); let sj = vy.binary_search(&0).unwrap(); board[si][sj] = true; let mut queue = VecDeque::new(); queue.push_back((si, sj)); while let Some((x, y)) = queue.pop_front() { if x > 0 { let conflict = wall(&y_wall[x], y); if !board[x - 1][y] && !conflict { board[x - 1][y] = true; queue.push_back((x - 1, y)); } } if x + 1 < board.len() { let conflict = wall(&y_wall[x + 1], y); if !board[x + 1][y] && !conflict { board[x + 1][y] = true; queue.push_back((x + 1, y)); } } if y > 0 { let conflict = wall(&x_wall[y], x); if !board[x][y - 1] && !conflict { board[x][y - 1] = true; queue.push_back((x, y - 1)); } } if y + 1 < board[x].len() { let conflict = wall(&x_wall[y + 1], x); if !board[x][y + 1] && !conflict { board[x][y + 1] = true; queue.push_back((x, y + 1)); } } } let mut ans = 0; for x in 0..board.len() { for y in 0..board[x].len() { if !board[x][y] { continue; } if x == 0 || x == board.len() - 1 || y == 0 || y == board[x].len() - 1 { println!("INF"); return; } let x1 = vx[x]; let x2 = vx[x + 1]; let y1 = vy[y]; let y2 = vy[y + 1]; ans += (x2 - x1) * (y2 - y1); } } println!("{}", ans); } fn wall(walls: &Vec<(usize, usize)>, x: usize) -> bool { let mut conflict = false; let right = walls.binary_search(&(x, 0)).err().unwrap(); if walls.len() > right { let (x1, x2) = walls[right]; if x1 <= x && x + 1 <= x2 { conflict = true; } } if right > 0 && walls.len() > right - 1 { let (x1, x2) = walls[right - 1]; if x1 <= x && x + 1 <= x2 { conflict = true; } } conflict } pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>); impl<R: std::io::Read, W: std::io::Write> IO<R, W> { pub fn new(r: R, w: W) -> Self { Self(r, std::io::BufWriter::new(w)) } pub fn write<S: ToString>(&mut self, s: S) { use std::io::Write; self.1.write(s.to_string().as_bytes()).unwrap(); } pub fn read<T: std::str::FromStr>(&mut self) -> T { use std::io::Read; let buf = self .0 .by_ref() .bytes() .map(|b| b.unwrap()) .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t') .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t') .collect::<Vec<_>>(); unsafe { std::str::from_utf8_unchecked(&buf) } .parse() .ok() .expect("Parse error.") } pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.read()).collect() } pub fn chars(&mut self) -> Vec<char> { self.read::<String>().chars().collect() } }