Problem p02680 (python)

Fixed submission:
N,M = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*N ]
DEF = [list(map(int,input().split())) for _ in [0]*M ]

X = [0]*3003
Y = [0]*3003
INF = 10**9 + 10
X[-1] = -INF
Y[-1] = -INF
X[-2] = INF
Y[-2] = INF
i = 0
j = 0
for a,b,c in ABC:
    X[i] = a
    X[i+1] = b
    Y[j] = c
    i += 2
    j += 1
for d,e,f in DEF:
    X[i] = d
    Y[j] = e
    Y[j+1] = f
    i += 1
    j += 2
X = sorted(set(X))
Y = sorted(set(Y))
Xdict = {x:i for i,x in enumerate(X)}
Ydict = {y:i for i,y in enumerate(Y)}

nx = len(X)
ny = len(Y)
nxy = nx * ny

E = [[15]*nx for _ in [0]*ny]

E[0] = [7]*nx
E[-1] = [13]*nx
for i in range(ny):
    E[i][0] -= 4
    E[i][-1] -= 1

for a,b,c in ABC:
    y1 = Ydict[c]
    y0 = y1-1
    for x in range(Xdict[a],Xdict[b]):
        E[y0][x] &= 15-2
        E[y1][x] &= 15-8

for d,e,f in DEF:
    x1 = Xdict[d]
    x0 = x1-1
    for y in range(Ydict[e],Ydict[f]):
        E[y][x0] &= 15-1
        E[y][x1] &= 15-4

dX = [x2-x for x,x2 in zip(X,X[1:])]
dY = [y2-y for y,y2 in zip(Y,Y[1:])]
burst = False
x0 = Xdict[0]
y0 = Ydict[0]
ans = dX[x0]*dY[y0]
done = [[False]*nx for _ in [0]*ny]
done[y0][x0] = True
q = [(x0,y0)]
dxy = [(1,0),(0,1),(-1,0),(0,-1)]
while q:
    qq = []
    for x,y in q:
        e = E[y][x]
        for i in range(4):
            if (e>>i)&1 == 0 : continue
            dx,dy = dxy[i]
            jx = x+dx
            jy = y+dy
            if done[jy][jx]:continue
            done[jy][jx] = True

            if jx==0 or jx==nx-1 or jy==0 or jy==ny-1:
                burst = True
                ans = "INF"
                qq = []
                break
            ans += dX[jx]*dY[jy]
            qq.append((jx,jy))
        if burst:break
    q = qq

print(ans)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*N ]
DEF = [list(map(int,input().split())) for _ in [0]*M ]

X = [0]*3003
Y = [0]*3003
INF = 10**9 + 10
X[-1] = -INF
Y[-1] = -INF
X[-2] = INF
Y[-2] = INF
i = 0
j = 0
for a,b,c in ABC:
    X[i] = a
    X[i+1] = b
    Y[j] = c
    i += 2
    j += 1
for d,e,f in DEF:
    X[i] = d
    Y[j] = e
    Y[j+1] = f
    i += 1
    j += 2
X = sorted(set(X))
Y = sorted(set(Y))
Xdict = {x:i for i,x in enumerate(X)}
Ydict = {y:i for i,y in enumerate(Y)}

nx = len(X)
ny = len(Y)
nxy = nx * ny
E = {(x,y):{(X[i-1],y),(X[i+1],y),(x,Y[j-1]),(x,Y[j+1])} for i,x in enumerate(X[1:-1],1) for j,y in enumerate(Y[1:-1],1)}

x0 = X[0]
x1 = X[-1]
for y in Y:
    E[(x0,y)] = set()
    E[(x1,y)] = set()
y0 = Y[0]
y1 = Y[-1]
for x in X:
    E[(x,y0)] = set()
    E[(x,y1)] = set()

for a,b,c in ABC:
    y = Y[Ydict[c]-1]
    for x in X[Xdict[a]:Xdict[b]]:
        E[(x,c)].discard((x,y))
        E[(x,y)].discard((x,c))

for d,e,f in DEF:
    x = X[Xdict[d]-1]
    for y in Y[Ydict[e]:Ydict[f]]:
        E[(d,y)].discard((x,y))
        E[(x,y)].discard((d,y))

dX = {x:x2-x for x,x2 in zip(X[:-1],X[1:])}
dY = {y:y2-y for y,y2 in zip(Y[:-1],Y[1:])}
burst = False
ans = 0
done = {(0,0)}
q = [(0,0)]
while q:
    qq = []
    for i in q:
        di = d[i]
        for j in E[i]:
            if j in done:continue
            done.add(j)
            x,y = j
            if abs(x)==INF or abs(y)==INF:
                burst = True
                ans = "INF"
                qq = []
                break
            ans += dX[x]*dY[y]
            qq.append(j)
        if burst:break
    q = qq

print(ans)