//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 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_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_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::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // struct SAT2 { n: usize, g: Vec>, ig: Vec>, } impl SAT2 { fn new(n: usize) -> SAT2 { SAT2 { n: n, g: vec![vec![]; 2 * n], ig: vec![vec![]; 2 * n], } } fn add(&mut self, a: usize, b: usize) { let g = &mut self.g; let ig = &mut self.ig; let n = self.n; let c = if a >= n {a - n} else {a + n}; let d = if b >= n {b - n} else {b + n}; g[c].push(b); g[d].push(a); ig[b].push(c); ig[a].push(d); } fn dfs_sort(&self, v: usize, used: &mut [bool], q: &mut Vec) { assert!(!used[v]); used[v] = true; for &u in &self.g[v] { if !used[u] { self.dfs_sort(u, used, q); } } q.push(v); } fn solve(&self) -> bool { let n = self.n; let mut used = vec![false; 2 * n]; let mut q = Vec::with_capacity(2 * n); for i in 0..(2 * n) { if used[i] { continue; } self.dfs_sort(i, &mut used, &mut q); } let mut id = vec![std::u32::MAX; 2 * n]; let mut k = 0; for v in q { if id[v] < k { continue; } let mut stack = vec![]; id[v] = k; stack.push(v); while let Some(v) = stack.pop() { for &u in &self.g[v] { if id[u] > k { id[u] = k; stack.push(u); } } } k += 1; } for i in 0..n { if id[i] == id[i + n] { return false; } } true } } use std::cmp::{max, min}; fn run() { input! { n: usize, m: u32, p: [(u32, u32); n], } let mut sat = SAT2::new(n); for i in 0..n { let (l, r) = (p[i].0, p[i].1 + 1); for &(a, b, x) in [(l, r, i + n), (m - r, m - l, i)].iter() { for j in 0..i { let (l, r) = (p[j].0, p[j].1 + 1); for &(c, d, y) in [(l, r, j + n), (m - r, m - l, j)].iter() { if max(a, c) < min(b, d) { sat.add(x, y); } } } } } if sat.solve() { println!("YES"); } else { println!("NO"); } } fn main() { run(); }