Problem:
Score : 700 points

Problem Statement

Snuke is playing a puzzle game.
In this game, you are given a rectangular board of dimensions R × C, filled with numbers. Each integer i from 1 through N is written twice, at the coordinates (x_{i,1},y_{i,1}) and (x_{i,2},y_{i,2}).

The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from 1 through N.
Here, the curves may not go outside the board or cross each other.

Determine whether this is possible.

Constraints

1 ≤ R,C ≤ 10^8

1 ≤ N ≤ 10^5

0 ≤ x_{i,1},x_{i,2} ≤ R(1 ≤ i ≤ N)

0 ≤ y_{i,1},y_{i,2} ≤ C(1 ≤ i ≤ N)

All given points are distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

R C N
x_{1,1} y_{1,1} x_{1,2} y_{1,2}
:
x_{N,1} y_{N,1} x_{N,2} y_{N,2}

Output

Print YES if the objective is achievable; print NO otherwise.

Sample Input 1

4 2 3
0 1 3 1
1 1 4 1
2 0 2 2

Sample Output 1

YES

The above figure shows a possible solution.

Sample Input 2

2 2 4
0 0 2 2
2 0 0 1
0 2 1 2
1 1 2 1

Sample Output 2

NO

Sample Input 3

5 5 7
0 0 2 4
2 3 4 5
3 5 5 2
5 5 5 4
0 3 5 1
2 2 4 4
0 5 4 1

Sample Output 3

YES

Sample Input 4

1 1 2
0 0 1 1
1 0 0 1

Sample Output 4

NO

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::collections::VecDeque;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let r: usize = sc.read();
    let c: usize = sc.read();
    let n: usize = sc.read();

    let mut bottom = vec![];
    let mut right = vec![];
    let mut top = vec![];
    let mut left = vec![];

    for i in 0..n {
        let x1: usize = sc.read();
        let y1: usize = sc.read();
        let x2: usize = sc.read();
        let y2: usize = sc.read();

        if !on_border((x1, y1), r, c) || !on_border((x2, y2), r, c) {
            continue;
        }

        for &(x, y) in [(x1, y1), (x2, y2)].iter() {
            if y == 0 && x < r {
                bottom.push((x, i));
            }
            if x == r && y < c {
                right.push((y, i));
            }
            if y == c && x > 0 {
                top.push((x, i));
            }
            if x == 0 && y > 0 {
                left.push((y, i));
            }
        }
    }
    bottom.sort();
    right.sort();
    top.sort();
    top.reverse();
    left.sort();
    left.reverse();

    let mut stack = VecDeque::new();
    for (_, i) in bottom.into_iter() {
        if let Some(head) = stack.pop_front() {
            if head != i {
                stack.push_front(head);
                stack.push_front(i);
            }
        } else {
            stack.push_front(i);
        }
    }
    for (_, i) in right.into_iter() {
        if let Some(head) = stack.pop_front() {
            if head != i {
                stack.push_front(head);
                stack.push_front(i);
            }
        } else {
            stack.push_front(i);
        }
    }
    for (_, i) in top.into_iter() {
        if let Some(head) = stack.pop_front() {
            if head != i {
                stack.push_front(head);
                stack.push_front(i);
            }
        } else {
            stack.push_front(i);
        }
    }
    for (_, i) in left.into_iter() {
        if let Some(head) = stack.pop_front() {
            if head != i {
                stack.push_front(head);
                stack.push_front(i);
            }
        } else {
            stack.push_front(i);
        }
    }

    if stack.is_empty() {
        println!("YES");
    } else {
        println!("NO");
    }
}

fn on_border((x, y): (usize, usize), r: usize, c: usize) -> bool {
    x == 0 || x == r || y == 0 || y == c
}

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) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.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()
    }
}