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-----
4 2 3
0 1 3 1
1 1 4 1
2 0 2 2

-----Sample Output-----
YES


The above figure shows a possible solution.

Difficulty: HARD

Solutions:
from sys import setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *

def read():
	return int(input())

def reads():
	return [int(x) for x in input().split()]
(R, C, N) = reads()

def edge(x, y):
	return x == 0 or x == R or y == 0 or (y == C)

def flat(x, y):
	assert edge(x, y)
	if y == 0:
		return x
	elif x == R:
		return R + y
	elif y == C:
		return R + C + (R - x)
	else:
		assert x == 0
		return 2 * R + C + (C - y)
ps = []
for i in range(N):
	(x1, y1, x2, y2) = reads()
	if edge(x1, y1) and edge(x2, y2):
		ps.append((flat(x1, y1), i))
		ps.append((flat(x2, y2), i))
ps.sort()
stack = []
for (_, i) in ps:
	if len(stack) > 0 and stack[-1] == i:
		stack.pop()
	else:
		stack.append(i)
print('YES' if len(stack) == 0 else 'NO')


(R, C, N) = map(int, input().split())
E = []
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if 0 < x1 < R and 0 < y1 < C or (0 < x2 < R and 0 < y2 < C):
		continue
	if y1 == 0:
		E.append((x1, i))
	elif x1 == R:
		E.append((R + y1, i))
	elif y1 == C:
		E.append((2 * R + C - x1, i))
	elif x1 == 0:
		E.append((2 * R + 2 * C - y1, i))
	if y2 == 0:
		E.append((x2, i))
	elif x2 == R:
		E.append((R + y2, i))
	elif y2 == C:
		E.append((2 * R + C - x2, i))
	elif x2 == 0:
		E.append((2 * R + 2 * C - y2, i))
E.sort()
visited = [False] * N
stack = []
for p in E:
	if not visited[p[1]]:
		stack.append(p[1])
		visited[p[1]] = True
	elif visited[p[1]]:
		if stack[-1] == p[1]:
			stack.pop()
		else:
			print('NO')
			exit()
print('YES')


(R, C, N) = list(map(int, input().split()))
points = [list(map(int, input().split())) for i in range(N)]

def circ(x, y):
	if y == 0:
		return x
	elif x == R:
		return y + R
	elif y == C:
		return R + C + R - x
	elif x == 0:
		return R + C + R + C - y
	else:
		return -1
L = []
for i in range(N):
	(x1, y1, x2, y2) = points[i]
	if circ(x1, y1) == -1 or circ(x2, y2) == -1:
		continue
	L.append((circ(x1, y1), i))
	L.append((circ(x2, y2), i))
R = [s[1] for s in sorted(L)]
X = []
for i in R:
	if len(X) == 0:
		X.append(i)
	elif X[-1] == i:
		X.pop()
	else:
		X.append(i)
if len(X) == 0:
	print('YES')
else:
	print('NO')


(R, C, N) = list(map(int, input().split()))
points = []

def dist(x, y):
	if x == 0:
		return R * 2 + C + (C - y)
	if x == R:
		return R + y
	if y == 0:
		return x
	if y == C:
		return R + C + (R - x)
for i in range(N):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if ((x1 == 0 or x1 == R) or (y1 == 0 or y1 == C)) and ((x2 == 0 or x2 == R) or (y2 == 0 or y2 == C)):
		points.append((i + 1, dist(x1, y1)))
		points.append((i + 1, dist(x2, y2)))
points.sort(key=lambda p: p[1])
l = []
for point in points:
	if len(l) == 0:
		l.append(point[0])
		continue
	if point[0] == l[-1]:
		l.pop()
	else:
		l.append(point[0])
if len(l) == 0:
	print('YES')
else:
	print('NO')


from cmath import phase
(r, c, n) = map(int, input().split())
l = list()
for i in range(n):
	(w, x, y, z) = map(int, input().split())
	if (w in (0, r) or x in (0, c)) and (y in (0, r) or z in (0, c)):
		l.append((i, w - r / 2 + (x - c / 2) * 1j))
		l.append((i, y - r / 2 + (z - c / 2) * 1j))
l.sort(key=lambda t: phase(t[1]))
p = list()
for (x, c) in l:
	if p == [] or p[-1] != x:
		p.append(x)
	else:
		p.pop()
ans = ['NO', 'YES']
print(ans[p == []])


import sys
input = sys.stdin.readline

def main():
	(R, C, N) = map(int, input().split())
	xyxy = [list(map(int, input().split())) for i in range(N)]
	r = []
	for i in range(N):
		(x1, y1, x2, y2) = xyxy[i]
		if ((x1 == 0 or x1 == R) or (y1 == 0 or y1 == C)) and ((x2 == 0 or x2 == R) or (y2 == 0 or y2 == C)):
			if x1 == 0:
				r.append((y1, i))
			elif x1 == R:
				r.append((C - y1 + C + R, i))
			elif y1 == 0:
				r.append((R - x1 + C * 2 + R, i))
			else:
				r.append((x1 + C, i))
			if x2 == 0:
				r.append((y2, i))
			elif x2 == R:
				r.append((C - y2 + C + R, i))
			elif y2 == 0:
				r.append((R - x2 + C * 2 + R, i))
			else:
				r.append((x2 + C, i))
	r = sorted(r)
	stack = []
	for i in range(len(r)):
		if len(stack) > 0:
			if stack[-1] == r[i][1]:
				stack.pop()
			else:
				stack.append(r[i][1])
		else:
			stack.append(r[i][1])
	if len(stack) > 0:
		print('NO')
	else:
		print('YES')

def __starting_point():
	main()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def main():
	(h, w, n) = MI()
	points = []
	cnt = 0
	for _ in range(n):
		(i0, j0, i1, j1) = MI()
		if 0 < i0 < h and 0 < j0 < w or (0 < i1 < h and 0 < j1 < w):
			continue
		for (i, j) in [(i0, j0), (i1, j1)]:
			if j == 0 or i == h:
				d = i + j
			else:
				d = 2 * (h + w) - i - j
			points.append([d, cnt])
		cnt += 1
	points.sort()
	first = [True] * cnt
	visited = []
	for (d, pi) in points:
		if first[pi]:
			visited.append(pi)
			first[pi] = False
		else:
			last_pi = visited.pop()
			if pi != last_pi:
				print('NO')
				return
	print('YES')
main()


def add(bound, r, c, x, y, i):
	if x == 0:
		bound.append((0, y, i))
	elif y == c:
		bound.append((1, x, i))
	elif x == r:
		bound.append((2, c - y, i))
	else:
		bound.append((3, r - x, i))

def solve(bound):
	if not bound:
		return True
	bound.sort()
	st = [0] * len(bound)
	p = -1
	for (_, _, i) in bound:
		if 0 <= p and st[p] == i:
			p -= 1
		else:
			p += 1
			st[p] = i
	return p == -1

def main():
	(r, c, n) = input().split()
	r = int(r)
	c = int(c)
	n = int(n)
	bound = []
	for i in range(n):
		(x1, y1, x2, y2) = input().split()
		x1 = int(x1)
		y1 = int(y1)
		x2 = int(x2)
		y2 = int(y2)
		if (x1 % r == 0 or y1 % c == 0) and (x2 % r == 0 or y2 % c == 0):
			add(bound, r, c, x1, y1, i)
			add(bound, r, c, x2, y2, i)
	print('YES' if solve(bound) else 'NO')

def __starting_point():
	main()
__starting_point()


(R, C, n) = list(map(int, input().split()))
x1 = []
y1 = []
x2 = []
y2 = []
for i in range(n):
	(xx1, yy1, xx2, yy2) = list(map(int, input().split()))
	x1.append(xx1)
	y1.append(yy1)
	x2.append(xx2)
	y2.append(yy2)
t = []
b = []
l = []
r = []

def add(x, y, i):
	if x == 0:
		t.append((i, y))
	elif x == R:
		b.append((i, y))
	elif y == 0:
		l.append((i, x))
	elif y == C:
		r.append((i, x))
for i in range(n):
	add(x1[i], y1[i], i)
	add(x2[i], y2[i], i)
