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[]> xys = new List<int[]>();
            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];
                    }

                    if (left > right)
                    {
                        left += right;
                        right = left - right;
                        left -= right;
                    }

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

            vals.Sort();
            var compressed = new Dictionary<int, int>();
            for(int i = 0; i < vals.Count; i++)
            {
                compressed.Add(vals[i], i);
            }
            xys = xys.OrderBy(a => a[0]).ToList();

            SegTree<bool> segTree = new SegTree<bool>(
                vals.Count, false, (a, b) => a | b, false);
            for(int i = 0; i < xys.Count; i++)
            {
                int leftI = compressed[xys[i][0]];
                int rightI = compressed[xys[i][1]];
                if(segTree.Scan(leftI,rightI))
                {
                    WriteLine("NO");
                    return;
                }
                segTree.Update(rightI, true);
            }

            WriteLine("YES");
        }

        class SegTree<T>
        {
            int totalLength;
            T[] tree;
            Func<T, T, T> integrate;
            T exValue;

            public SegTree(int length, Func<T, T, T> integrate, T exValue)
            {
                this.integrate = integrate;
                this.exValue = exValue;
                totalLength = 1;
                while (totalLength < length) totalLength *= 2;

                tree = new T[2 * totalLength - 1];
                for (int i = 0; i < tree.Length; i++) tree[i] = exValue;
            }

            public SegTree(int length, T initialValue,
                Func<T, T, T> integrate, T exValue)
                : this(length, integrate, exValue)
            {
                for (int i = 0; i < length; i++)
                {
                    tree[i + totalLength - 1] = initialValue;
                }
                UpdateAll();
            }

            public void AssignWithoutUpdate(int i, T value)
            {
                tree[i + totalLength - 1] = value;
            }

            public void Update(int i, T value)
            {
                int now = i + totalLength - 1;
                tree[now] = value;
                while (now > 0)
                {
                    now = (now - 1) / 2;
                    tree[now] = integrate(
                        tree[now * 2 + 1], tree[now * 2 + 2]);
                }
            }

            public void UpdateAll()
            {
                for (int i = totalLength - 2; i >= 0; i--)
                {
                    tree[i] = integrate(tree[i * 2 + 1], tree[i * 2 + 2]);
                }
            }

            public T Look(int i) { return tree[i + totalLength - 1]; }

            //[top,last)
            public T Scan(int top, int last)
            {
                return Query(top, last, 0, 0, totalLength);
            }

            T Query(int top, int last, int i, int left, int right)
            {
                if (right <= top || last <= left) return exValue;
                if (top <= left && right <= last) return tree[i];

                T leftValue = Query(top, last, i * 2 + 1,
                    left, (left + right) / 2);
                T rightValue = Query(top, last, (i + 1) * 2,
                    (left + right) / 2, right);

                return integrate(leftValue, rightValue);
            }
        }

        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); }
    }
}