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



Sample input:
4 2 3
0 1 3 1
1 1 4 1
2 0 2 2


Sample output:
YES

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))
            # 2つ目
            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)
    # print(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()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意


r,c,n = [int(i) for i in readline().split()]
#xyxy = [[int(i) for i in readline().split()] for _ in range(n)] 
#for i,(x1,y1,x2,y2) in enumerate(xyxy):

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()
#print(edge)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))
#board=[[circ(x,y) for y in range(C+1)] for x in range(R+1)]
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")

Submission status: Accepted

Solution code:
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])
#print(H,table)
while len(H)!=0 and H[0]==H[-1]:
    H.popleft()
    H.pop()
if len(H)==0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

#print(XY)
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()
#print(D, D2)
if D == D2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
R, C, N = map(int, input().split())
UP, RIGHT, DOWN, LEFT = [], [], [], []
for i in range(N):
    x1, y1, x2, y2 = map(int, input().split())

    # 2つとも周上にない
    if 0 < x1 < R and 0 < y1 < C:
        continue
    if 0 < x2 < R and 0 < y2 < C:
        continue

    # 2つとも周回上にある
    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")

Submission status: Accepted

Solution code:
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))

#print(dots)
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")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def main():
    # res = []
    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)
    # print dr
    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"

    # print '\n'.join(res)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
w, h, n = 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 = 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 ])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

edges = []

def convert(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
  else:
    return -1


for i in range(N):
  a,b,c,d = map(int, input().split())
  
  p,q = convert(a,b),convert(c,d)
  if p >= 0 and q >= 0:
    edges.append((p,q) if p < q else (q,p))

edges.sort()

# print(edges)


stack = [R+R+C+C]
flag = 'YES'
for a,b in edges:
  while a >= stack[-1]:
    stack.pop()

  if b > stack[-1]:
    flag = 'NO'
    break
  else:
    stack.append(b)

print(flag)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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