t = sorted(t, key=lambda x: x[1])
r = sorted(r, key=lambda x: x[1])
b = sorted(b, key=lambda x: x[1], reverse=True)
l = sorted(l, key=lambda x: x[1], reverse=True)
lis = []
lis.extend(t)
lis.extend(r)
lis.extend(b)
lis.extend(l)
lis = [x[0] for x in lis]
cnt = {}
for i in lis:
	if not i in cnt:
		cnt[i] = 0
	cnt[i] += 1
lis = [x for x in lis if cnt[x] == 2]
stk = []
for i in lis:
	if len(stk) == 0 or stk[-1] != i:
		stk.append(i)
	else:
		stk.pop()
if len(stk) == 0:
	print('YES')
else:
	print('NO')


from collections import deque
(r, c, n) = [int(item) for item in input().split()]
on_edge = [[] for _ in range(4)]
for i in range(n):
	(x1, y1, x2, y2) = [int(item) for item in input().split()]
	v1_on_edge = x1 == 0 or x1 == r or y1 == 0 or (y1 == c)
	v2_on_edge = x2 == 0 or x2 == r or y2 == 0 or (y2 == c)
	if v1_on_edge and v2_on_edge:
		if x1 == 0:
			on_edge[0].append([y1, i])
		elif x1 == r:
			on_edge[2].append([y1, i])
		elif y1 == 0:
			on_edge[3].append([x1, i])
		elif y1 == c:
			on_edge[1].append([x1, i])
		if x2 == 0:
			on_edge[0].append([y2, i])
		elif x2 == r:
			on_edge[2].append([y2, i])
		elif y2 == 0:
			on_edge[3].append([x2, i])
		elif y2 == c:
			on_edge[1].append([x2, i])
for i in range(4):
	if i <= 1:
		on_edge[i].sort()
	else:
		on_edge[i].sort(reverse=True)
total = [item for line in on_edge for item in line]
st = deque()
st.append(-1)
for (val, item) in total:
	if st[-1] == item:
		st.pop()
	else:
		st.append(item)
if st[-1] == -1:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
(R, C, N) = list(map(int, input().split()))

def is_on_edge(x, y):
	return x == 0 or x == R or y == 0 or (y == C)
line = []
for i in range(0, N):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if is_on_edge(x1, y1) and is_on_edge(x2, y2):
		line.append([x1, y1, x2, y2])
n = len(line)
query = []
for i in range(n):
	(x1, y1, x2, y2) = line[i]
	if y1 == 0:
		query.append([x1, i])
	elif x1 == R:
		query.append([R + y1, i])
	elif y1 == C:
		query.append([R + C + (R - x1), i])
	else:
		query.append([2 * R + 2 * C - y1, i])
	if y2 == 0:
		query.append([x2, i])
	elif x2 == R:
		query.append([R + y2, i])
	elif y2 == C:
		query.append([R + C + (R - x2), i])
	else:
		query.append([2 * R + 2 * C - y2, i])
query.sort(reverse=True)
que = []
dic = [0 for i in range(n)]
while query:
	(t, num) = query.pop()
	if dic[num] == 0:
		que.append(num)
		dic[num] = 1
	elif que[-1] != num:
		print('NO')
		return
	else:
		dic[num] = 2
		que.pop()
print('YES')


def put(i, x, y):
	if x == 0:
		on_top.append((y, i))
	elif x == r:
		on_bottom.append((y, i))
	elif y == 0:
		on_left.append((x, i))
	else:
		on_right.append((x, i))
(on_top, on_bottom, on_left, on_right) = ([], [], [], [])
(r, c, n) = list(map(int, input().split()))
for i in range(n):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if 0 < x1 < r and 0 < y1 < c:
		continue
	if 0 < x2 < r and 0 < y2 < c:
		continue
	put(i, x1, y1)
	put(i, x2, y2)
on_top.sort()
on_bottom.sort(reverse=True)
on_left.sort(reverse=True)
on_right.sort()
stack = []
for on_edge in [on_top, on_right, on_bottom, on_left]:
	for (p, i) in on_edge:
		if stack and stack[-1] == i:
			stack.pop()
		else:
			stack.append(i)
print('NO' if stack else 'YES')


(w, h, n) = map(int, input().split())
pos = []
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	if (x1 in [0, w] or y1 in [0, h]) and (x2 in [0, w] or y2 in [0, h]):
		pos.append((x1, y1, i))
		pos.append((x2, y2, i))
pos.sort(key=lambda v: v[0])
edge = [v[-1] for v in pos if v[1] == 0 and v[0] != 0]
pos.sort(key=lambda v: v[1])
edge += [v[-1] for v in pos if v[0] == w and v[1] != 0]
pos.sort(key=lambda v: v[0], reverse=True)
edge += [v[-1] for v in pos if v[1] == h and v[0] != w]
pos.sort(key=lambda v: v[1], reverse=True)
edge += [v[-1] for v in pos if v[0] == 0 and v[1] != h]
hist = [None] * n
step = 0
for v in edge:
	if hist[v] is None:
		hist[v] = step + 1
		step += 1
	else:
		if hist[v] != step:
			print('NO')
			break
		step -= 1
else:
	print('YES')


def hantei(x, y):
	if y == 0:
		return 'l'
	elif x == R:
		return 'd'
	elif y == C:
		return 'r'
	elif x == 0:
		return 'u'
	return 'n'
(R, C, N) = list(map(int, input().split()))
u = []
l = []
d = []
r = []
for i in range(N):
	(X1, Y1, X2, Y2) = list(map(int, input().split()))
	if hantei(X1, Y1) != 'n' and hantei(X2, Y2) != 'n':
		if hantei(X1, Y1) == 'l':
			l.append([X1, Y1, i])
		elif hantei(X1, Y1) == 'd':
			d.append([X1, Y1, i])
		elif hantei(X1, Y1) == 'r':
			r.append([X1, Y1, i])
		elif hantei(X1, Y1) == 'u':
			u.append([X1, Y1, i])
		X1 = X2
		Y1 = Y2
		if hantei(X1, Y1) == 'l':
			l.append([X1, Y1, i])
		elif hantei(X1, Y1) == 'd':
			d.append([X1, Y1, i])
		elif hantei(X1, Y1) == 'r':
			r.append([X1, Y1, i])
		elif hantei(X1, Y1) == 'u':
			u.append([X1, Y1, i])
l.sort()
d.sort()
r.sort()
r.reverse()
u.sort()
u.reverse()
ps = []
for i in l:
	ps.append(i[2])
for i in d:
	ps.append(i[2])
for i in r:
	ps.append(i[2])
for i in u:
	ps.append(i[2])
q = []
for i in ps:
	if len(q) > 0 and q[-1] == i:
		del q[-1]
	else:
		q.append(i)
if len(q) == 0:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline

def main():
	(r, c, n) = map(int, input().split())
	L = []

	def trans(x, y):
		z = None
		if x == 0:
			z = y
		if y == c:
			z = c + x
		if x == r:
			z = c + r + c - y
		if y == 0:
			z = 2 * r + 2 * c - x
		return z
	for _ in range(n):
		(x1, y1, x2, y2) = map(int, input().split())
		z1 = trans(x1, y1)
		z2 = trans(x2, y2)
		if z1 is not None and z2 is not None:
			L.append((z1, z2))
			L.append((z2, z1))
	L.sort()
	cnt = 0
	D = dict()
	for (z1, z2) in L:
		if z1 < z2:
			cnt += 1
			D[z1] = cnt
		else:
			if D[z2] != cnt:
				print('NO')
				exit()
			cnt -= 1
	print('YES')
main()


import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7

def II():
	return int(input())

def ILI():
	return list(map(int, input().split()))

def IAI(LINE):
	return [ILI() for __ in range(LINE)]

def IDI():
	return {key: value for (key, value) in ILI()}

def read():
	(R, C, N) = ILI()
	num_point = []
	for __ in range(N):
		(x_1, y_1, x_2, y_2) = ILI()
		num_point.append([(x_1, y_1), (x_2, y_2)])
	return (R, C, N, num_point)

