Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
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():
    import operator
    class SegmentTree:
        def __init__(self, size, default, op = operator.add):
            self.size = 2**size.bit_length()
            self.dat = [default]*(self.size*2)
            self.op = op

        def update(self, i, x):
            i += self.size
            self.dat[i] = x
            while i > 0:
                i >>= 1
                self.dat[i] = self.op(self.dat[i*2], self.dat[i*2+1])

        def add(self, i, x):
            i += self.size
            self.dat[i] = self.op(self.dat[i], x)
            while i > 0:
                i >>= 1
                self.dat[i] = self.op(self.dat[i], x)

        def get(self, a, b = None):
            if b is None:
                b = a + 1
            l, r = a + self.size, b + self.size
            res = None
            while l < r:
                if l & 1:
                    if res is None:
                        res = self.dat[l]
                    else:
                        res = self.op(res, self.dat[l])
                    l += 1

                if r & 1:
                    r -= 1
                    if res is None:
                        res = self.dat[r]
                    else:
                        res = self.op(res, self.dat[r])
                l >>= 1
                r >>= 1
            return res

    def check(x,y):
        return (x == 0)|(x == w)|(y == 0)|(y == h)

    w,h,n = LI()
    p = []
    lx = set()
    ly = set()
    for i in range(n):
        x,y,s,t = LI()
        if check(x,y)&check(s,t):
            p.append((x,y,s,t))
            lx |= {x,s}
            ly |= {y,t}
    if len(p) < 2:
        print("YES")
        return
    lx = list(lx)
    ly = list(ly)
    lx.sort()
    ly.sort()
    px = {lx[i]: i for i in range(len(lx))}
    py = {ly[i]: i for i in range(len(ly))}
    w,h = max(px.values()),max(py.values())
    fy0 = SegmentTree(w+1, 0)
    fyh = SegmentTree(w+1, 0)
    fx0 = SegmentTree(h+1, 0)
    fxw = SegmentTree(h+1, 0)
    for i in range(len(p)):
        b = 1<<i
        x,y,s,t = p[i]
        x,y,s,t = px[x],py[y],px[s],py[t]
        if x == 0:
            f1 = fx0.get(0,y+1)
        elif x == w:
            f1 = fxw.get(0,y+1)
        elif y == 0:
            f1 = fy0.get(0,x+1)
        else:
            f1 = fyh.get(0,x+1)
        if s == 0:
            f2 = fx0.get(0,t+1)
        elif s == w:
            f2 = fxw.get(0,t+1)
        elif t == 0:
            f2 = fy0.get(0,s+1)
        else:
            f2 = fyh.get(0,s+1)
        if f1^f2:
            print("NO")
            return
        if x == 0:
            if s == 0:
                y,t = min(y,t),max(y,t)
                fx0.add(y,b)
                fx0.add(t+1,-b)
            elif s == w:
                fx0.add(0,b)
                fx0.add(y+1,-b)
                fy0.add(0,b)
                fxw.add(0,b)
                fxw.add(t+1,-b)
            elif t == 0:
                fx0.add(0,b)
                fx0.add(y+1,-b)
                fy0.add(0,b)
                fy0.add(s+1,-b)
            else:
                fx0.add(y,b)
                fyh.add(0,b)
                fyh.add(s+1,-b)
        elif x == w:
            if s == 0:
                fx0.add(0,b)
                fx0.add(t+1,-b)
                fy0.add(0,b)
                fxw.add(0,b)
                fxw.add(y+1,-b)
            elif s == w:
                y,t = min(y,t),max(y,t)
                fxw.add(y,b)
                fxw.add(t+1,-b)
            elif t == 0:
                fy0.add(s,b)
                fxw.add(0,b)
                fxw.add(y+1,-b)
            else:
                fxw.add(y,b)
                fyh.add(s,b)
        elif y == 0:
            if s == 0:
                fy0.add(0,b)
                fy0.add(x+1,-b)
                fx0.add(0,b)
                fx0.add(t+1,-b)
            elif s == w:
                fy0.add(x,b)
                fxw.add(0,b)
                fxw.add(t+1,-b)
            elif t == 0:
                x,s = min(x,s),max(x,s)
                fy0.add(x,b)
                fy0.add(s,-b)
            else:
                fy0.add(x,b)
                fxw.add(0,b)
                fyh.add(s,b)
        else:
            if s == 0:
                fx0.add(t,b)
                fyh.add(0,b)
                fyh.add(x+1,-b)
            elif s == w:
                fxw.add(t,b)
                fyh.add(x,b)
            elif t == 0:
                fyh.add(x,b)
                fxw.add(0,b)
                fy0.add(s,b)
            else:
                x,s = min(x,s),max(x,s)
                fyh.add(x,b)
                fyh.add(s,-b)
    print("YES")
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def check(x,y):
        return (x == 0)|(x == w)|(y == 0)|(y == h)

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

