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: Accepted)

#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;

#if defined(DEBUG)

#include <unistd.h>
#include <sys/fcntl.h>
#define DLOG(...)        cerr << dbgFormat(__VA_ARGS__) << endl
#define DCALL(func, ...) func(__VA_ARGS__)

template <class... Args>
string dbgFormat(const char* fmt, Args... args) {
  size_t len = snprintf(nullptr, 0, fmt, args...);
  char buf[len + 1];
  snprintf(buf, len + 1, fmt, args...);
  return string(buf);
}

#else  // defined(DEBUG)

#define DLOG(...)
#define DCALL(func, ...)

#endif // defined(DEBUG)
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
struct T {
  int xc;
  int yc;
  int idx;
  T(int xxc, int yyc, int iidx) : xc(xxc), yc(yyc), idx(iidx) {}
};

int main(int argc, char *argv[]) {
#if defined(DEBUG)
  // GDB on Cygwin ignores redirection at run command.
  if (argc == 2) dup2(open(argv[1], 0), 0);
#else
  // For performance.  We should not use C-style stdio functions
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cout << setprecision(20);

  int R, C, N; cin >> R >> C >> N;

  auto edge = [&](int x, int y) -> bool {
    return x == 0 || x == R || y == 0 || y == C;
  };

  auto eord = [&](int x, int y) -> int {
    if (y == 0) return x;
    if (x == R) return R + y;
    if (y == C) return R + C + (R - x);
    if (x == 0) return R + C + R + (C - y);
    assert(false);
  };

  vector<T> P;
  int idx = 0;
  for (int i = 0; i < N; i++) {
    int p, q, r, s; cin >> p >> q >> r >> s;
    if (edge(p, q) && edge(r, s)) {
      P.emplace_back(p, q, idx);
      P.emplace_back(r, s, idx);
      idx++;
    }
  }
  sort(P.begin(), P.end(),
       [&](T u, T v) { return eord(u.xc, u.yc) < eord(v.xc, v.yc); });
  stack<T> st;
  for (auto u : P) {
    if (!st.empty() && st.top().idx == u.idx) {
      st.pop();
      DLOG("popped: (%d, %d, %d)", u.xc, u.yc, u.idx);
    }else {
      st.push(u);
      DLOG("popped: (%d, %d, %d)", u.xc, u.yc, u.idx);
    }
  }
  cout << (st.empty() ? "YES" : "NO") << endl;

  return 0;
}