def change_edge_point(R, C, point):
	(x, y) = point
	if x == 0:
		return R * 2 + C + (C - y)
	if x == R:
		return R + y
	if y == 0:
		return x
	if y == C:
		return R + C + (R - x)

def solve(R, C, N, num_point):
	point_double = []
	for (ind, point) in enumerate(num_point):
		(p_1, p_2) = point
		if ((p_1[0] == 0 or p_1[0] == R) or (p_1[1] == 0 or p_1[1] == C)) and ((p_2[0] == 0 or p_2[0] == R) or (p_2[1] == 0 or p_2[1] == C)):
			point_double.append((ind + 1, change_edge_point(R, C, p_1)))
			point_double.append((ind + 1, change_edge_point(R, C, p_2)))
	point_double.sort(key=lambda x: x[1])
	stack = []
	for point in point_double:
		if len(stack) == 0:
			stack.append(point[0])
			continue
		if point[0] == stack[-1]:
			stack.pop()
		else:
			stack.append(point[0])
	if len(stack) == 0:
		return 'YES'
	else:
		return 'NO'

def main():
	params = read()
	print(solve(*params))
main()


(r, c, n) = map(int, input().split())

def po(x, y):
	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)
q = []
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	if (x1 in [0, r] or y1 in [0, c]) and (x2 in [0, r] or y2 in [0, c]):
		q.append((po(x1, y1), i))
		q.append((po(x2, y2), i))
if len(q) == 0:
	exit(print('YES'))
q.sort()
d = []
for (_, i) in q:
	if len(d) == 0:
		d.append(i)
	elif d[-1] == i:
		del d[-1]
	else:
		d.append(i)
if len(d) == 0:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
(R, C, N) = map(int, input().split())
right = []
top = []
left = []
bottom = []
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if (x1 in (0, R) or y1 in (0, C)) and (x2 in (0, R) or y2 in (0, C)):
		if x1 == 0:
			top.append((y1, i))
		elif x1 == R:
			bottom.append((y1, i))
		elif y1 == 0:
			left.append((x1, i))
		elif y1 == C:
			right.append((x1, i))
		if x2 == 0:
			top.append((y2, i))
		elif x2 == R:
			bottom.append((y2, i))
		elif y2 == 0:
			left.append((x2, i))
		elif y2 == C:
			right.append((x2, i))
left.sort(key=lambda p: p[0])
bottom.sort(key=lambda p: p[0])
right.sort(key=lambda p: -p[0])
top.sort(key=lambda p: -p[0])
points = left + bottom + right + top
stack = []
for (x, i) in points:
	if not stack or stack[-1] != i:
		stack.append(i)
	else:
		stack.pop()
print('NO' if stack else 'YES')


from collections import defaultdict, deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return [list(x) for x in sys.stdin.readline().split()]

def S():
	res = list(sys.stdin.readline())
	if res[-1] == '\n':
		return res[:-1]
	return res

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]
sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():

	def check(x, y):
		return (x == 0) | (x == w) | (y == 0) | (y == h)
	(w, h, n) = LI()
	px0 = []
	pxw = []
	py0 = []
	pyh = []
	for i in range(1, n + 1):
		(x, y, s, t) = LI()
		if check(x, y) & check(s, t):
			if x == 0:
				px0.append((y, i))
			elif x == w:
				pxw.append((-y, i))
			elif y == 0:
				py0.append((-x, i))
			else:
				pyh.append((x, i))
			if s == 0:
				px0.append((t, i))
			elif s == w:
				pxw.append((-t, i))
			elif t == 0:
				py0.append((-s, i))
			else:
				pyh.append((s, i))
	px0.sort()
	pxw.sort()
	py0.sort()
	pyh.sort()
	p = px0 + pyh + pxw + py0
	q = deque()
	for (x, i) in p:
		if q:
			qi = q.pop()
			if qi != i:
				q.append(qi)
				q.append(i)
		else:
			q.append(i)
	if q:
		print('NO')
	else:
		print('YES')
	return
solve()


import heapq

def coord_on_edge(x, y, R, C):
	if y == 0:
		return x
	if x == R:
		return R + y
	if y == C:
		return R + C + R - x
	if x == 0:
		return 2 * (R + C) - y
	return None

def on_edge(x1, y1, x2, y2, R, C, i, edge_points):
	c1 = coord_on_edge(x1, y1, R, C)
	c2 = coord_on_edge(x2, y2, R, C)
	if c1 is not None and c2 is not None:
		heapq.heappush(edge_points, (c1, i))
		heapq.heappush(edge_points, (c2, i))

def solve():
	(R, C, N) = list(map(int, input().split()))
	edge_points = []
	for i in range(N):
		(x1, y1, x2, y2) = list(map(int, input().split()))
		on_edge(x1, y1, x2, y2, R, C, i, edge_points)
	q = []
	while edge_points:
		(c, i) = heapq.heappop(edge_points)
		if len(q) and q[-1] == i:
			q.pop()
		else:
			q.append(i)
	if len(q):
		return 'NO'
	return 'YES'
print(solve())


import sys
input = sys.stdin.readline
(r, c, n) = map(int, input().split())
xy = [list(map(int, input().split())) for i in range(n)]
ls = []
for (x1, y1, x2, y2) in xy:
	if (x1 in (0, r) or y1 in (0, c)) and (x2 in (0, r) or y2 in (0, c)):
		k = []
		for (x, y) in ((x1, y1), (x2, y2)):
			if x == 0:
				s = 2 * r + 2 * c - y
			if x == r:
				s = r + y
			if y == 0:
				s = x
			if y == c:
				s = 2 * r + c - x
			k.append(s)
		t = len(ls) // 2 + 1
		ls.append((k[0], t))
		ls.append((k[1], t))
if not ls:
	print('YES')
	return
ls.sort()
lsi = list(map(list, zip(*ls)))[1]
m = len(lsi)
stack = []
for i in lsi:
	if not stack:
		stack.append(i)
	elif stack[-1] == i:
		stack.pop()
	else:
		stack.append(i)
if stack:
	print('NO')
else:
	print('YES')


import sys
from collections import defaultdict

def read_int_list():
	return list(map(int, input().split()))

def read_int():
	return int(input())

def read_str_list():
	return input().split()

def read_str():
	return input()

def pos(X, Y):
	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
(r, c, n) = read_int_list()
x = [0, 0]
y = [0, 0]
a = []
for i in range(n):
	(x[0], y[0], x[1], y[1]) = read_int_list()
	if (x[0] in [0, r] or y[0] in [0, c]) and (x[1] in [0, r] or y[1] in [0, c]):
		for k in range(2):
			a.append((pos(x[k], y[k]), i))
m = len(a) // 2
a.sort()
res = 'YES'
started = set()
s = []
for (p, i) in a:
	if i not in started:
		started.add(i)
		s.append(i)
	else:
		if s[-1] != i:
			res = 'NO'
			break
		s.pop()
print(res)


(R, C, N) = list(map(int, input().split()))
point = []

def XtoZ(x, y):
	if x == 0:
		if y == 0:
			return 0
		else:
			return 2 * R + 2 * C - y
	elif x == R:
		return R + y
	elif y == 0:
		return x
	elif y == C:
		return 2 * R + C - x
for i in range(N):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if ((x1 == 0 or x1 == R) or (y1 == 0 or y1 == C)) and ((x2 == 0 or x2 == R) or (y2 == 0 or y2 == C)):
		point.append([XtoZ(x1, y1), i])
		point.append([XtoZ(x2, y2), i])
point.sort(key=lambda x: x[0])
flag = [0] * N
from collections import deque
Q = deque()
flag = [True] * N
flag2 = False
for p in point:
	if flag[p[1]]:
		Q.append(p[1])
		flag[p[1]] = False
	elif Q.pop() != p[1]:
		flag2 = True
		break
if flag2:
	print('NO')