#Solve
if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
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():
    import operator
    class SegmentTree:
        def __init__(self, size, default, op = operator.add):
            self.size = 2**size.bit_length()
            self.dat = [default]*(self.size*2)
            self.op = op

        def update(self, i, x):
            i += self.size
            self.dat[i] = x
            while i > 0:
                i >>= 1
                self.dat[i] = self.op(self.dat[i*2], self.dat[i*2+1])

        def add(self, i, x):
            i += self.size
            self.dat[i] = self.op(self.dat[i], x)
            while i > 0:
                i >>= 1
                self.dat[i] = self.op(self.dat[i], x)

        def get(self, a, b = None):
            if b is None:
                b = a + 1
            l, r = a + self.size, b + self.size
            res = None
            while l < r:
                if l & 1:
                    if res is None:
                        res = self.dat[l]
                    else:
                        res = self.op(res, self.dat[l])
                    l += 1

                if r & 1:
                    r -= 1
                    if res is None:
                        res = self.dat[r]
                    else:
                        res = self.op(res, self.dat[r])
                l >>= 1
                r >>= 1
            return res

    def check(x,y):
        return (x == 0)|(x == w)|(y == 0)|(y == h)

    w,h,n = LI()
    p = []
    lx = set()
    ly = set()
    for i in range(n):
        x,y,s,t = LI()
        if check(x,y)&check(s,t):
            p.append((x,y,s,t))
            lx |= {x,s}
            ly |= {y,t}
    if len(p) < 2:
        print("YES")
        return
    lx = list(lx)
    ly = list(ly)
    lx.sort()
    ly.sort()
    px = {lx[i]: i for i in range(len(lx))}
    py = {ly[i]: i for i in range(len(ly))}
    w,h = max(px.values()),max(py.values())
    fy0 = SegmentTree(w+1, 0)
    fyh = SegmentTree(w+1, 0)
    fx0 = SegmentTree(h+1, 0)
    fxw = SegmentTree(h+1, 0)
    for i in range(len(p)):
        b = 1<<i
        x,y,s,t = p[i]
        x,y,s,t = px[x],py[y],px[s],py[t]
        if x == 0:
            f1 = fx0.get(0,y+1)
        elif x == w:
            f1 = fxw.get(0,y+1)
        elif y == 0:
            f1 = fy0.get(0,x+1)
        else:
            f1 = fyh.get(0,x+1)
        if s == 0:
            f2 = fx0.get(0,t+1)
        elif s == w:
            f2 = fxw.get(0,t+1)
        elif t == 0:
            f2 = fy0.get(0,s+1)
        else:
            f2 = fyh.get(0,s+1)
        if f1^f2:
            print("NO")
            return
        if x == 0:
            if s == 0:
                y,t = min(y,t),max(y,t)
                fx0.add(y,b)
                fx0.add(t+1,-b)
            elif s == w:
                fx0.add(0,b)
                fx0.add(y+1,-b)
                fy0.add(0,b)
                fxw.add(0,b)
                fxw.add(t+1,-b)
            elif t == 0:
                fx0.add(0,b)
                fx0.add(y+1,-b)
                fy0.add(0,b)
                fy0.add(s+1,-b)
            else:
                fx0.add(y,b)
                fyh.add(0,b)
                fyh.add(s+1,-b)
        elif x == w:
            if s == 0:
                fx0.add(0,b)
                fx0.add(t+1,-b)
                fy0.add(0,b)
                fxw.add(0,b)
                fxw.add(y+1,-b)
            elif s == w:
                y,t = min(y,t),max(y,t)
                fxw.add(y,b)
                fxw.add(t+1,-b)
            elif t == 0:
                fy0.add(s,b)
                fxw.add(0,b)
                fxw.add(y+1,-b)
            else:
                fxw.add(y,b)
                fyh.add(s,b)
        elif y == 0:
            if s == 0:
                fy0.add(0,b)
                fy0.add(x+1,-b)
                fx0.add(0,b)
                fx0.add(t+1,-b)
            elif s == w:
                fy0.add(x,b)
                fxw.add(0,b)
                fxw.add(t+1,-b)
            elif t == 0:
                x,s = min(x,s),max(x,s)
                fy0.add(x,b)
                fy0.add(s,-b)
            else:
                fy0.add(x,b)
                fxw.add(0,b)
                fyh.add(s,b)
        else:
            if s == 0:
                fx0.add(t,b)
                fyh.add(0,b)
                fyh.add(x+1,-b)
            elif s == w:
                fxw.add(t,b)
                fyh.add(x,b)
            elif t == 0:
                fyh.add(x,b)
                fxw.add(0,b)
                fy0.add(s,b)
            else:
                x,s = min(x,s),max(x,s)
                fyh.add(x,b)
                fyh.add(s,-b)
    print("YES")
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def check(x,y):
        return (x == 0)|(x == w)|(y == 0)|(y == h)

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

#Solve
if __name__ == "__main__":
    solve()