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 Python (3.4.3) (judge verdict: Time Limit Exceeded)

def main():
    from bisect import bisect_left as bl

    class BIT():
        def __init__(self, member):
            self.member_list = sorted(member)
            self.member_dict = {v: i+1 for i, v in enumerate(self.member_list)}
            self.n = len(member)
            self.maxmember = self.member_list[-1]
            self.minmember = self.member_list[0]
            self.maxbit = 2**(len(bin(self.n))-3)
            self.bit = [0]*(self.n+1)
            self.allsum = 0

        # 要素iにvを追加する
        def add(self, i, v):
            x = self.member_dict[i]
            self.allsum += v
            while x < self.n + 1:
                self.bit[x] += v
                x += x & (-x)

        # 位置0からiまでの和(sum(bit[:i]))を計算する
        def sum(self, i):
            ret = 0
            x = i
            while x > 0:
                ret += self.bit[x]
                x -= x & (-x)
            return ret

        # 位置iからjまでの和(sum(bit[i:j]))を計算する
        def sum_range(self, i, j):
            return self.sum(j) - self.sum(i)

        # 和がw以上となる最小のインデックスを求める
        def lowerbound(self, w):
            if w <= 0:
                return 0
            x, k = 0, self.maxbit
            while k:
                if x+k <= self.n and self.bit[x+k] < w:
                    w -= self.bit[x+k]
                    x += k
                k //= 2
            return x

        # vに一番近いv以上の値を求める
        def greater(self, v):
            if v > self.maxmember:
                return None
            p = self.sum(bl(self.member_list, v))
            if p == self.allsum:
                return None
            return self.member_list[self.lowerbound(p+1)]

        # vに一番近いv以下の値を求める
        def smaller(self, v):
            if v < self.minmember:
                return None
            b = bl(self.member_list, v)
            if b == self.n:
                b -= 1
            elif self.member_list[b] != v:
                b -= 1
            p = self.sum(b+1)
            if p == 0:
                return None
            return self.member_list[self.lowerbound(p)]

    r, c, n = map(int, input().split())
    xyzw = [list(map(int, input().split())) for _ in [0]*n]
    outer = []
    for x, y, z, w in xyzw:
        if x in [0, r] or y in [0, c]:
            if z in [0, r] or w in [0, c]:
                if y == 0:
                    p = x
                elif x == r:
                    p = r+y
                elif y == c:
                    p = 2*r+c-x
                else:
                    p = 2*r+2*c-y
                if w == 0:
                    q = z
                elif z == r:
                    q = r+w
                elif w == c:
                    q = 2*r+c-z
                else:
                    q = 2*r+2*c-w
                if p > q:
                    p, q = q, p
                outer.append((p, q))

    member = [i for i, j in outer]+[j for i, j in outer]+[-1]+[2*r+2*c+1]
    bit = BIT(member)
    bit.add(-1, 1)
    bit.add(2*r+2*c+1, 1)

    outer.sort(key=lambda x: x[0]-x[1])
    for a, b in outer:
        if bit.greater(a) < b:
            print("NO")
            return
        bit.add(a, 1)
        bit.add(b, 1)
    print("YES")


main()