else:
	print('YES')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def main():
	(R, C, N) = LI()
	a = []

	def ff(x, y):
		if x == 0:
			return (0, y)
		if x == R:
			return (R, -y)
		if y == 0:
			return (R + 1, -x)
		return (1, x)
	for _ in range(N):
		(x1, y1, x2, y2) = LI()
		if 0 < x1 < R and 0 < y1 < C or (0 < x2 < R and 0 < y2 < C):
			continue
		a.append((ff(x1, y1), _))
		a.append((ff(x2, y2), _))
	a.sort()
	b = [a[i][1] for i in range(len(a))]
	s = [None] * len(b)
	si = -1
	for c in b:
		if si < -1:
			si += 1
			s[0] = c
			continue
		if s[si] == c:
			si -= 1
		else:
			si += 1
			s[si] = c
	if si > 1:
		return 'NO'
	return 'YES'
print(main())


(R, C, N) = map(int, input().split())

def outer_pos(x, y):
	if 0 < x < R and 0 < y < C:
		return -1
	if y == 0:
		return x
	if x == R:
		return R + y
	if y == C:
		return R + C + (R - x)
	return R + C + R + (C - y)
outer = []
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	p1 = outer_pos(x1, y1)
	p2 = outer_pos(x2, y2)
	if p1 < 0 or p2 < 0:
		continue
	outer.append((p1, i))
	outer.append((p2, i))
outer.sort()
stack = []
for (p, i) in outer:
	if len(stack) == 0 or stack[-1] != i:
		stack.append(i)
	else:
		stack.pop()
