結果

問題 No.274 The Wall
ユーザー zeronosu77108zeronosu77108
提出日時 2022-07-17 16:54:21
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 495 ms / 2,000 ms
コード長 6,940 bytes
コンパイル時間 13,332 ms
コンパイル使用メモリ 376,960 KB
実行使用メモリ 377,088 KB
最終ジャッジ日時 2024-06-29 18:01:43
合計ジャッジ時間 15,868 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 180 ms
126,720 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 495 ms
377,088 KB
testcase_12 AC 13 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 160 ms
98,944 KB
testcase_17 AC 149 ms
93,696 KB
testcase_18 AC 168 ms
101,504 KB
testcase_19 AC 19 ms
5,376 KB
testcase_20 AC 22 ms
5,376 KB
testcase_21 AC 24 ms
5,376 KB
testcase_22 AC 25 ms
5,376 KB
testcase_23 AC 25 ms
5,376 KB
testcase_24 AC 25 ms
5,376 KB
testcase_25 AC 25 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    input! {
        n : usize,
        m : Usize1,
        lr : [(usize, usize); n]
    }

    let mut ts = TwoSat::new(n);
    for (i, &(li, ri)) in lr.iter().enumerate() {
        for (j, &(lj, rj)) in lr.iter().enumerate().skip(i+1) {
            if (li..=ri).contains(&lj) || (lj..=rj).contains(&li) {
                ts.add_clause(i, false, j, false);
            }

            {
                let (li, ri) = (m - ri, m - li);
                if (li..=ri).contains(&lj) || (lj..=rj).contains(&li) {
                    ts.add_clause(i, true, j, false);
                }
            }


            {
                let (lj, rj) = (m - rj, m - lj);
                if (li..=ri).contains(&lj) || (lj..=rj).contains(&li) {
                    ts.add_clause(i, false, j, true);
                }
            }
            {
                let (li, ri) = (m - ri, m - li);
                let (lj, rj) = (m - rj, m - lj);
                if (li..=ri).contains(&lj) || (lj..=rj).contains(&li) {
                    ts.add_clause(i, true, j, true) }
            }
        }
    }

    let ans = ts.satisfiable();
    println!("{}", if ans {"YES"} else {"NO"});
}


// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($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_export] 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_export] macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ]) => { read_value!($next, [$t ; read_value!($next, usize)]) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, Chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, Bytes) => { read_value!($next, String).bytes().collect::<Vec<u8>>() }; ($next:expr, Usize1) => { read_value!($next, usize) - 1 }; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; }


