Problem p02680 (python)

Fixed submission:
from collections import deque

def main():
    n, m = map(int, input().split())
    INF = 10**20
    mapX = {INF, -INF}
    mapY = {INF, -INF}
    dx = [-1, 0, 1, 0]
    dy = [0, -1, 0, 1]
    lh = [None]*n
    lv = [None]*m
    for i in range(n):
        a, b, c = map(int, input().split())
        mapX.add(a)
        mapX.add(b)
        mapY.add(c)
        lh[i] = [a, b, c]
    for i in range(m):
        d, e, f = map(int, input().split())
        mapX.add(d)
        mapY.add(e)
        mapY.add(f)
        lv[i] = [d, e, f]
        
    x_len = len(mapX)*2 - 1
    y_len = len(mapY)*2 - 1
    mapX, mapY = sorted(list(mapX)), sorted(list(mapY))
    mapXi, mapYi = {val:i for i, val in enumerate(mapX)}, {val:i for i, val in enumerate(mapY)}
    grid = [[0]*y_len for _ in range(x_len)]
    
    x_range = [0] * x_len
    y_range = [0] * y_len
    x_range[0] = x_range[-1] = INF
    y_range[0] = y_range[-1] = INF
    
    for i, (x1, x2) in enumerate(zip(mapX, mapX[1:])):
        x_range[i*2 + 1] = x2 - x1
        if x1 <= 0 <= x2:
            sx = i*2 + 1
    
    for i, (y1, y2) in enumerate(zip(mapY, mapY[1:])):
        y_range[i*2 + 1] = y2 - y1
        if y1 <= 0 <= y2:
            sy = i*2 + 1
    
    for a, b, c in lh:
        y = mapYi[c]*2
        for x in range(mapXi[a]*2+1, mapXi[b]*2+1, 2):
            grid[x][y] = 1
    for a, b, c in lv:
        x = mapXi[a]*2
        for y in range(mapYi[b]*2+1, mapYi[c]*2+1, 2):
            grid[x][y] = 1
    q = deque()
    grid[sx][sy] = 1
    q.appendleft([sx, sy])
    ans = x_range[sx] * y_range[sy]
    while q:
        x, y = q.pop()
        for i in range(4):
            nx, ny = x+dx[i], y+dy[i]
            if grid[nx][ny]:
                continue
            nx += dx[i]
            ny += dy[i]
            if grid[nx][ny]:
                continue
            grid[nx][ny] = 1
            q.appendleft([nx, ny])
            ans += x_range[nx]*y_range[ny]
            if ans >= INF:
                print("INF")
                return
    print(ans)
        
            
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    n, m = map(int, input().split())
    mapX = dict()
    mapY = dict()
    mapX["0"] = 0
    mapY["0"] = 0
    dx = [-1, 0, 1, 0]
    dy = [0, -1, 0, 1]
    lh = [None]*n
    lv = [None]*m
    for i in range(n):
        a, b, c = input().split()
        mapX[a], mapX[b], mapY[c] = 0, 0, 0
        lh[i] = [a, b, c]
    for i in range(m):
        d, e, f = input().split()
        mapX[d], mapY[e], mapY[f] = 0, 0, 0
        lv[i] = [f, e, d]
    xs, ys = [], []
    for k in mapX.keys():
        mapX[k] = len(xs)
        xs.append(k)
    for k in mapY.keys():
        mapY[k] = len(ys)
        ys.append(k)
    h = len(xs)*2
    w = len(ys)*2
    
    d = [[None]*w for _ in range(h)]
    for i in range(n):
        a, b, c = mapX[lh[i][0]]*2, mapX[lh[i][1]]*2, mapY[lh[i][2]]*2
        for x in range(a, b+1):
            d[x][c] = -1
    for i in range(m):
        a, b, c = mapY[lv[i][0]]*2, mapY[lv[i][1]]*2, mapX[lv[i][2]]*2
        for x in range(a, b+1):
            d[c][y] = -1
    q = deque()
    sx, sy = mapX["0"]*2, mapY["0"]*2
    d[sx][sy] = 1
    q.appendleft([sx, sy])
    while q:
        x, y = q.pop()
        for i in range(4):
            nx, ny = x+dx[i], y+dy[i]
            if nx < 0 or ny >= h:
                continue
            if ny < 0 or ny >= w:
                continue
            if d[nx][ny] != 0:
                continue
            d[nx][ny] = 1
            q.appendleft(nx, ny)
    ans = 0
    for x in range(h):
        for y in range(w):
            if d[x][y] != 1:
                continue
            if x*y == 0 or x == h-1 or y == w-1:
                print("INF")
                return
            if (x%2) * (y%2) == 0:
                continue
            ex = xs[x//2 + 1] - xs[x//2]
            ey = ys[y//2 + 1] - ys[y//2]
            ans += ex*ey
    print(ans)
    return
        
            
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    n, m = map(int, input().split())
    mapX = dict()
    mapY = dict()
    mapX["0"] = 0
    mapY["0"] = 0
    dx = [-1, 0, 1, 0]
    dy = [0, -1, 0, 1]
    lh = [None]*n
    lv = [None]*m
    for i in range(n):
        a, b, c = input().split()
        mapX[a], mapX[b], mapY[c] = 0, 0, 0
        lh[i] = [a, b, c]
    for i in range(m):
        d, e, f = input().split()
        mapX[d], mapY[e], mapY[f] = 0, 0, 0
        lv[i] = [f, e, d]
    xs, ys = [], []
    for k in mapX.keys():
        mapX[k] = len(xs)
        xs.append(k)
    for k in mapY.keys():
        mapY[k] = len(ys)
        ys.append(k)
    h = len(xs)*2
    w = len(ys)*2
    
    d = [[None]*w for _ in range(h)]
    for i in range(n):
        a, b, c = mapX[lh[i][0]]*2, mapX[lh[i][1]]*2, mapY[lh[i][2]]*2
        for x in range(a, b+1):
            d[x][c] = -1
    for i in range(m):
        a, b, c = mapY[lv[i][0]]*2, mapY[lv[i][1]]*2, mapX[lv[i][2]]*2
        for x in range(a, b+1):
            d[c][y] = -1
    q = deque()
    sx, sy = mapX["0"]*2, mapY["0"]*2
    d[sx][sy] = 1
    q.appendleft([sx, sy])
    while q:
        x, y = q.pop()
        for i in range(4):
            nx, ny = x+dx[i], y+dy[i]
            if nx < 0 or ny >= h:
                continue
            if ny < 0 or ny >= w:
                continue
            if d[nx][ny] != 0:
                continue
            d[nx][ny] = 1
            q.appendleft(nx, ny)
    ans = 0
    for x in range(h):
        for y in range(w):
            if d[x][y] != 1:
                continue
            if x*y == 0 or x == h-1 or y == w-1:
                print("INF")
                return
            if (x%2) * (y%2) == 0:
                continue
            ex = xs[x//2 + 1] - xs[x//2]
            ey = ys[y//2 + 1] - ys[y//2]
    print(ans)
    return
        
            
if __name__ == "__main__":
    main()