print('NO' if stack else 'YES')


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

		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

		def sum(self, i):
			ret = 0
			x = i
			while x > 0:
				ret += self.bit[x]
				x -= x & -x
			return ret

		def sum_range(self, i, j):
			return self.sum(j) - self.sum(i)

		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

		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)]

		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) = list(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()


def inpl():
	return [int(i) for i in input().split()]
(R, C, N) = inpl()
loopx = {0: [], R: []}
loopy = {0: [], C: []}
for i in range(N):
	(x1, y1, x2, y2) = inpl()
	if (x1 in (0, R) or y1 in (0, C)) and (x2 in (0, R) or y2 in (0, C)):
		if x1 in (0, R):
			loopx[x1].append((y1, i))
		elif y1 in (0, C):
			loopy[y1].append((x1, i))
		if x2 in (0, R):
			loopx[x2].append((y2, i))
		elif y2 in (0, C):
			loopy[y2].append((x2, i))
loop = [j for (i, j) in sorted(loopy[0])] + [j for (i, j) in sorted(loopx[R])] + [j for (i, j) in sorted(loopy[C])][::-1] + [j for (i, j) in sorted(loopx[0])][::-1]
stack = []
for i in loop:
	if not stack:
		stack.append(i)
		continue
	if stack[-1] == i:
		stack.pop()
	else:
		stack.append(i)
if not stack:
	print('YES')
else:
	print('NO')


(R, C, N) = map(int, input().split())
pts = []
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	zs = []
	for (x, y) in [(x1, y1), (x2, y2)]:
		if y == 0:
			zs.append((x, i))
		elif x == R:
			zs.append((R + y, i))
		elif y == C:
			zs.append((2 * R + C - x, i))
		elif x == 0:
			zs.append((2 * R + 2 * C - y, i))
	if len(zs) == 2:
		pts += zs
pts.sort()
stack = []
for (z, i) in pts:
	if not stack:
		stack.append(i)
	elif stack[-1] == i:
		stack.pop()
	else:
		stack.append(i)
if not stack:
	print('YES')
else:
	print('NO')


(w, h, n) = map(int, input().split())
vec = []
l = []
b = []
r = []
t = []
for i in range(n):
	(x, y, x1, y1) = map(int, input().split())
	c = [x, y, x1, y1]
	if ((c[0] == 0 or c[0] == w) or (c[1] == 0 or c[1] == h)) and ((c[2] == 0 or c[2] == w) or (c[3] == 0 or c[3] == h)):
		if x == 0:
			l.append([x, y, i])
		elif y == 0:
			t.append([x, y, i])
		elif x == w:
			r.append([x, y, i])
		elif y == h:
			b.append([x, y, i])
		if x1 == 0:
			l.append([x1, y1, i])
		elif y1 == 0:
			t.append([x1, y1, i])
		elif x1 == w:
			r.append([x1, y1, i])
		elif y1 == h:
			b.append([x1, y1, i])
sorted_node = sorted(l, key=lambda x: x[1]) + sorted(b, key=lambda x: x[0]) + sorted(r, key=lambda x: x[1], reverse=True) + sorted(t, key=lambda x: x[0], reverse=True)
stack = []
for node in sorted_node:
	if not stack or stack[-1] != node[2]:
		stack.append(node[2])
	else:
		stack.pop()
print('NO' if stack else 'YES')


def calc(x, y):
	if y == 0:
		return x
	elif x == r:
		return r + y
	elif y == c:
		return r * 2 + c - x
	elif x == 0:
		return r * 2 + c * 2 - y
	else:
		return -1
(r, c, n) = map(int, input().split())
pts = []
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	(u, v) = (calc(x1, y1), calc(x2, y2))
	if u >= 0 and v >= 0:
		pts.append((u, i))
		pts.append((v, i))
pts = sorted(pts)
stk = []
for (a, b) in pts:
	if len(stk) == 0 or stk[-1] != b:
		stk.append(b)
	else:
		stk.pop()
if len(stk):
	print('NO')
else:
	print('YES')


(r, c, n) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
iru = []
for i in range(n):
	if (a[i][0] == 0 or a[i][0] == r or a[i][1] == 0 or (a[i][1] == c)) and (a[i][2] == 0 or a[i][2] == r or a[i][3] == 0 or (a[i][3] == c)):
		b = a[i]
		b1 = [b[0], b[1]]
		b2 = [b[2], b[3]]
		if b1[0] == 0:
			b1 = b1[1]
		elif b1[1] == c:
			b1 = b1[0] + b1[1]
		elif b1[0] == r:
			b1 = c + r + c - b1[1]
		else:
			b1 = c * 2 + r * 2 - b1[0]
		if b2[0] == 0:
			b2 = b2[1]
		elif b2[1] == c:
			b2 = b2[0] + b2[1]
		elif b2[0] == r:
			b2 = c * 2 + r - b2[1]
		else:
			b2 = c * 2 + r * 2 - b2[0]
		if b1 > b2:
			tmp = b1 + 0
			b1 = b2 + 0
			b2 = tmp + 0
		iru.append([b1, b2])
iru = sorted(iru)
ato = 0
stack = [r + r + c + c]
flag = 'YES'
for (a, b) in iru:
	while a >= stack[-1]:
		stack.pop()
	if b > stack[-1]:
		flag = 'NO'
		break
	else:
		stack.append(b)
print(flag)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
(r, c, n) = map(int, readline().split())
data = list(map(int, read().split()))
p = []
for i in range(n):
	(x1, y1, x2, y2) = data[i * 4:i * 4 + 4]
	if ((x1 == 0) | (x1 == r) | (y1 == 0) | (y1 == c)) & ((x2 == 0) | (x2 == r) | (y2 == 0) | (y2 == c)):
		for (x, y) in zip([x1, x2], [y1, y2]):
			num = 0
			if x == 0:
				num = y
			elif x == r:
				num = r + 2 * c - y
			elif y == 0:
				num = 2 * (r + c) - x
			else:
				num = c + x
			p.append([num, i])
p.sort()
d = []
for (_, i) in p:
	if not d:
		d.append(i)
	else:
		right = d.pop()
		if right != i:
			d.append(right)
			d.append(i)
if len(d) == 0:
	print('YES')
else:
	print('NO')


(R, C, N) = list(map(int, input().split()))
(UP, RIGHT, DOWN, LEFT) = ([], [], [], [])
for i in range(N):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if 0 < x1 < R and 0 < y1 < C:
		continue
	if 0 < x2 < R and 0 < y2 < C:
		continue
	if x1 == 0:
		UP.append([i, y1])
	elif x1 == R:
		DOWN.append([i, y1])
	elif y1 == 0:
		LEFT.append([i, x1])
	elif y1 == C:
		RIGHT.append([i, x1])
	if x2 == 0:
		UP.append([i, y2])
	elif x2 == R:
		DOWN.append([i, y2])
	elif y2 == 0:
		LEFT.append([i, x2])
	elif y2 == C:
		RIGHT.append([i, x2])
UP.sort(key=lambda x: x[1])
RIGHT.sort(key=lambda x: x[1])
DOWN.sort(key=lambda x: x[1], reverse=True)
LEFT.sort(key=lambda x: x[1], reverse=True)
Numbers = UP + RIGHT + DOWN + LEFT
stack = []
for (n, z) in Numbers:
	if stack and stack[-1] == n:
		stack.pop()
	else:
		stack.append(n)
print('NO' if stack else 'YES')


(r, c, n) = map(int, input().split())
points = []
xx = [0, r]
yy = [0, c]

def add(x, y):
	if x == 0:
		points.append(r * 2 + c + c - y)
	elif x == r:
		points.append(r + y)
	elif y == 0:
		points.append(x)
	else:
		points.append(r + c + r - x)
for i in range(n):
	(x0, y0, x1, y1) = map(int, input().split())
	if (x0 in xx or y0 in yy) and (x1 in xx or y1 in yy):
		add(x0, y0)
		add(x1, y1)
index = list(range(len(points)))
index.sort(key=lambda a: points[a])
que = []
flag = True
for i in index:
	if que and que[-1] // 2 == i // 2:
		que.pop()
	else:
		que.append(i)
if que:
	print('NO')
else:
	print('YES')


(r, c, n) = list(map(int, input().split()))
p = []
for i in range(n):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if x1 == 0 or x1 == r or y1 == 0 or (y1 == c):
		if x2 == 0 or x2 == r or y2 == 0 or (y2 == c):
			if y1 == 0:
				p.append([x1, i])
			elif x1 == r:
				p.append([r + y1, i])
			elif y1 == c:
				p.append([2 * r + c - x1, i])
			else:
				p.append([2 * r + 2 * c - y1, i])
			if y2 == 0:
				p.append([x2, i])
			elif x2 == r:
				p.append([r + y2, i])
			elif y2 == c:
				p.append([2 * r + c - x2, i])
			else:
				p.append([2 * r + 2 * c - y2, i])
p.sort()
t = []
old = -1
for i in range(0, len(p)):
	temp = p[i][1]
	if temp == old:
		t.pop()
		if len(t) == 0:
			old = -1
		else:
			old = t[-1]
	else:
		t.append(temp)
		old = temp
if len(t) > 0:
	print('NO')
else:
	print('YES')


import sys
read = sys.stdin.read
readline = sys.stdin.readline
(h, w, n, *xyxy) = map(int, read().split())

def f(x, y):
	if y == 0:
		return x
	elif x == h:
		return h + y
	elif y == w:
		return h + w + h - x
	else:
		return h + w + h + w - y
m = iter(xyxy)
res = []
idx = 0
for (x1, y1, x2, y2) in zip(m, m, m, m):
	if (x1 * (h - x1) == 0 or y1 * (w - y1) == 0) and (x2 * (h - x2) == 0 or y2 * (w - y2) == 0):
		res.append((f(x1, y1), idx))
		res.append((f(x2, y2), idx))
		idx += 1
res.sort()
st = []
for (d, i) in res:
	if st and st[-1] == i:
		st.pop()
	else:
		st.append(i)
if st:
	print('NO')
else:
	print('YES')


def main():
	import sys
	input = sys.stdin.readline
	(R, C, N) = map(int, input().split())
	P = []
	X = {0, R}
	Y = {0, C}
	M = 0
	for i in range(N):
		(x1, y1, x2, y2) = map(int, input().split())
		if (x1 in X or y1 in Y) and (x2 in X or y2 in Y):
			if y1 == 0:
				L1 = x1
			elif x1 == R:
				L1 = R + y1
			elif y1 == C:
				L1 = 2 * R + C - x1
			else:
				L1 = 2 * (R + C) - y1
			if y2 == 0:
				L2 = x2
			elif x2 == R:
				L2 = R + y2
			elif y2 == C:
				L2 = 2 * R + C - x2
			else:
				L2 = 2 * (R + C) - y2
			P.append([min(L1, L2), max(L1, L2)])
	P = sorted(P, key=lambda a: a[0])
	M = len(P)
	flag = 0
	Q = []
	for i in range(M):
		(x1, x2) = P[i]
		if Q == []:
			Q.append([x1, x2])
		else:
			while Q != [] and Q[-1][1] < x1:
				Q.pop()
			if Q == []:
				Q.append([x1, x2])
			else:
				(y1, y2) = Q[-1]
				if y2 < x2:
					flag = 1
					break
				else:
					Q.append([x1, x2])
	if flag == 1:
		print('NO')
	else:
		print('YES')
main()


import cmath
(r, c, n) = map(int, input().split())
lis = []
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	if 0 < x1 < r and 0 < y1 < c or (0 < x2 < r and 0 < y2 < c):
		continue
	else:
		lis.append((i, x1 - r / 2 + (y1 - c / 2) * 1j))
		lis.append((i, x2 - r / 2 + (y2 - c / 2) * 1j))
lis.sort(key=lambda x: cmath.phase(x[1]))
stack = []
for i in lis:
	if stack == [] or stack[-1] != i[0]:
		stack.append(i[0])
	else:
		stack.pop()
if stack == []:
	print('YES')
else:
	print('NO')


(R, C, N) = map(int, input().split())
edge = []

def F(x, y):
	if x == 0:
		return y
	if y == C:
		return C + x
	if x == R:
		return R + C * 2 - y
	if y == 0:
		return 2 * R + 2 * C - x
	return -1
for i in range(1, N + 1):
	(x1, y1, x2, y2) = map(int, input().split())
	d1 = F(x1, y1)
	d2 = F(x2, y2)
	if d1 < 0:
		continue
	if d2 < 0:
		continue
	edge.append((d1, i))
	edge.append((d2, i))
edge.sort()
stack = []
used = [False] * (N + 1)
ans = 'YES'
for (_, x) in edge:
	if not used[x]:
		used[x] = True
		stack.append(x)
	else:
		y = stack.pop()
		if x != y:
			ans = 'NO'
			break
print(ans)


import sys
input = sys.stdin.readline
import math
(R, C, N) = map(int, input().split())
pts = []
is_inner = lambda x, y: 0 < x < R and 0 < y < C
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if is_inner(x1, y1) or is_inner(x2, y2):
		continue
	pts.append((math.atan2(y1 - C / 2, x1 - R / 2), i))
	pts.append((math.atan2(y2 - C / 2, x2 - R / 2), i))
pts.sort()
arr = []
for (_, i) in pts:
	if arr and arr[-1] == i:
		arr.pop()
		continue
	arr.append(i)
answer = 'NO' if arr else 'YES'
print(answer)


import sys
input = lambda : sys.stdin.readline().rstrip()

def calc(a, b):
	if b == 0:
		return a
	if a == R:
		return b + R
	if b == C:
		return R + C + (R - a)
	if a == 0:
		return R + C + R + (C - b)
(R, C, N) = map(int, input().split())
A = []
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if (x1 == 0 or x1 == R or y1 == 0 or (y1 == C)) and (x2 == 0 or x2 == R or y2 == 0 or (y2 == C)):
		A.append((calc(x1, y1), i))
		A.append((calc(x2, y2), i))
A = [l[1] for l in sorted(A, key=lambda x: x[0])]
B = []
while A:
	while len(B) and A[-1] == B[-1]:
		A.pop()
		B.pop()
	if A:
		B.append(A.pop())
print('NO' if len(B) else 'YES')


(w, h, n) = list(map(int, input().split()))

def proj(x, y):
	if y == 0:
		return x
	elif x == w:
		return w + y
	elif y == h:
		return w + h + (w - x)
	elif x == 0:
		return w + h + w + (h - y)
	else:
		return None
ps = []
for i in range(n):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	p1 = proj(x1, y1)
	p2 = proj(x2, y2)
	if p1 is not None and p2 is not None:
		ps += [(p1, i), (p2, i)]
ps.sort()
stk = []
for (_, i) in ps:
	if stk and stk[-1] == i:
		stk.pop()
	else:
		stk.append(i)
result = not stk
print(['NO', 'YES'][result])


class BIT:

	def __init__(self, n):
		self.n = n
		self.bit = [0] * (n + 1)

	def _add(self, i, val):
		while i > 0:
			self.bit[i] += val
			i -= i & -i

	def get_val(self, i):
		i = i + 1
		s = 0
		while i <= self.n:
			s += self.bit[i]
			i += i & -i
		return s

	def add(self, l, r, val):
		self._add(r, val)
		self._add(l, -val)
from operator import itemgetter

def compress(array):
	set_ = set()
	for num in array:
		set_.add(num[0])
		set_.add(num[1])
	array2 = sorted(set_)
	memo = {value: index for (index, value) in enumerate(array2)}
	for i in range(len(array)):
		array[i] = (memo[array[i][0]], memo[array[i][1]])
	return array

def on_line(x, y):
	return x == 0 or x == r or y == 0 or (y == c)

def val(x, y):
	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)
