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: Wrong Answer

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

memo = {}
for i in range(n):
    is_point1_on_edge = (info[i][0] == 0 or info[i][0] == r) or (info[i][1] == 0 or info[i][1] == c) 
    is_point2_on_edge = (info[i][2] == 0 or info[i][2] == r) or (info[i][3] == 0 or info[i][3] == c) 
    if is_point1_on_edge and is_point2_on_edge:
        memo[1000000000*info[i][0] + info[i][1]] = i
        memo[1000000000*info[i][2] + info[i][3]] = i

li = []
#反時計回りで周を巡回
for i in range(r+1):
    if 1000000000*i in memo:
        li.append(memo[1000000000*i]+1)
for i in range(c+1):
    if 1000000000*r + i in memo:
        li.append(memo[1000000000*r + i]+1)
for i in range(r)[::-1]:
    if 1000000000*i + c in memo:
        li.append(memo[1000000000*i + c]+1)
for i in range(c)[::-1]:
    if i in memo:
        li.append(memo[i]+1)

#回文を作れるかどうか
if li[0] == li[-1]:
    pass
else:
    for i in range(len(li)-1):
        if li[i] == li[i+1]:
            li = li[i+1:] + li[0:i+1] 
            break
for i in range(len(li)):
    if li[i] == li[-1-i]:
        pass
    else:
        print("NO")
        exit()
print("YES")

Submission status: Wrong Answer

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 len(bound) == 0:
        return True

    bound.sort()
    i1 = i2 = -1
    l = -1
    for tpl in bound:
        _, _, i = tpl
        if i == l:
            i1 = l
            i2 = i
            break
        l = i

    if i1 < 0:
        return False

    bound = bound[i2:] + bound[: i2]
    for j in range(len(bound) // 2):
        _, _, i1 = bound[j]
        _, _, i2 = bound[-1 - j]
        if i1 != i2:
            return False

    return True


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: Wrong Answer

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 R+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: Wrong Answer

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):
    break
  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: Wrong Answer

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 (x1 in [0, C] and x2 in [0, C]) or (y1 in [0, R] and y2 in [0, R]):
        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: Wrong Answer

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 (x1 in [0, C] and x2 in [0, C]) or (y1 in [0, R] and y2 in [0, R]):
        if y1 == 0: UP.append([i+1, x1])
        elif x1 == 0: LEFT.append([i+1, y1])
        elif x1 == C: RIGHT.append([i+1, y1])
        elif y1 == R: DOWN.append([i+1, x1])

        if y2 == 0: UP.append([i+1, x2])
        elif x2 == 0: LEFT.append([i+1, y2])
        elif x2 == C: RIGHT.append([i+1, y2])
        elif y2 == R: DOWN.append([i+1, 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 not stack:
        stack.append(n)

    elif stack[-1] == n:
        stack.pop()

    else:
        stack.append(n)

print("NO" if stack else "YES")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
R, C, N = map(int, input().split())
Numbers = []
for i in range(N):
    x1, y1, x2, y2 = map(int, input().split())
    # 2つとも壁にくっついてるやつだけ
    if (x1 in [0, C] and x2 in [0, C]) or (y1 in [0, R] and y2 in [0, R]):
        Numbers.append([i+1, x1, y1])
        Numbers.append([i+1, x2, y2])

# 列でソート
Numbers.sort(key=lambda n: n[1])
D = defaultdict(set)
for i in range(1, len(Numbers), 2):
    D[Numbers[i][1]].add(Numbers[i][0])

for i in range(0, len(Numbers), 2):
    if Numbers[i][0] not in D[Numbers[i + 1][1]]:
        print("NO")
        exit()


# 行でソート
Numbers.sort(key=lambda n: n[2])
D = defaultdict(set)
for i in range(1, len(Numbers), 2):
    D[Numbers[i][2]].add(Numbers[i][0])

for i in range(0, len(Numbers), 2):
    if Numbers[i][0] not in D[Numbers[i + 1][2]]:
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
R, C, N = map(int, input().split())
Numbers = []
for i in range(N):
    x1, y1, x2, y2 = map(int, input().split())
    # 2つとも壁にくっついてるやつだけ
    if (x1 in [0, C] and x2 in [0, C]) or (y1 in [0, R] and y2 in [0, R]):
        Numbers.append([i+1, x1, y1])
        Numbers.append([i+1, x2, y2])

# 列でソート
Numbers.sort(key=lambda n: n[1])
D = defaultdict(set)
for i in range(1, len(Numbers), 2):
    D[Numbers[i][1]].add(Numbers[i][0])

for i in range(0, len(Numbers), 2):
    if Numbers[i][0] not in D[Numbers[i + 1][1]]:
        print("NO")
        exit()


# 行でソート
Numbers.sort(key=lambda n: n[2])
D = defaultdict(set)
for i in range(1, len(Numbers), 2):
    D[Numbers[i][2]].add(Numbers[i][0])

for i in range(0, len(Numbers), 2):
        print("NO")
        exit()

print("YES")

Submission status: Wrong Answer

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
for i in range(len(iru)):
  b = iru[i]
  if ato < b[1]:
    if b[0] < ato:
      print("NO")
      exit()
  if (ato < b[0] and b[1] < ato):
      print("NO")
      exit()
  ato = max(b[1],ato)
print("YES")

Submission status: Wrong Answer

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]
    print(b)
    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
for i in range(len(iru)):
  b = iru[i]
  for j in range(i+1,len(iru)):
    d = iru[j]
    if (b[0] <= d[0] and d[0] <= b[1] and b[1] <= d[1]):
        print("NO")
        exit()
    elif (d[0] <= b[0] and b[0] <= d[1] and d[1] <= b[1]):
        print("NO")
        exit()
print("YES")

Submission status: Wrong Answer

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)
if len(iru) <= 1:
  print("YES")
