Problem p02680 (python)

Fixed submission:
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    dir = [(-1,0),(1,0),(0,-1),(0,1)]
    n,m = map(int,ipt().split())
    xb = []
    yb = []
    xs = []
    ys = []
    for i in range(n):
        a,b,c = map(int,ipt().split())
        xb.append((a,b,c))
        xs.append(a)
        xs.append(b)
        ys.append(c)
    for j in range(m):
        d,e,f = map(int,ipt().split())
        yb.append((d,e,f))
        xs.append(d)
        ys.append(e)
        ys.append(f)
    xs = sorted(list(set(xs)))
    ys = sorted(list(set(ys)))
    lx = len(xs)
    ly = len(ys)

    dix = dict()
    diy = dict()

    for i,xi in enumerate(xs):
        dix[xi] = i
    for i,yi in enumerate(ys):
        diy[yi] = i

    brd = [[1]*(ly*2-1) for _ in range(lx*2-1)]
    for a,b,c in xb:
        xa = dix[a]
        xb = dix[b]
        xc = diy[c]
        for i in range(xa,xb):
            brd[2*i+1][xc*2] = 0
    for d,e,f in yb:
        xd = dix[d]
        xe = diy[e]
        xf = diy[f]
        for i in range(xe,xf):
            brd[xd*2][2*i+1] = 0
    for i,xi in enumerate(xs[1::]):
        for j,yj in enumerate(ys[1::]):
            brd[2*i+1][2*j+1] = (yj-ys[j])*(xi-xs[i])

    sx = bisect.bisect_left(xs,0)
    sy = bisect.bisect_left(ys,0)
    if sx == 0 or sy == 0 or sx == lx or sy == ly:
        print("INF")
        exit()
    q = []
    q.append((sx*2-1,sy*2-1))
    ans = brd[sx*2-1][sy*2-1]
    brd[sx*2-1][sy*2-1] = 0
    while q:
        qx,qy = q.pop()
        for dx,dy in dir:
            nx = qx+dx
            ny = qy+dy
            if brd[nx][ny]:
                if nx == 0 or nx == lx*2-2 or ny == 0 or ny == ly*2-2:
                    print("INF")
                    exit()
                nx += dx
                ny += dy
                if brd[nx][ny]:
                    ans += brd[nx][ny]
                    brd[nx][ny] = 0
                    q.append((nx,ny))
        #print(qx,qy,q)
    print(ans)


    return None

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    dir = [(-1,0),(1,0),(0,-1),(0,1)]
    n,m = map(int,ipt().split())
    xb = []
    yb = []
    xs = []
    ys = []
    for i in range(n):
        a,b,c = map(int,ipt().split())
        xb.append((a,b,c))
        xs.append(a)
        xs.append(b)
        ys.append(c)
    for j in range(m):
        d,e,f = map(int,ipt().split())
        yb.append((d,e,f))
        xs.append(d)
        ys.append(e)
        ys.append(f)
    xs = sorted(list(set(xs)))
    ys = sorted(list(set(ys)))
    lx = len(xs)
    ly = len(ys)

    dix = dict()
    diy = dict()

    for i,xi in enumerate(xs):
        dix[xi] = i
    for i,yi in enumerate(ys):
        diy[yi] = i

    brd = [[1]*(ly*2-1) for _ in range(lx*2-1)]
    for a,b,c in xb:
        xa = dix[a]
        xb = dix[b]
        xc = diy[c]
        for i in range(xa,xb):
            brd[2*i+1][xc*2] = 0
    for d,e,f in yb:
        xd = dix[d]
        xe = diy[e]
        xf = diy[f]
        for i in range(xe,xf):
            brd[xd*2][2*i+1] = 0
    for i,xi in enumerate(xs[1::]):
        for j,yj in enumerate(ys[1::]):
            brd[2*i+1][2*j+1] = (yj-ys[j])*(xi-xs[i])

    sx = bisect.bisect_left(xs,0)
    sy = bisect.bisect_left(ys,0)
    if sx == 0 or sy == 0:
        print("INF")
        exit()
    q = []
    q.append((sx*2-1,sy*2-1))
    ans = brd[sx*2-1][sy*2-1]
    brd[sx*2-1][sy*2-1] = 0
    while q:
        qx,qy = q.pop()
        for dx,dy in dir:
            nx = qx+dx
            ny = qy+dy
            if brd[nx][ny]:
                if nx == 0 or nx == lx*2-2 or ny == 0 or ny == ly*2-2:
                    print("INF")
                    exit()
                nx += dx
                ny += dy
                if brd[nx][ny]:
                    ans += brd[nx][ny]
                    brd[nx][ny] = 0
                    q.append((nx,ny))
        #print(qx,qy,q)
    print(ans)


    return None

if __name__ == '__main__':
    main()