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 (Python):
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
    else:
        if Q.pop()!=p[1]:
            flag2=True
            break

if flag2:
    print("NO")
else:
    print("YES")

Solution (Python):

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 = map(int,input().split())

u = []
l = []
d = []
r = []

for i in range(N):

    X1,Y1,X2,Y2 = 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")

Solution (Python):
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")
                exit()
    print("YES")

main()

Solution (Python):

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


if __name__ == "__main__":
    solve()

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline


class BIT():
    """区間加算、一点取得クエリをそれぞれO(logN)で答える
    add: 区間[l, r)にvalを加える
    get_val: i番目の値を求める
    i, l, rは0-indexed
    """
    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 = i + 1
        s = 0
        while i <= self.n:
            s += self.bit[i]
            i += i & -i
        return s

    def add(self, l, r, val):
        """区間[l, r)にvalを加える"""
        self._add(r, val)
        self._add(l, -val)


r, c, n = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]

def solve(x1, y1):
    if y1 == 0:
        return x1
    if x1 == r:
        return r + y1
    if y1 == c:
        return r + c + (r-x1)
    if x1 == 0:
        return r + c + r + (c-y1)

li = []
for i in range(n):
    x1, y1, x2, y2 = info[i]
    if (x1 % r == 0 or y1 % c == 0) and (x2 % r == 0 or y2 % c == 0):
        pos1, pos2 = solve(x1, y1), solve(x2, y2)
        li.append((pos1, i))
        li.append((pos2, i))

li = sorted(li)
q = deque([])
for i in range(len(li)):
    _, j = li[i]
    if q and q[-1] == j:
        q.pop()
    else:
        q.append(j)
if q:
    print("NO")
else:
    print("YES")

Solution (Python):
import random
import sys
input = sys.stdin.readline


class BIT():
    """区間加算、一点取得クエリをそれぞれO(logN)で答える
    add: 区間[l, r)にvalを加える
    get_val: i番目の値を求める
    i, l, rは0-indexed
    """
    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 = i + 1
        s = 0
        while i <= self.n:
            s += self.bit[i]
            i += i & -i
        return s

    def add(self, l, r, val):
        """区間[l, r)にvalを加える"""
        self._add(r, val)
        self._add(l, -val)


r, c, n = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]

def solve(x1, y1):
    if y1 == 0:
        return x1
    if x1 == r:
        return r + y1
    if y1 == c:
        return r + c + (r-x1)
    if x1 == 0:
        return r + c + r + (c-y1)

def compress(array):
    set_ = set([])
    for i in range(len(array)):
        a, b = array[i]
        set_.add(a)
        set_.add(b)
    memo = {value : index for index, value in enumerate(sorted(list(set_)))}
    max_ = 0
    for i in range(len(array)):
        array[i][0] = memo[array[i][0]]
        array[i][1] = memo[array[i][1]]
        max_ = max(max_, *array[i])
    return max_, array

query = []
for i in range(n):
    x1, y1, x2, y2 = info[i]
    if (x1 % r == 0 or y1 % c == 0) and (x2 % r == 0 or y2 % c == 0):
        pos1, pos2 = solve(x1, y1), solve(x2, y2)
        if pos1 > pos2:
            pos1, pos2 = pos2, pos1
        query.append([pos1, pos2 + 1])

m, query = compress(query)
bit = BIT(m)  
for i in range(len(query)):
    begin, end = query[i]
    if bit.get_val(begin) != bit.get_val(end - 1):
        print("NO")
        exit()
    tmp = random.randrange(0, 10**18)
    bit.add(begin, end, tmp)
print("YES")

Solution (Python):
import sys
input = sys.stdin.buffer.readline
R, C, N = map(int, input().split())


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


def check(x, y):
    if x == 0:
        return y
    elif x == R:
        return R + C + C - y
    elif y == 0:
        return C + R + C + R - x
    elif y == C:
        return C + x
    else:
        return -1


lr_list_raw = []
z_list = []
for i in range(N):
    x1, y1, x2, y2 = map(int, input().split())
    z1, z2 = check(x1, y1), check(x2, y2)
    if z1 >= 0 and z2 >= 0:
        lr_list_raw.append((min(z1, z2), max(z1, z2)))
        z_list.append(z1)
        z_list.append(z2)

z_list.sort()
idx = {zi: i+1 for i, zi in enumerate(z_list)}
lr_list = []
for l_raw, r_raw in lr_list_raw:
    lr_list.append((idx[l_raw], idx[r_raw]))