else:
  ato = 0
  for i in range(len(iru)):
    b = iru[i]
    if (b[0]<= ato and ato <= b[1]):
      print("NO")
      exit()
    if ato < b[1]:
      ato = b[1]
  print("YES")

Submission status: Wrong Answer

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*2 +r -b1[1]
    else:
      b1 = c*2 + r*2 -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 -2 - b2[0]
    if b1 > b2:
      tmp = b1 +0
      b1 = b2 +0
      b2 = tmp + 0
    iru.append([b1,b2])
iru = sorted(iru)
if len(iru) <= 1:
  print("YES")
else:
  ato = 0
  for i in range(len(iru)):
    b = iru[i]
    if (b[0]< ato and ato < b[1]):
      print("NO")
      exit()
    if ato < b[1]:
      ato = b[1]
  print("YES")

Submission status: Wrong Answer

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*2 +r-1 -b1[1]
    else:
      b1 = c*2 + r*2 -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-1 -b2[1]
    else:
      b2 = c*2 + r*2 -2 - b2[0]
    if b1 > b2:
      tmp = b1 +0
      b1 = b2 +0
      b2 = tmp + 0
    iru.append([b1,b2])
iru = sorted(iru)
if len(iru) <= 1:
  print("YES")
else:
  ato = 0
  for i in range(len(iru)):
    b = iru[i]
    if (b[0]< ato and ato < b[1]):
      print("NO")
      exit()
    if ato < b[1]:
      ato = b[1]
  print("YES")

Submission status: Wrong Answer

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 and a[i][2] == r:
    iru.append(a[i])
  elif a[i][0] == r and a[i][2] == 0:
    iru.append(a[i])
  elif a[i][1] == c and a[i][3] == 0:
    iru.append(a[i])
  elif a[i][1] == 0 and a[i][3] == c:
    iru.append(a[i])
if len(iru) <= 1:
  print("YES")