(r, c, n) = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(n)]
res = []
for i in range(n):
	(x1, y1, x2, y2) = info[i]
	if on_line(x1, y1) and on_line(x2, y2):
		left = val(x1, y1)
		right = val(x2, y2)
		if left > right:
			(left, right) = (right, left)
		res.append((left, right))
res = sorted(res, key=itemgetter(1))
res = compress(res)
bit = BIT(2 * len(res))
for (left, right) in res:
	if bit.get_val(left) != 0:
		print('NO')
		return
	else:
		bit.add(left, right + 1, 1)
print('YES')


import sys
from collections import deque

def MI():
	return list(map(int, sys.stdin.readline().rstrip().split()))
(R, C, N) = MI()
(A1, A2, A3, A4) = ([], [], [], [])

def f(x, y):
	if x == 0 or x == R or y == 0 or (y == C):
		return True
	return False
for i in range(1, N + 1):
	(x1, y1, x2, y2) = MI()
	if not (f(x1, y1) and f(x2, y2)):
		continue
	if y1 == 0:
		A1.append((x1, i))
	elif y1 == C:
		A3.append((-x1, i))
	elif x1 == 0:
		A4.append((-y1, i))
	else:
		A2.append((y1, i))
	if y2 == 0:
		A1.append((x2, i))
	elif y2 == C:
		A3.append((-x2, i))
	elif x2 == 0:
		A4.append((-y2, i))
	else:
		A2.append((y2, i))
A1.sort()
A2.sort()
A3.sort()
A4.sort()
A = A1 + A2 + A3 + A4
deq = deque([])
flag = [0] * (N + 1)
for (z, i) in A:
	if flag[i] == 0:
		flag[i] = 1
		deq.append(i)
	else:
		j = deq.pop()
		if j != i:
			print('NO')
			return
else:
	print('YES')


(R, C, n) = list(map(int, input().split()))
pair = [list(map(int, input().split())) for _ in range(n)]
u = []
r = []
d = []
l = []
for i in range(n):
	(x1, y1, x2, y2) = pair[i]
	if 0 < x1 < R and 0 < y1 < C:
		continue
	if 0 < x2 < R and 0 < y2 < C:
		continue
	if x1 == 0:
		u.append([y1, i])
	if x1 == R:
		d.append([y1, i])
	if y1 == 0:
		if x1 != 0 and x1 != R:
			l.append([x1, i])
	if y1 == C:
		if x1 != 0 and x1 != R:
			r.append([x1, i])
	if x2 == 0:
		u.append([y2, i])
	if x2 == R:
		d.append([y2, i])
	if y2 == 0:
		if x2 != 0 and x2 != R:
			l.append([x2, i])
	if y2 == C:
		if x2 != 0 and x2 != R:
			r.append([x2, i])
u.sort()
r.sort()
d.sort(reverse=True)
l.sort(reverse=True)
urdl = u + r + d + l
stack = []
for i in range(len(urdl)):
	if len(stack) == 0:
		stack.append(urdl[i][1])
	elif stack[-1] == urdl[i][1]:
		stack.pop()
	else:
		stack.append(urdl[i][1])
print('YES' if len(stack) == 0 else 'NO')


(R, C, N) = map(int, input().split())
pts = [tuple(map(int, input().split())) for _ in range(N)]

def on_boundary(x1, y1):
	return x1 == 0 or x1 == R or y1 == 0 or (y1 == C)

def perim_until(x, y):
	if x == 0:
		return y
	if y == C:
		return C + x
	if x == R:
		return R + C + (C - y)
	if y == 0:
		return R + C + C + (R - x)
	return -1
perimpts = []
for (i, pt) in enumerate(pts):
	(x1, y1, x2, y2) = pt
	if on_boundary(x1, y1) and on_boundary(x2, y2):
		(per1, per2) = (perim_until(x1, y1), perim_until(x2, y2))
		perimpts.append((per1, i))
		perimpts.append((per2, i))
perimpts.sort()

def solve():
	stack = []
	seen = [False for _ in range(N)]
	for ppt in perimpts:
		(_, ind) = ppt
		if seen[ind]:
			if stack[-1] != ind:
				print('NO')
				return
			stack.pop()
		else:
			seen[ind] = True
			stack.append(ind)
	print('YES')
solve()


import sys
input = sys.stdin.readline
from operator import itemgetter
(X, Y, N) = map(int, input().split())
U = []
D = []
R = []
L = []

def mustconsider(x, y):
	return x in [0, X] or y in [0, Y]

def divide(x, y, i):
	if x == 0 and y != Y:
		L.append((y, i))
	if x == X and y != 0:
		R.append((y, i))
	if y == 0 and x != 0:
		D.append((x, i))
	if y == Y and x != X:
		U.append((x, i))
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if mustconsider(x1, y1) and mustconsider(x2, y2):
		divide(x1, y1, i)
		divide(x2, y2, i)
D.sort()
R.sort()
U.sort(reverse=True)
L.sort(reverse=True)
Arounds = []
for (_, ind) in D:
	Arounds.append(ind)
for (_, ind) in R:
	Arounds.append(ind)
for (_, ind) in U:
	Arounds.append(ind)
for (_, ind) in L:
	Arounds.append(ind)
stack = []
for a in Arounds:
	if not stack:
		stack.append(a)
	elif stack[-1] == a:
		stack.pop()
	else:
		stack.append(a)
print('YES' if not stack else 'NO')


(R, C, N) = map(int, input().split())

def makexy(x1, y1):
	if y1 == 0:
		return x1
	elif x1 == R:
		return R + y1
	elif y1 == C:
		return R * 2 + C - x1
	else:
		return R * 2 + C * 2 - y1
XY = []
XY2 = []
D = []
for _ in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if (x1 in [0, R] or y1 in [0, C]) and (x2 in [0, R] or y2 in [0, C]):
		xy1 = makexy(x1, y1)
		xy2 = makexy(x2, y2)
		(xy1, xy2) = sorted((xy1, xy2))
		XY.append((xy1, xy2))
		XY2.append((xy1, 0))
		XY2.append((xy2, 1))
		D.append(xy2 - xy1)
XY.sort()
XY2.sort()
D.sort()
D2 = []
Stack = []
for xy in XY2:
	if xy[1] == 0:
		Stack.append(xy)
	else:
		D2.append(xy[0] - Stack[-1][0])
		del Stack[-1]
D2.sort()
if D == D2:
	print('YES')
else:
	print('NO')


(R, C, N) = map(int, input().split())

def calc(x, y):
	if x == 0 or y == C:
		return x + y
	return 2 * R + 2 * C - x - y
A = {}
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if not ((x1 in [0, R] or y1 in [0, C]) and (x2 in [0, R] or y2 in [0, C])):
		continue
	A[calc(x1, y1)] = i
	A[calc(x2, y2)] = i
st = []
for (i, a) in sorted(A.items()):
	if st and st[-1] == a:
		st.pop()
	elif a is not None:
		st.append(a)
print('YES' if not st else 'NO')