//https://github.com/rust-lang-ja/ac-library-rs
pub mod internal_scc { pub struct Csr<E> { start: Vec<usize>, elist: Vec<E>, } impl<E> Csr<E> where E: Copy, { pub fn new(n: usize, edges: &[(usize, E)], init: E) -> Self { let mut csr = Csr { start: vec![0; n + 1], elist: vec![init; edges.len()], }; for e in edges.iter() { csr.start[e.0 + 1] += 1; } for i in 1..=n { csr.start[i] += csr.start[i - 1]; } let mut counter = csr.start.clone(); for e in edges.iter() { csr.elist[counter[e.0]] = e.1; counter[e.0] += 1; } csr } } #[derive(Copy, Clone)] struct _Edge { to: usize, } pub struct SccGraph { n: usize, edges: Vec<(usize, _Edge)>, } impl SccGraph { pub fn new(n: usize) -> Self { SccGraph { n, edges: vec![] } } pub fn num_vertices(&self) -> usize { self.n } pub fn add_edge(&mut self, from: usize, to: usize) { self.edges.push((from, _Edge { to })); } pub fn scc_ids(&self) -> (usize, Vec<usize>) { struct _Env { g: Csr<_Edge>, now_ord: usize, group_num: usize, visited: Vec<usize>, low: Vec<usize>, ord: Vec<Option<usize>>, ids: Vec<usize>, } let mut env = _Env { g: Csr::new(self.n, &self.edges, _Edge { to: 0 }), now_ord: 0, group_num: 0, visited: Vec::with_capacity(self.n), low: vec![0; self.n], ord: vec![None; self.n], ids: vec![0; self.n], }; fn dfs(v: usize, n: usize, env: &mut _Env) { env.low[v] = env.now_ord; env.ord[v] = Some(env.now_ord); env.now_ord += 1; env.visited.push(v); for i in env.g.start[v]..env.g.start[v + 1] { let to = env.g.elist[i].to; if let Some(x) = env.ord[to] { env.low[v] = std::cmp::min(env.low[v], x); } else { dfs(to, n, env); env.low[v] = std::cmp::min(env.low[v], env.low[to]); } } if env.low[v] == env.ord[v].unwrap() { loop { let u = *env.visited.last().unwrap(); env.visited.pop(); env.ord[u] = Some(n); env.ids[u] = env.group_num; if u == v { break; } } env.group_num += 1; } } for i in 0..self.n { if env.ord[i].is_none() { dfs(i, self.n, &mut env); } } for x in env.ids.iter_mut() { *x = env.group_num - 1 - *x; } (env.group_num, env.ids) }pub fn scc(&self) -> Vec<Vec<usize>> { let ids = self.scc_ids(); let group_num = ids.0; let mut counts = vec![0usize; group_num]; for &x in ids.1.iter() { counts[x] += 1; } let mut groups: Vec<Vec<usize>> = (0..ids.0).map(|_| vec![]).collect(); for i in 0..group_num { groups[i].reserve(counts[i]); } for i in 0..self.n { groups[ids.1[i]].push(i); } groups } }}
pub mod twosat { use crate::internal_scc; pub struct TwoSat { n: usize, scc: internal_scc::SccGraph, answer: Vec<bool>, } impl TwoSat { pub fn new(n: usize) -> Self { TwoSat { n, answer: vec![false; n], scc: internal_scc::SccGraph::new(2 * n), } } pub fn add_clause(&mut self, i: usize, f: bool, j: usize, g: bool) { assert!(i < self.n && j < self.n); self.scc.add_edge(2 * i + !f as usize, 2 * j + g as usize); self.scc.add_edge(2 * j + !g as usize, 2 * i + f as usize); } pub fn satisfiable(&mut self) -> bool { let id = self.scc.scc_ids().1; for i in 0..self.n { if id[2 * i] == id[2 * i + 1] { return false; } self.answer[i] = id[2 * i] < id[2 * i + 1]; } true } pub fn answer(&self) -> &[bool] { &self.answer } } #[cfg(test)] mod tests { #![allow(clippy::many_single_char_names)] use super::*; #[test] fn solve_alpc_h_sample1() { let (n, d) = (3, 2); let x = [1, 2, 0i32]; let y = [4, 5, 6]; let mut t = TwoSat::new(n); for i in 0..n { for j in i + 1..n { if (x[i] - x[j]).abs() < d { t.add_clause(i, false, j, false); } if (x[i] - y[j]).abs() < d { t.add_clause(i, false, j, true); } if (y[i] - x[j]).abs() < d { t.add_clause(i, true, j, false); } if (y[i] - y[j]).abs() < d { t.add_clause(i, true, j, true); } } } assert!(t.satisfiable()); let answer = t.answer(); let mut res = vec![]; for (i, &v) in answer.iter().enumerate() { if v { res.push(x[i]) } else { res.push(y[i]); } } res.sort_unstable(); let mut min_distance = i32::max_value(); for i in 1..res.len() { min_distance = std::cmp::min(min_distance, res[i] - res[i - 1]); } assert!(min_distance >= d); } #[test] fn solve_alpc_h_sample2() { let (n, d) = (3, 3); let x = [1, 2, 0i32]; let y = [4, 5, 6]; let mut t = TwoSat::new(n); for i in 0..n { for j in i + 1..n { if (x[i] - x[j]).abs() < d { t.add_clause(i, false, j, false); } if (x[i] - y[j]).abs() < d { t.add_clause(i, false, j, true); } if (y[i] - x[j]).abs() < d { t.add_clause(i, true, j, false); } if (y[i] - y[j]).abs() < d { t.add_clause(i, true, j, true); } } } assert!(!t.satisfiable()); } }}

use twosat::*;
0