else:
  for i in range(len(iru)):
    b = iru[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*2 +r-1 -b1[1]
    else:
      b1 = c*2 + r*2 -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-1 -b2[1]
    else:
      b2 = c*2 + r*2 -2 - b2[0]
    if b1 > b2:
      tmp = b1 +0
      b1 = b2 +0
      b2 = tmp + 0
    for j in range(i+1,len(iru)):
      d = iru[j]
      d1 = [d[0],d[1]]
      d2 = [d[2],d[3]]
      if d2[0] == 0:
        d2 = d2[1]
      elif d2[1] == c:
        d2 = d2[0] + d2[1]
      elif d2[0] == r:
        d2 = c*2 +r-1 -d2[1]
      else:
        d2 = c*2 + r*2 -2 - d2[0]
      
      if d1[0] == 0:
        d1 = d1[1]
      elif d1[1] == c:
        d1 = d1[0] + d1[1]
      elif d1[0] == r:
        d1 =  c*2 +r-1 -d1[1]
      else:
        d1 = c*2 + r*2 -2 - d1[0]
      if d1 > d2:
        tmp = d1 +0
        d1 = d2 +0
        d2 = tmp + 0
      if (b1 <= d1 and d1 <= b2 and b2 <= d2):
        print("NO")
        exit()
      elif (d1 <= b1 and b1 <= d2 and d2 <= b2):
        print("NO")
        exit()
  print("YES")

Submission status: Wrong Answer

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

l = r = -1
for i,((p1,i1),(p2,i2)) in enumerate(zip(outer, outer[1:])):
    if i1 == i2:
        l,r = i,i+1
        break

if l < 0:
    print('NO')
    exit()

L = len(outer)
for i in range(L//2 - 1):
    l = (l-1)%L
    r = (r+1)%L
    if outer[l][1] != outer[r][1]:
        print('NO')
        exit()
print('YES')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
h, w, 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: Wrong Answer

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

print("ok")

Submission status: Wrong Answer

Solution code:
w, h, n = map(int, input().split())

vec = []
for i in range(n):
    x, y, x1, y1 = map(int, input().split())
    vec.append([x, y, i])
    vec.append([x1, y1, i])

sorted_node = []

vec.sort(key=lambda x: x[1])
for p in vec:
    if p[0] == 0:
        p[0] = -1
        p[1] = -1
        sorted_node.append(p)

vec.sort(key=lambda x: x[0])
for p in vec:
    if p[1] == h:
        p[0] = -1
        p[1] = -1
        sorted_node.append(p)

vec.sort(key=lambda x: x[1], reverse=True)
for p in vec:
    if p[0] == w:
        p[0] = -1
        p[1] = -1
        sorted_node.append(p)

vec.sort(key=lambda x: x[0], reverse=True)
for p in vec:
    if p[1] == 0:
        p[0] = -1
        p[1] = -1
        sorted_node.append(p)

print("ok")

Submission status: Wrong Answer

Solution code:
w, h, n = map(int, input().split())

vec = []
for i in range(n):
    x, y, x1, y1 = map(int, input().split())
    vec.append([x, y, i])
    vec.append([x1, y1, i])

sorted_node = []

vec.sort(key=lambda x: x[1])
for p in vec:
    if p[0] == 0:
        p[0] = -1
        p[1] = -1
        sorted_node.append(p)
print("ok")

Submission status: Wrong Answer

Solution code:
w, h, n = map(int, input().split())

vec = []
for i in range(n):
    x, y, x1, y1 = map(int, input().split())
    vec.append([x, y, i])
    vec.append([x1, y1, i])

sorted_node = []

vec.sort(key=lambda x: x[1])
# for p in vec:
#     if p[0] == 0:
#         p[0] = -1
#         p[1] = -1
#         sorted_node.append(p)
print("ok")

Submission status: Wrong Answer

Solution code:
w, h, n = map(int, input().split())

vec = []
for i in range(n):
    x, y, x1, y1 = map(int, input().split())
    vec.append((x, y, i))
    vec.append((x1, y1, i))

sorted_node = []
print("Ok")

Submission status: Wrong Answer

Solution code:
w, h, n = map(int, input().split())

vec = []
for i in range(n):
    x, y, x1, y1 = map(int, input().split())
    vec.append((x, y, i))
    vec.append((x1, y1, i))

sorted_node = []

vec.sort(key=lambda x: x[1])
for p in vec:
    if p[0] == 0:
        sorted_node.append(p)
        vec.remove(p)
print("a")

Submission status: Wrong Answer

Solution code:
R, C, N = map(int, raw_input().split())
 
bad = False
left = []
right = []
length = R*2 + C*2
for i in range(N):
	if bad:
		x = raw_input()
		continue
	else:
		x, y, xx, yy = map(int, raw_input().split())
	if (x in [0, R] or y in [0, C]) and (xx in [0, R] or yy in [0, C]):
		if x == 0 or y == C:
			first = x+y
		else:
			first = length-(x+y)
		if xx == 0 or yy == C:
			second = xx+yy
		else:
			second = length-(xx+yy)
		new_left, new_right = (second, first) if first < second else (first, second)
		left_count, right_count = 0, 0
		for i in range(len(left))[::]:
			if new_left < left[i]:
				break
			left_count += 1
		for i in range(len(left))[::]:
			if new_right < left[i]:
				break
			left_count -= 1
		for i in range(len(right))[::]:
			if right[i] < new_right:
				break
			right_count += 1
		for i in range(len(right))[::]:
			if right[i] < new_left:
				break
			right_count -= 1
 
		if left_count != right_count:
			bad = True
		else:
			left.insert(left_count, new_left)
			right.insert(right_count, new_right)
 
 
if bad:
	print 'NO'
else:
	print 'YES'

Submission status: Wrong Answer

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

bad = False
left = []
right = []
length = R*2 + C*2
for i in range(N):
	if bad:
		x = raw_input()
		continue
	else:
		x, y, xx, yy = map(int, raw_input().split())
	if (x in [0, R] or y in [0, C]) and (xx in [0, R] or yy in [0, C]):
		if x == 0 or y == C:
			first = x+y
		else:
			first = length-(x+y)
		if xx == 0 or yy == C:
			second = xx+yy
		else:
			second = length-(xx+yy)
		new_left, new_right = (second, first) if first < second else (first, second)
		left_count, right_count = 0, 0
		for i in range(len(left))[::]:
			if new_left < left[i]:
				break
			left_count += 1
		for i in range(len(right))[::]:
			if right[i] < new_right:
				break
			right_count += 1

		if left_count != right_count:
			bad = True
		else:
			left.insert(left_count, new_left)
			right.insert(right_count, new_right)


if bad:
	print 'NO'
else:
	print 'YES'

Submission status: Wrong Answer

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

bad = False
line = 0
for i in range(N):
	if bad:
		x = raw_input()
		continue
	else:
		x, y, xx, yy = map(int, raw_input().split())
	if (x in [0, R] or y in [0, C]) and (xx in [0, R] or yy in [0, C]):
		if x == 0 or y == C:
			first = x+y
		else:
			first = (R*2 + C*2)-(x+y)
		if xx == 0 or yy == C:
			second = xx+yy
		else:
			second = (R*2 + C*2)-(xx+yy)
		new_line = (1 << second) - (1 << first) if first < second else (1 << first) - (1 << second)
		if line | new_line in [line, new_line] or line & new_line == 0:
			line = line ^ new_line
		else:
			bad = True

if bad:
	print 'NO'
else:
	print 'YES'

Submission status: Wrong Answer

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

false_flag = False
lines = []
for i in range(N):
	if false_flag:
		x = raw_input()
		continue
	else:
		x, y, xx, yy = map(int, raw_input().split())
	if (x in [0, R] or y in [0, C]) and (xx in [0, R] or yy in [0, C]):
		if x <= y:
			first = x+y
		else:
			first = (R*2 + C*2)-(x+y)
		if xx <= yy:
			second = xx+yy
		else:
			second = (R*2 + C*2)-(xx+yy)
		new_line = [first, second] if first < second else [second, first]
		j = 0
		for j in range(len(lines)):
			new_left = new_line[0]
			last_left = lines[j][0]
			if new_left < last_left:
				new_right = new_line[1]
				last_right = lines[j][1]
				if last_left < new_right and new_right < last_right:
					false_flag = True
				else:
					lines.insert(j, new_line)
				break
		else:
			lines.append(new_line)

scopes = []
scopes.append(lines.pop(0))
while lines and not false_flag:
	while scopes:
		next_scope = lines.pop(0)
		right = scopes[-1][1]
		next_left = next_scope[0]
		next_right = next_scope[1]
		if next_left < right:
			if next_right < right:
				if lines:
					scopes.append(next_scope)
				else:
					break
			else:
				false_flag = True
				break
		else:
			scopes.pop()
	else:
		scopes.append(next_scope)

if false_flag:
	print 'NO'
else:
	print 'YES'

Submission status: Wrong Answer

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

lines = []
for i in range(N):
	x, y, xx, yy = map(int, raw_input().split())
	if (x in [0, R] or y in [0, C]) and (xx in [0, R] or yy in [0, C]):
		if x <= y:
			first = x+y
		else:
			first = (R*2 + C*2)-(x+y)
		if xx <= yy:
			second = xx+yy
		else:
			second = (R*2 + C*2)-(xx+yy)
		new_line = [first, second] if first < second else [second, first]
		j = 0
		for j in range(len(lines)):
			if new_line[0] < lines[j][0]:
				lines.insert(j, new_line)
				break
		else:
			lines.append(new_line)

false_flag = False
scopes = []
scopes.append(lines.pop(0))
while lines and not false_flag:
	while scopes:
		next_scope = lines.pop(0)
		right = scopes[-1][1]
		next_left = next_scope[0]
		next_right = next_scope[1]
		if next_left < right:
			if next_right < right:
				if lines:
					scopes.append(next_scope)
				else:
					break
			else:
				false_flag = True
				break
		else:
			scopes.pop()
	else:
		scopes.append(next_scope)

if false_flag:
	print 'NO'
else:
	print 'YES'

Submission status: Wrong Answer

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

lines = []
for i in range(N):
	x, y, xx, yy = map(int, raw_input().split())
	if (x in [0, R] or y in [0, C]) and (xx in [0, R] or yy in [0, C]):
		if x <= y:
			first = x+y
		else:
			first = (R*2 + C*2)-(x+y)
		if xx <= yy:
			second = xx+yy
		else:
			second = (R*2 + C*2)-(xx+yy)
		new_line = [first, second] if first < second else [second, first]
		j = 0
		for j in range(len(lines)):
			if new_line[0] < lines[j][0]:
				lines.insert(j, new_line)
				break
		else:
			lines.append(new_line)

end_flag = False
pop_count = 0
while lines and not end_flag:
	scopes = []
	scopes.append(lines.pop(0))
	for i in range(len(lines)):
		next_left = lines[i-pop_count][0]
		next_right = lines[i-pop_count][1]
		while scopes:
			right = scopes[-1][1]
			if next_left < right:
				if next_right < right:
					scopes.append(lines.pop(0))
					pop_count += 1
					break
				else:
					end_flag = True
					break
			else:
				scopes.pop()
		if end_flag:
			break


if lines:
	print 'NO'
else:
	print 'YES'

Submission status: Wrong Answer

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 len(s) > 2:
        return 'NO'
    return 'YES'

print(main())

Submission status: Wrong Answer

Solution code:
# coding: utf-8
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


# 周上を (0, 0) を原点として反時計回りに 1 本の数直線とした時の point の座標を返す．
# 周上にあるかの判定も行う．
def change_edge_point(R, C, point):
    x, y = point
    if 0 < x < C or 0 < y < R:
        return False
    if y == 0:
        return x
    elif y == C:
        return C + R + C - x
    if x == 0:
        return C + y
    elif x == R:
        return C + R + C + R - y


def solve(R, C, N, num_point):
    point_double = []
    for point in num_point:
        p_1, p_2 = point
        ret_1 = change_edge_point(R, C, p_1)
        ret_2 = change_edge_point(R, C, p_2)
        if ret_1 is False or ret_2 is False:
            continue
        point_double.append((min(ret_1, ret_2), max(ret_1, ret_2)))

    for p_1, p_2 in itertools.combinations(point_double, 2):
        if p_1[0] == p_2[0] or p_1[1] == p_2[1]:
            return "NO"

        if p_1[0] < p_2[0]:
            if p_1[1] > p_2[1]:
                continue
            else:
                return "NO"
        elif p_1[0] > p_2[0]:
            if p_1[1] < p_2[1]:
                continue
            else:
                return "NO"

    return "YES"


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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
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


# 周上を (0, 0) を原点として反時計回りに 1 本の数直線とした時の point の座標を返す．
# 周上にあるかの判定も行う．
def change_edge_point(R, C, point):
    x, y = point
    if 0 < x < C or 0 < y < R:
        return False
    if y == 0:
        return x
    elif y == R:
        return C + R + C - x
    if x == 0:
        return C + y
    elif x == C:
        return C + R + C + R - y


def solve(R, C, N, num_point):
    point_double = []
    for point in num_point:
        p_1, p_2 = point
        ret_1 = change_edge_point(R, C, p_1)
        ret_2 = change_edge_point(R, C, p_2)
        if ret_1 is False or ret_2 is False:
            continue
        point_double.append((min(ret_1, ret_2), max(ret_1, ret_2)))

    for p_1, p_2 in itertools.combinations(point_double, 2):
        if p_1[0] == p_2[0] or p_1[1] == p_2[1]:
            return "NO"

        if p_1[0] < p_2[0]:
            if p_1[1] > p_2[1]:
                continue
            else:
                return "NO"
        elif p_1[0] > p_2[0]:
            if p_1[1] < p_2[1]:
                continue
            else:
                return "NO"

    return "YES"


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


if __name__ == "__main__":
    main()