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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.ForYellow
{
    class ARC076_E
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int[] rcn = ReadInts();
            int r = rcn[0];
            int c = rcn[1];
            int n = rcn[2];
            List<int[]> vals = new List<int[]>();
            for(int i = 0; i < n; i++)
            {
                int[] xy = ReadInts();
                if ((xy[0] == 0 || xy[0] == c || xy[1] == 0 || xy[1] == r)
                    && (xy[2] == 0 || xy[2] == c || xy[3] == 0 || xy[3] == r))
                {
                    int left = 0;
                    if (xy[0] == 0)
                    {
                        left = xy[1];
                    }
                    else if (xy[1] == r)
                    {
                        left = r + xy[0];
                    }
                    else if (xy[0] == c)
                    {
                        left = r * 2 + c - xy[1];
                    }
                    else
                    {
                        left = r * 2 + c * 2 - xy[0];
                    }

                    int right = 0;
                    if (xy[2] == 0)
                    {
                        right = xy[3];
                    }
                    else if (xy[3] == r)
                    {
                        right = r + xy[2];
                    }
                    else if (xy[2] == c)
                    {
                        right = r * 2 + c - xy[3];
                    }
                    else
                    {
                        right = r * 2 + c * 2 - xy[2];
                    }

                    vals.Add(new int[2] { left, i });
                    vals.Add(new int[2] { right, i });
                }
            }

            vals = vals.OrderBy(a => a[0]).ToList();
            Stack<int> indexes = new Stack<int>();
            for(int i = 0; i < vals.Count; i++)
            {
                if (indexes.Count > 0 && indexes.Peek() == vals[i][1])
                {
                    indexes.Pop();
                }
                else
                {
                    indexes.Push(vals[i][1]);
                }
            }

            WriteLine(indexes.Count == 0 ? "YES" : "NO");
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}