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

Solution (Python):

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

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

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

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




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)

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

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

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

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

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):
R,C,n=map(int,raw_input().split())
pair=[map(int,raw_input().split()) for _ in xrange(n)]
u=[]
r=[]
d=[]
l=[]
for i in xrange(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 xrange(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"

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

Solution (Python):
R, C, n = map(int, raw_input().split())
x1 = []
y1 = []
x2 = []
y2 = []
for i in xrange(n): 
  xx1, yy1, xx2, yy2 = map(int, raw_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 xrange(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 = map(lambda x : x[0], lis)

cnt = {}
for i in lis:
  if not i in cnt:
    cnt[i] = 0
  cnt[i] += 1

lis = filter(lambda x : cnt[x] == 2, lis)

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

Solution (Python):


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 = map(int, input().split())
    edge_points = []            
    for i in range(N):
        x1, y1, x2, y2 = 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())

Solution (Python):
R, C, N = 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 = 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")

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

Solution (Python):
def calc_pos(x, y, R, C):
    if y == 0:
        return x
    if y == C:
        return 2 * R + C - x
    if x == R:
        return R + y
    if x == 0:
        return 2 * R + 2 * C - y

def read_data():
    R, C, N = map(int, input().split())
    pairs = []
    xys = []
    idx = 0
    for i in range(N):
        x1, y1, x2, y2 = map(int, input().split())
        xys.append((x1, y1))
        xys.append((x2, y2))
        if (x1 != 0 and x1 != R and y1 != 0 and y1 != C) or (x2 != 0 and x2 != R and y2 != 0 and y2 != C):
            continue
        a = calc_pos(x1, y1, R, C)
        b = calc_pos(x2, y2, R, C)
        pairs.append((a, idx))
        pairs.append((b, idx))
        idx += 1
    pairs.sort()
    return pairs, xys

def is_valid(xys):
    xys.sort()
    prev_x, prev_y = xys[0]
    for x, y in xys[1:]:
        if prev_x == x and prev_y == y:
            return False
        prev_x = x
        prev_y = y
    return True

def solve(pairs, xys):
    if len(xys) == 2:
        return "YES"
    if not is_valid(xys):
        return "NO"    
    idxs = [i for a, i in pairs]
    stack = []
    for idx in idxs:
        if stack and stack[-1] == idx:
            stack.pop()
            continue
        stack.append(idx)
    if stack:
        return "NO"
    else:
        return "YES"

pairs, xys = read_data()
print(solve(pairs, xys))