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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
        for (auto &u : v) os << u << endl;
        return (os);
}

template<typename T>
istream& operator >>(istream &is, pair<vector<T>&, int> v) {
        while (v.second--) {
                T a;
                v.first.push_back(is >> a, a);
        }
        return (is);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
        for (auto &u : v) is >> u;
        return (is);
}

int main() {
    int R, C, N;
    vector<Pi> dat[4];  // saigo ni merge suru

    int cnt = 0;
    cin >> R >> C >> N;
    for (int i = 0; i < N; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if ((x1 == 0 || x1 == C || y1 == 0 || y1 == R) && 
            (x2 == 0 || x2 == C || y2 == 0 || y2 == R)) {
            cnt++;
            //upper (syoujunn)
            if (x1 > 0 && y1 == R) dat[0].emplace_back(x1, i);
            if (x2 > 0 && y2 == R) dat[0].emplace_back(x2, i);
            //migi (koujunn)
            if (y1 < R && x1 == C) dat[1].emplace_back(y1, i);
            if (y2 < R && x2 == C) dat[1].emplace_back(y2, i);
            //lower (koujunn)
            if (x1 < C && y1 == 0) dat[2].emplace_back(x1, i);
            if (x2 < C && y2 == 0) dat[2].emplace_back(x2, i);
            //hidari (syoujunn)
            if (y1 > 0 && x1 == 0) dat[3].emplace_back(y1, i);
            if (y2 > 0 && x2 == 0) dat[3].emplace_back(y2, i);
        }
    }
    if (cnt == 0) {
        cout << "YES" << endl;
        return (0);
    }
    sort(begin(dat[0]), end(dat[0]));
    sort(rbegin(dat[1]), rend(dat[1]));
    sort(rbegin(dat[2]), rend(dat[2]));
    sort(begin(dat[3]), end(dat[3]));
    
    vint adat;

    for (int i = 0; i < 4; i++) {
        for (auto &x : dat[i]) {
            adat.push_back(x.second);
        }
    }
    
    int j = -1, k = -1;
    for (int i = 0; i < (int)adat.size() - 1; i++) {
        if (adat[i] == adat[i + 1]) {
            j = i;
            k = i + 1;
        }
    }
    if (j == -1) {
        cout << "NO" << endl;
        return (0);
    }
    bool flag[200005] = {};
    int size = adat.size();

    while (!flag[j] || !flag[k]) {
        if (adat[j] != adat[k]) {
            cout << "NO" << endl;
            return (0);
        }
        flag[j] = true;
        flag[k] = true;
        (j += size - 1) %= size;
        (k += 1) %= size;
    }
    cout << "YES" << endl;
    return (0);
}