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 C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include "math.h"
#include <set>
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long long int lli;

int r, c, n;
struct node {
    int x, y, i;
    int type()
    {
        if (x == 0 and y == 0)
            return 1;
        else if (x == 0 and y == r) {
            return 2;
        } else if (x == c and y == r) {
            return 3;
        } else if (x == c and y == 0) {
            return 4;
        }
        if (x == 0)
            return 1;
        else if (y == 0)
            return 4;
        else if (x == r)
            return 3;
        else if (y == c)
            return 2;
    }
};
int main()
{
    cin >> r >> c >> n;
    lli x1[100005], y1[100005], x2[100005], y2[100005];
    vector<node> v;
    rep(i, n)
    {
        cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
        bool f1 = max(x1[i], x2[i]) == r;
        bool f2 = min(x1[i], x2[i]) == 0;
        bool f3 = max(y1[i], y2[i]) == c;
        bool f4 = min(y1[i], y2[i]) == 0;

        if (f1 and f3) {
            v.push_back(node{x1[i], y1[i], i});
            v.push_back(node{x2[i], y2[i], i});
        }
        if (f1 and f4) {
            v.push_back(node{x1[i], y1[i], i});
            v.push_back(node{x2[i], y2[i], i});
        }
        if (f2 and f3) {
            v.push_back(node{x1[i], y1[i], i});
            v.push_back(node{x2[i], y2[i], i});
        }
        if (f2 and f4) {
            v.push_back(node{x2[i], y2[i], i});

            v.push_back(node{x1[i], y1[i], i});
        }
    }
    auto cmp = [](node l, node r) -> bool {
        if (l.type() != r.type()) {
            return l.type() < r.type();
        } else {
            int t = l.type();
            if (t == 1) {
                return l.y < r.y;
            } else if (t == 2) {
                return l.x < r.x;
            } else if (t == 3) {
                return l.y > r.y;
            } else {
                return l.x > r.x;
            }
        }
    };
    bool flag = true;

    if (v.size() > 1) {
        sort(v.begin(), v.end(), cmp);
        rep(i, v.size())
        {
            if (v[i].i != v[v.size() - 1 - i].i) {
                flag = false;
            }
        }
    }
    if (!flag) {
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
}