'''
print(z_list)
print(idx)
print(lr_list_raw)
'''

bit = Bit(max(idx.values()) + 3)
for l, r in lr_list:
    if (bit.sum(r) - bit.sum(l)) % 2 == 1:
        print('NO')
        exit()
    bit.add(l, 1)
    bit.add(r, 1)

print('YES')

Solution (Python):
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==[]])

Solution (Python):
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()

Solution (Python):
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")
            



if __name__ == "__main__":
    main()

Solution (Python):



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

Solution (Python):
import sys
input = sys.stdin.readline
import math

"""
内部の点が絡むものは、曲線で結んでホモトピーで変形すれば、無視できる
境界の2点を結ぶものたちだけが問題
"""

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)

Solution (Python):
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")

Solution (Python):



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


if __name__ == '__main__':
    main()

Solution (Python):
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}
        else:
            if s[-1] != n: break
            else: s.pop()
    else:
        for point, n in r:
            if n not in used:
                s.append(n)
                used |= {n}
            else:
                if s[-1] != n: break
                else: s.pop()
        else:
            for point, n in d:
                if n not in used:
                    s.append(n)
                    used |= {n}
                else:
                    if s[-1] != n: break
                    else: s.pop()
            else:
                for point, n in l:
                    if n not in used:
                        s.append(n)
                        used |= {n}
                    else:
                        if s[-1] != n: break
                        else: s.pop()
                else: crossed = False

    print("NO" if crossed else "YES")
                            
    return 0
  
if __name__ == "__main__":
    solve()

Solution (Python):
R,C,N=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)
    else:
        if X[-1]==i:
            X.pop()
        else:
            X.append(i)
if len(X)==0:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
R,C,N=map(int,input().split())
table=[]
def f(i,j):
    if i==0:
        return j
    if j==C:
        return C+i
    if i==R:
        return R+C+C-j
    if j==0:
        return R+R+C+C-i
for i in range(N):
    a,b,c,d=map(int,input().split())
    if (0<a<R and 0<b<C) or (0<c<R and 0<d<C):
        continue
    table.append((f(a,b),i))
    table.append((f(c, d), i))
table.sort()
H=deque()
for i in range(len(table)):
    if len(H)!=0 and H[-1]==table[i][1]:
        H.pop()
    else:
        H.append(table[i][1])

while len(H)!=0 and H[0]==H[-1]:
    H.popleft()
    H.pop()
if len(H)==0:
    print('YES')
else:
    print('NO')

Solution (Python):
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")

Solution (Python):
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')

Solution (Python):
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")

Solution (Python):
R, C, N = map(int, input().split())
UP, RIGHT, DOWN, LEFT = [], [], [], []
for i in range(N):
    x1, y1, x2, y2 = 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")

Solution (Python):
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])
		else:
			if stack[-1] == dots[i][1]:
				stack.pop()
			else:
				stack.append(dots[i][1])
	if stack:
		print("NO")
	else:
		print("YES")

Solution (Python):
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])
		else:
			if stack[-1] == dots[i][1]:
				stack.pop()
			else:
				stack.append(dots[i][1])
	if stack:
		print("NO")
	else:
		print("YES")

Solution (Python):
from sys import exit, 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")

Solution (Python):
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)

Solution (Python):
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")

Solution (Python):
import sys

def main():
    
    inps = map(lambda s: s.strip(), sys.stdin.readlines())
    iid = 0
    r, c, n = map(int, inps[iid].split())
    iid += 1

    def boundary(x,y):
        if x==0 or y==0 or x==r or y==c:
            return True
        return False

    def downright(x, y):
        if x==0 or y==c:
            return True
        return False


    points = []
    dr = []
    ul = []
    for i in xrange(1,n+1):
        x1, y1, x2, y2 = map(int, inps[iid].split())
        iid += 1
        if boundary(x1, y1) and boundary(x2, y2):
            if downright(x1,y1):
                dr.append((x1,y1,i))
            else:
                ul.append((x1,y1,i))
            if downright(x2,y2):
                dr.append((x2,y2,i))
            else:
                ul.append((x2,y2,i))
    dr.sort()
    ul.sort(reverse=True)
    dr.extend(ul)
    
    stack = []
    for elem in dr:
        if len(stack)!=0 and stack[-1]==elem[2]:
            stack.pop()
        else:
            stack.append(elem[2])
    if len(stack)==0:
        print "YES"
    else:
        print "NO"

    
if __name__ == '__main__':
    main()

Solution (Python):
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')