(r, c, n) = list(map(int, input().split()))
l = []
for i in range(n):
	(x1, y1, x2, y2) = list(map(int, input().split()))
	if (x1 == 0 or x1 == r or y1 == 0 or (y1 == c)) and (x2 == 0 or x2 == r or y2 == 0 or (y2 == c)):
		if x1 == 0:
			l.append((y1, i))
		elif y1 == c:
			l.append((c + x1, i))
		elif x1 == r:
			l.append((c * 2 + r - y1, i))
		else:
			l.append((r * 2 + c * 2 - x1, i))
		if x2 == 0:
			l.append((y2, i))
		elif y2 == c:
			l.append((c + x2, i))
		elif x2 == r:
			l.append((c * 2 + r - y2, i))
		else:
			l.append((r * 2 + c * 2 - x2, i))
l.sort()
s = []
d = [False] * n
for (x, i) in l:
	if d[i]:
		if s[-1] != i:
			print('NO')
			return
		s.pop()
	else:
		s.append(i)
		d[i] = True
print('YES')


import sys
input = sys.stdin.readline
(R, C, N) = map(int, input().split())
a = []

def conv(i, j):
	res = 0
	if i == 0:
		res = j
	elif j == C:
		res = C + i
	elif i == R:
		res = C + R + (C - j)
	elif j == 0:
		res = 2 * C + R + (R - i)
	else:
		res = -1
	return res
for i in range(N):
	(x, y, u, v) = map(int, input().split())
	(d, dd) = (conv(x, y), conv(u, v))
	if d >= 0 and dd >= 0:
		a.append((d, i))
		a.append((dd, i))
a.sort()
s = []
for (_, i) in a:
	if len(s) and s[-1] == i:
		s.pop()
	else:
		s.append(i)
if len(s):
	print('NO')
else:
	print('YES')


(r, c, n) = map(int, input().split())
dots = []
num = 0
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	if 0 < x1 < r and 0 < y1 < c or (0 < x2 < r and 0 < y2 < c):
		continue
	else:
		num += 1
		d1 = (x1, y1)
		d2 = (x2, y2)
		if x1 == 0:
			dots.append((y1, i))
		elif y1 == c:
			dots.append((c + x1, i))
		elif x1 == r:
			dots.append((2 * c + r - y1, i))
		else:
			dots.append((2 * (r + c) - x1, i))
		if x2 == 0:
			dots.append((y2, i))
		elif y2 == c:
			dots.append((c + x2, i))
		elif x2 == r:
			dots.append((2 * c + r - y2, i))
		else:
			dots.append((2 * (r + c) - x2, i))
if num <= 1:
	print('YES')
else:
	dots.sort(key=lambda x: x[0])
	stack = []
	for i in range(num * 2):
		if stack == []:
			stack.append(dots[i][1])
		elif stack[-1] == dots[i][1]:
			stack.pop()
		else:
			stack.append(dots[i][1])
	if stack:
		print('NO')
	else:
		print('YES')


import sys, math, collections, heapq, itertools
F = sys.stdin

def single_input():
	return F.readline().strip('\n')

def line_input():
	return F.readline().strip('\n').split()

def solve():
	(R, C, N) = map(int, line_input())
	(u, l, r, d) = ([], [], [], [])
	for i in range(N):
		(x, y, z, w) = map(int, line_input())
		if x == 0:
			if z == 0:
				u.append((y, i))
				u.append((w, i))
			elif w == C:
				u.append((y, i))
				r.append((z, i))
			elif z == R:
				u.append((y, i))
				d.append((w, i))
			elif w == 0:
				u.append((y, i))
				l.append((z, i))
		elif x == R:
			if z == 0:
				d.append((y, i))
				u.append((w, i))
			elif w == C:
				d.append((y, i))
				r.append((z, i))
			elif z == R:
				d.append((y, i))
				d.append((w, i))
			elif w == 0:
				d.append((y, i))
				l.append((z, i))
		elif y == 0:
			if z == 0:
				l.append((x, i))
				u.append((w, i))
			elif w == C:
				l.append((x, i))
				r.append((z, i))
			elif z == R:
				l.append((x, i))
				d.append((w, i))
			elif w == 0:
				l.append((x, i))
				l.append((z, i))
		elif y == C:
			if z == 0:
				r.append((x, i))
				u.append((w, i))
			elif w == C:
				r.append((x, i))
				r.append((z, i))
			elif z == R:
				r.append((x, i))
				d.append((w, i))
			elif w == 0:
				r.append((x, i))
				l.append((z, i))
	u.sort()
	r.sort()
	d.sort(reverse=True)
	l.sort(reverse=True)
	s = []
	used = set()
	crossed = True
	for (point, n) in u:
		if n not in used:
			s.append(n)
			used |= {n}
		elif s[-1] != n:
			break
		else:
			s.pop()
	else:
		for (point, n) in r:
			if n not in used:
				s.append(n)
				used |= {n}
			elif s[-1] != n:
				break
			else:
				s.pop()
		else:
			for (point, n) in d:
				if n not in used:
					s.append(n)
					used |= {n}
				elif s[-1] != n:
					break
				else:
					s.pop()
			else:
				for (point, n) in l:
					if n not in used:
						s.append(n)
						used |= {n}
					elif s[-1] != n:
						break
					else:
						s.pop()
				else:
					crossed = False
	print('NO' if crossed else 'YES')
	return 0

def __starting_point():
	solve()
__starting_point()


from collections import deque

def f(x, y):
	if x == 0:
		return y
	if y == 0:
		return -x
	if x == r:
		return -(x + y)
	if y == c:
		return x + y
(r, c, n) = map(int, input().split())
xy = []
lxy = 0
for i in range(n):
	(x1, y1, x2, y2) = map(int, input().split())
	d = []
	if min(x1, y1) == 0 or x1 == r or y1 == c:
		d.append([f(x1, y1), i])
	if min(x2, y2) == 0 or x2 == r or y2 == c:
		d.append([f(x2, y2), i])
	if len(d) == 2:
		xy.append(d[0])
		xy.append(d[1])
		lxy += 2
xy.sort()
q = deque()
lq = -1
for i in range(lxy):
	if not q:
		q.append(xy[i][1])
		lq += 1
	elif q[lq] == xy[i][1]:
		q.pop()
		lq -= 1
	else:
		q.append(xy[i][1])
		lq += 1
print('YES' if not q else 'NO')


def ri():
	return int(input())

def rli():
	return list(map(int, input().split()))

def rls():
	return list(input())

def pli(a):
	return ''.join(list(map(str, a)))
(R, C, N) = rli()
lis = []
idx = 0
i = 0
for j in range(N):
	(x1, y1, x2, y2) = rli()
	if (x1 % R == 0 or y1 % C == 0) and (x2 % R == 0 or y2 % C == 0):
		i += 1
		for _ in range(2):
			if y1 == 0:
				lis.append([x1, i])
			elif x1 == R:
				lis.append([R + y1, i])
			elif y1 == C:
				lis.append([2 * R + C - x1, i])
			elif x1 == 0:
				lis.append([2 * R + 2 * C - y1, i])
			x1 = x2
			y1 = y2
lis.sort()
stack = []
exist = [False for _ in range(2 * i)]
ans = True
for k in range(2 * i):
	if exist[lis[k][1]]:
		if lis[k][1] != stack.pop():
			ans = False
			break
		exist[lis[k][1]] = False
	else:
		stack.append(lis[k][1])
		exist[lis[k][1]] = True
if ans:
	print('YES')
else:
	print('NO')


from operator import itemgetter
import collections
(r, c, n) = map(int, input().split())
l = [list(map(int, input().split())) for i in range(n)]
e1 = []
e2 = []
e3 = []
e4 = []
for i in range(n):
	if (l[i][0] == 0 or l[i][0] == r or l[i][1] == 0 or (l[i][1] == c)) and (l[i][2] == 0 or l[i][2] == r or l[i][3] == 0 or (l[i][3] == c)):
		if l[i][0] == 0 and l[i][1] != c:
			e1.append([i + 1, l[i][0], l[i][1]])
		elif l[i][1] == c and l[i][0] != r:
			e2.append([i + 1, l[i][0], l[i][1]])
		elif l[i][0] == r and l[i][1] != 0:
			e3.append([i + 1, l[i][0], l[i][1]])
		else:
			e4.append([i + 1, l[i][0], l[i][1]])
		if l[i][2] == 0 and l[i][3] != c:
			e1.append([i + 1, l[i][2], l[i][3]])
		elif l[i][3] == c and l[i][2] != r:
			e2.append([i + 1, l[i][2], l[i][3]])
		elif l[i][2] == r and l[i][3] != 0:
			e3.append([i + 1, l[i][2], l[i][3]])
		else:
			e4.append([i + 1, l[i][2], l[i][3]])
e1 = sorted(e1, key=itemgetter(2))
e2 = sorted(e2, key=itemgetter(1))
e3 = sorted(e3, key=itemgetter(2))
e4 = sorted(e4, key=itemgetter(1))
e3.reverse()
e4.reverse()
e = e1 + e2 + e3 + e4
q = []
q = collections.deque(q)
for i in range(len(e)):
	if len(q) == 0:
		q.append(e[i][0])
	elif q[-1] == e[i][0]:
		q.pop()
	else:
		q.append(e[i][0])
if len(q) == 0:
	print('YES')
else:
	print('NO')


def main(r, c, n, xy):
	ary = []
	for (i, (x1, y1, x2, y2)) in enumerate(xy):
		if x1 in (0, r) or y1 in (0, c):
			if x2 in (0, r) or y2 in (0, c):
				for (x, y) in ((x1, y1), (x2, y2)):
					tmp = 0
					if x == 0:
						tmp = y
					elif x == r:
						tmp = r + c + (c - y)
					elif y == 0:
						tmp = 2 * r + 2 * c - x
					elif y == c:
						tmp = c + x
					ary.append([i, tmp])
	ary.sort(key=lambda x: x[1])
	stc = []
	for (i, x) in ary:
		if stc and stc[-1] == i:
			stc.pop()
		else:
			stc.append(i)
	if stc:
		return 'NO'
	else:
		return 'YES'

def __starting_point():
	(r, c, n) = map(int, input().split())
	xy = [list(map(int, input().split())) for _ in range(n)]
	print(main(r, c, n, xy))
__starting_point()


(R, C, N) = map(int, input().split())
consider = []

def dist(x, y):
	if x == 0:
		return R * 2 + C + (C - y)
	if x == R:
		return R + y
	if y == 0:
		return x
	if y == C:
		return R + C + (R - x)
for i in range(N):
	(x1, y1, x2, y2) = map(int, input().split())
	if ((x1 == 0 or x1 == R) or (y1 == 0 or y1 == C)) and ((x2 == 0 or x2 == R) or (y2 == 0 or y2 == C)):
		consider.append((i, dist(x1, y1)))
		consider.append((i, dist(x2, y2)))
l = []
consider = sorted(consider, key=lambda p: p[1])
for c in consider:
	if len(l) == 0:
		l.append(c[0])
		continue
	elif c[0] == l[-1]:
		l.pop()
	else:
		l.append(c[0])
if len(l) == 0:
	print('YES')
else:
	print('NO')


def f(r, c):
	if c == 0:
		return r
	elif c == C:
		return R + C + (R - r)
	elif r == 0:
		return R * 2 + C + (C - c)
	elif r == R:
		return R + c
	else:
		return -1
import sys
input = sys.stdin.readline
(R, C, N) = map(int, input().split())
Q = []
l = 1
for i in range(N):
	(r0, c0, r1, c1) = map(int, input().split())
	if f(r0, c0) != -1 and f(r1, c1) != -1:
		(s, t) = (f(r0, c0), f(r1, c1))
		if s > t:
			(s, t) = (t, s)
		Q.append((s, l))
		Q.append((t, -l))
		l += 1
Q.sort(key=lambda x: x[0])
flg = True
R = []
for (_, q) in Q:
	if q > 0:
		R.append(q)
	elif q < 0:
		if not R:
			flg = False
			break
		p = R.pop()
		if -p != q:
			flg = False
			break
print('YES' if flg else 'NO')


import os
import sys
from collections import deque, Counter
if os.getenv('LOCAL'):
	sys.stdin = open('_in.txt', 'r')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
IINF = 10 ** 18
MOD = 10 ** 9 + 7
(R, C, N) = list(map(int, sys.stdin.buffer.readline().split()))
XY = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
p1 = []
p2 = []
p3 = []
p4 = []
for (i, (x1, y1, x2, y2)) in enumerate(XY):
	for (x, y) in ((x1, y1), (x2, y2)):
		if y == 0:
			p1.append((x, i))
		elif x == R:
			p2.append((y, i))
		elif y == C:
			p3.append((x, i))
		elif x == 0:
			p4.append((y, i))
p1.sort()
p2.sort()
p3.sort(reverse=True)
p4.sort(reverse=True)
points = p1 + p2 + p3 + p4
counts = Counter([i for (_, i) in points])
que = deque()
for (_, i) in points:
	if counts[i] <= 1:
		continue
	if que and que[-1] == i:
		que.pop()
	else:
		que.append(i)
while len(que) >= 2 and que[0] == que[-1]:
	que.popleft()
	que.pop()
ok = len(que) == 0
if ok:
	print('YES')
else:
	print('NO')


def examC():
	ans = 0
	print(ans)
	return

def examD():
	ans = 0
	print(ans)
	return

def examE():
	(R, C, N) = LI()
	P = []
	P1 = []
	P2 = []
	P3 = []
	P4 = []
	index = 0
	for i in range(N):
		(x1, y1, x2, y2) = LI()
		if x1 not in (0, R) and y1 not in (0, C):
			continue
		if x2 not in (0, R) and y2 not in (0, C):
			continue
		if x1 == 0:
			P1.append((x1, y1, index))
		elif y1 == C:
			P2.append((x1, y1, index))
		elif x1 == R:
			P3.append((x1, y1, index))
		else:
			P4.append((x1, y1, index))
		if x2 == 0:
			P1.append((x2, y2, index))
		elif y2 == C:
			P2.append((x2, y2, index))
		elif x2 == R:
			P3.append((x2, y2, index))
		else:
			P4.append((x2, y2, index))
		index += 1
	P1.sort(key=lambda x: x[1])
	P2.sort()
	P3.sort(key=lambda x: x[1], reverse=True)
	P4.sort(reverse=True)
	for (_, _, p) in P1:
		P.append(p)
	for (_, _, p) in P2:
		P.append(p)
	for (_, _, p) in P3:
		P.append(p)
	for (_, _, p) in P4:
		P.append(p)
	que = deque()
	for index in P:
		if que:
			now = que.pop()
			if index == now:
				continue
			que.append(now)
			que.append(index)
		else:
			que.append(index)
	if que:
		print('NO')
	else:
		print('YES')
	return

def examF():
	ans = 0
	print(ans)
	return
from decimal import getcontext, Decimal as dec
import sys, bisect, itertools, heapq, math, random
from copy import deepcopy
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def I():
	return int(input())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def DI():
	return dec(input())

def LDI():
	return list(map(dec, sys.stdin.readline().split()))

def LSI():
	return list(map(str, sys.stdin.readline().split()))

def LS():
	return sys.stdin.readline().split()

def SI():
	return sys.stdin.readline().strip()
global mod, mod2, inf, alphabet, _ep
mod = 10 ** 9 + 7
mod2 = 998244353
inf = 10 ** 18
_ep = dec('0.000000000001')
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}
getcontext().prec = 28
sys.setrecursionlimit(10 ** 7)
examE()


import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
(r, c, n) = [int(i) for i in readline().split()]

def addedge(x1, y1):
	if y1 == 0:
		edge.append((x1, i))
	elif x1 == r:
		edge.append((r + y1, i))
	elif y1 == c:
		edge.append((r + c + (r - x1), i))
	else:
		edge.append((r + c + r + (c - y1), i))
	return
edge = []
for i in range(n):
	(x1, y1, x2, y2) = [int(i) for i in readline().split()]
	if x1 not in (0, r) and y1 not in (0, c) or (x2 not in (0, r) and y2 not in (0, c)):
		continue
	addedge(x1, y1)
	addedge(x2, y2)
edge.sort()
ans = []
for (d, i) in edge:
	if ans and ans[-1] == i:
		ans.pop()
	else:
		ans.append(i)
if ans:
	print('NO')
else:
	print('YES')