Problem:


Score: 600 points

Problem Statement

There is a grass field that stretches infinitely.

In this field, there is a negligibly small cow. Let (x, y) denote the point that is x\ \mathrm{cm} south and y\ \mathrm{cm} east of the point where the cow stands now. The cow itself is standing at (0, 0).

There are also N north-south lines and M east-west lines drawn on the field. The i-th north-south line is the segment connecting the points (A_i, C_i) and (B_i, C_i), and the j-th east-west line is the segment connecting the points (D_j, E_j) and (D_j, F_j).

What is the area of the region the cow can reach when it can move around as long as it does not cross the segments (including the endpoints)? If this area is infinite, print INF instead.

Constraints

All values in input are integers between -10^9 and 10^9  (inclusive).

1 \leq N, M \leq 1000

A_i < B_i\ (1 \leq i \leq N)

E_j < F_j\ (1 \leq j \leq M)

The point (0, 0) does not lie on any of the given segments.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
:
A_N B_N C_N
D_1 E_1 F_1
:
D_M E_M F_M

Output

If the area of the region the cow can reach is infinite, print INF; otherwise, print an integer representing the area in \mathrm{cm^2}.

(Under the constraints, it can be proved that the area of the region is always an integer if it is not infinite.)

Sample Input 1

5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4

Sample Output 1

13

The area of the region the cow can reach is 13\ \mathrm{cm^2}.

Sample Input 2

6 1
-3 -1 -2
-3 -1 1
-2 -1 2
1 4 -2
1 4 -1
1 4 1
3 1 4

Sample Output 2

INF

The area of the region the cow can reach is infinite.



Sample input:
5 6
1 2 0
0 1 1
0 2 2
-3 4 -1
-2 6 3
1 0 1
0 1 2
2 0 2
-1 -4 5
3 -2 4
1 2 4


Sample output:
13

Submission status: Runtime Error

Solution code:
from numba import njit
import numpy as np
#'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

abcs = []
defs = []
for _ in range(N-2):
    a, b = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    c = random.randint(-10**9, 10**9)
    if a == b:
        abcs.append((a, b+1, c))
    else:
        abcs.append((a, b, c))

abcs += [(-10**9, 10**9, -10**9), (-10**9, 10**9, 10**9)]

for _ in range(M-2):
    e, f = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    d = random.randint(-10**9, 10**9)
    if e == f:
        defs.append((d, e, f+1))
    else:
        defs.append((d, e, f))
    
defs += [(-10**9, -10**9, 10**9), (10**9, -10**9, 10**9)]

#'''

xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x) + 1
y_size = 2 * len(scatter_y) + 1

field = np.zeros((x_size, y_size), np.int32)

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1

@njit
def imos(field):
    for x in range(x_size-1):
        for y in range(y_size-1):
            field[x][y+1] += field[x][y]

    for y in range(y_size-1):
        for x in range(x_size-1):
            field[x+1][y] += field[x][y]

    return field

field = imos(field)

area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = 1
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy] == 0:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = 1

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size or y == y_size:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)

Submission status: Runtime Error

Solution code:
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()

Submission status: Runtime Error

Solution code:
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()

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
INF = 4 * 10**18 + 1

X = [0]
Y = [0]
X_line = []
Y_line = [] 

for i in range(N):
  A, B, C = map(int, input().split())
  if A > B:
    A, B = B, A
  X.append(A)
  X.append(B)
  Y.append(C)
  X_line.append((A,B,C)) 
  
for j in range(M):
  D, E, F = map(int, input().split())
  if E > F:
    E, F = F, E
  X.append(D)
  Y.append(E)
  Y.append(F)
  Y_line.append((D,E,F)) 

X.sort()
Y.sort()
X = list(set(X))
Y = list(set(Y))
X.sort()
Y.sort()

X_dict = {}
Y_dict = {}

i = 0
for x in X:
  X_dict[x] = 2 * i + 1
  i += 1
j = 0
for y in Y:
  Y_dict[y] = 2 * j + 1
  j += 1

G = []
for i in range(2*len(X)+1):
  G.append([])
  for j in range(2*len(Y)+1):
    if i % 2 == 1 or j % 2 == 1:
      G[i].append(0) #奇数の箇所が面積0の要素を表す
    else:
      if 1 <= i//2 < len(X) and 1 <= j//2 < len(Y):
        G[i].append( (X[i//2]-X[i//2-1])*(Y[j//2]-Y[j//2-1]) )
      else:
        G[i].append(INF)#各ノードが持つ面積を記録、最外殻は面積無限大 
        
for (A, B, C) in X_line:
  xd = X_dict[A]
  yd = Y_dict[C] 
  #print(xd, yd, X_dict[B], len(X), len(Y))
  while xd <= X_dict[B]:
    #print(xd, yd, X_dict[B], len(X), len(Y))
    G[xd][yd] = -1 #壁は面積-1で区別
    xd += 1
    
for (D, E, F) in Y_line:
  xd = X_dict[D]
  yd = Y_dict[E] 
  while yd <= Y_dict[F]:
    G[xd][yd] = -1 #壁は面積-1で区別
    yd += 1
    
#探索により面積導出
ans = 0
que = []
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
while que and ans < INF:
  xd, yd = que.pop(-1)
  #print(xd, yd, G[xd][yd])
  if G[xd][yd] >= 0: 
    ans += G[xd][yd]
    G[xd][yd] = -1
    if G[xd+1][yd] >= 0 and xd < 2*len(X):
      que.append((xd+1,yd))
    if G[xd-1][yd] >= 0 and xd > 0:
      que.append((xd-1,yd))
    if G[xd][yd+1] >= 0 and yd < 2*len(Y):
      que.append((xd,yd+1))
    if G[xd][yd-1] >= 0 and yd > 0:
      que.append((xd,yd-1))
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
INF = 10**20 + 1

X = [0]
Y = [0]
X_line = []
Y_line = [] 

for i in range(N):
  A, B, C = map(int, input().split())
  if A > B:
    A, B = B, A
  X.append(A)
  X.append(B)
  Y.append(C)
  X_line.append((A,B,C)) 
  
for j in range(M):
  D, E, F = map(int, input().split())
  if E > F:
    E, F = F, E
  X.append(D)
  Y.append(E)
  Y.append(F)
  Y_line.append((D,E,F)) 

X.sort()
Y.sort()
X = list(set(X))
Y = list(set(Y))
X.sort()
Y.sort()

X_dict = {}
Y_dict = {}

i = 0
for x in X:
  X_dict[x] = 2 * i + 1
  i += 1
j = 0
for y in Y:
  Y_dict[y] = 2 * j + 1
  j += 1

G = []
for i in range(2*len(X)+1):
  G.append([])
  for j in range(2*len(Y)+1):
    if i % 2 == 1 or j % 2 == 1:
      G[i].append(0) #奇数の箇所が面積0の要素を表す
    else:
      if 1 <= i//2 < len(X) and 1 <= j//2 < len(Y):
        G[i].append( (X[i//2]-X[i//2-1])*(Y[j//2]-Y[j//2-1]) )
      else:
        G[i].append(INF)#各ノードが持つ面積を記録、最外殻は面積無限大 
        
for (A, B, C) in X_line:
  xd = X_dict[A]
  yd = Y_dict[C] 
  #print(xd, yd, X_dict[B], len(X), len(Y))
  while xd <= X_dict[B]:
    #print(xd, yd, X_dict[B], len(X), len(Y))
    G[xd][yd] = -1 #壁は面積-1で区別
    xd += 1
    
for (D, E, F) in Y_line:
  xd = X_dict[D]
  yd = Y_dict[E] 
  while yd <= Y_dict[F]:
    G[xd][yd] = -1 #壁は面積-1で区別
    yd += 1
    
#探索により面積導出
ans = 0
que = []
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
while que and ans < INF:
  xd, yd = que.pop(-1)
  #print(xd, yd, G[xd][yd])
  if G[xd][yd] >= 0: 
    ans += G[xd][yd]
    G[xd][yd] = -1
    if G[xd+1][yd] >= 0 and xd < 2*len(X):
      que.append((xd+1,yd))
    if G[xd-1][yd] >= 0 and xd > 0:
      que.append((xd-1,yd))
    if G[xd][yd+1] >= 0 and yd < 2*len(Y):
      que.append((xd,yd+1))
    if G[xd][yd-1] >= 0 and yd > 0:
      que.append((xd,yd-1))
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
INF = 10**20 + 1

X = [0]
Y = [0]
X_line = []
Y_line = [] 

for i in range(N):
  A, B, C = map(int, input().split())
  if A > B:
    A, B = B, A
  X.append(A)
  X.append(B)
  Y.append(C)
  X_line.append((A,B,C)) 
  
for j in range(M):
  D, E, F = map(int, input().split())
  if E > F:
    E, F = F, E
  X.append(D)
  Y.append(E)
  Y.append(F)
  Y_line.append((D,E,F)) 

X = list(set(X))
Y = list(set(Y))
X.sort()
Y.sort()

X_dict = {}
Y_dict = {}

i = 0
for x in X:
  X_dict[x] = 2 * i + 1
  i += 1
j = 0
for y in Y:
  Y_dict[y] = 2 * j + 1
  j += 1

G = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
for i in range(2*len(X)+1):
  for j in range(2*len(Y)+1):
    if i % 2 == 1 or j % 2 == 1:
      G[i][j] = 0 #奇数の箇所が面積0の要素を表す
    else:
      if 1 <= i//2 < len(X) and 1 <= j//2 < len(Y):
        G[i][j] = (X[i//2]-X[i//2-1])*(Y[j//2]-Y[j//2-1])
      else:
        G[i][j] = INF #各ノードが持つ面積を記録、最外殻は面積無限大 
        
for (A, B, C) in X_line:
  xd = X_dict[A]
  yd = Y_dict[C] 
  #print(xd, yd, X_dict[B], len(X), len(Y))
  while xd <= X_dict[B]:
    #print(xd, yd, X_dict[B], len(X), len(Y))
    G[xd][yd] = -1 #壁は面積-1で区別
    xd += 1
    
for (D, E, F) in Y_line:
  xd = X_dict[D]
  yd = Y_dict[E] 
  while yd <= Y_dict[F]:
    G[xd][yd] = -1 #壁は面積-1で区別
    yd += 1
    
#探索により面積導出
ans = 0
que = []
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
while que and ans < INF:
  xd, yd = que.pop(-1)
  #print(xd, yd, G[xd][yd])
  if G[xd][yd] >= 0: 
    ans += G[xd][yd]
    G[xd][yd] = -1
    if G[xd+1][yd] >= 0 and xd < 2*len(X):
      que.append((xd+1,yd))
    if G[xd-1][yd] >= 0 and xd > 0:
      que.append((xd-1,yd))
    if G[xd][yd+1] >= 0 and yd < 2*len(Y):
      que.append((xd,yd+1))
    if G[xd][yd-1] >= 0 and yd > 0:
      que.append((xd,yd-1))
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Runtime Error

Solution code:
tate,yoko=map(int,input().split())


yy=set([0])
xx=set([0])

yyy=[]
for _ in range(tate):
    y1,y2,x=map(int,input().split())
    yy.add(y1)
    yy.add(y2)
    xx.add(x)
    yyy.append([y1,y2,x])


xxx=[[0,0,0]]
for _ in range(yoko):
    y,x1,x2=map(int,input().split())
    xx.add(x1)
    xx.add(x2)
    yy.add(y)
    xxx.append([y,x1,x2])

xx=list(xx)
yy=list(yy)
xx.sort()
yy.sort()

y=1
yindex={}
yindexrev={}
for v in yy:
    yindex[v]=y
    yindexrev[y]=v
    y+=2

x=1
xindex={}
xindexrev={}
for v in xx:
    xindex[v]=x
    xindexrev[x]=v
    x+=2

maze=[[" "]*x for _ in range(y)]


for item in yyy:
    [y1,y2,x]=item
    for y in range(len(maze)):
        maze[y][xindex[x]]="."
    
    for x3 in range(len(maze[0])):
        if maze[yindex[y1]][x3]==" ":
            maze[yindex[y1]][x3]="."
        if maze[yindex[y2]][x3]==" ":
            maze[yindex[y2]][x3]="."
    
    for y in range(yindex[y1],yindex[y2]+1):
        maze[y][xindex[x]]="#"

for item in xxx:
    [y,x1,x2]=item
    for x in range(len(maze[0])):
        if maze[yindex[y]][x]==" ":
            maze[yindex[y]][x]="."
    
    for y3 in range(len(maze[0])):
        if maze[y3][xindex[x1]]==" ":
            maze[y3][xindex[x1]]="."
        if maze[y3][xindex[x2]]==" ":
            maze[y3][xindex[x2]]="."
    for x in range(xindex[x1],xindex[x2]+1):
        maze[yindex[y]][x]="#"

cnt=0
d=[(yindex[0],xindex[0])]
from collections import deque
stack = deque(d)

#for item in maze:
#    print("".join(item))

while len(stack)>0:
    [y,x]=stack.popleft()
    #print(y,x)
    if maze[y][x]==".":
        maze[y][x]="0"
    elif maze[y][x]==" ":
        if y-1<=0 or x-1<=0 or y+1>=len(maze)-1 or x+1>=len(maze[0])-1:
            print("INF")
            exit() 
        
        tmp=(yindexrev[y+1]-yindexrev[y-1])*(xindexrev[x+1]-xindexrev[x-1])
        maze[y][x]=str(tmp)
        cnt+=tmp
    
    for (my,mx) in [(1,0),(0,1),(-1,0),(0,-1)]:
        #print(y+my,x+mx)
        if my+y==0 or my+y==len(maze)-1 or mx+x==0 or mx+x==len(maze[0])-1:
            print("INF")
            exit()
        if maze[y+my][x+mx] in [" ","."]:
            stack.append((y+my,x+mx)) 
        


#for item in maze:
#    print("".join(item))
    



print(cnt)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    from collections import deque
    from bisect import bisect_left

    N, M = map(int, input().split())
    tate = []
    yoko = []
    X = set()
    Y = set()
    for _ in range(N):
        A, B, C = map(int, readline().split())
        X.add(A)
        X.add(B)
        Y.add(C)
        tate.append((C, A, B))
    for _ in range(M):
        D, E, F = map(int, readline().split())
        X.add(D)
        Y.add(E)
        Y.add(F)
        yoko.append((D, E, F))

    X = sorted(list(X))
    Y = sorted(list(Y))
    H = len(X)
    W = len(Y)

    ix = {}
    for i, x in enumerate(X):
        ix[x] = i
    jy = {}
    for j, y in enumerate(Y):
        jy[y] = j

    wall = [[15] * W for _ in range(H)]

    for c, a, b in tate:
        ia = ix[a]
        ib = ix[b]
        jc = jy[c]
        for i in range(ia, ib):
            wall[i][jc - 1] &= 15 - (1 << 1)
            wall[i][jc] &= 15 - (1 << 3)
    for d, e, f in yoko:
        i_d = ix[d]
        je = jy[e]
        jf = jy[f]
        for j in range(je, jf):
            wall[i_d - 1][j] &= 15 - (1 << 2)
            wall[i_d][j] &= 15 - (1 << 0)

    q = deque()
    x0 = max(0, bisect_left(X, 0) - (0 not in ix))
    y0 = max(0, bisect_left(Y, 0) - (0 not in jy))
    path = [True] * (W * H)
    path[x0 * W + y0] = False
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        for i, (dx, dy) in enumerate(direction):
            if wall[x][y] & (1 << i):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                    if path[nx * W + ny]:
                        path[nx * W + ny] = False
                        q.append((nx, ny))
                else:
                    print('INF')
                    exit()
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    from collections import deque
    from bisect import bisect_left

    N, M = map(int, input().split())
    tate = []
    yoko = []
    X = set()
    Y = set()
    for _ in range(N):
        A, B, C = map(int, readline().split())
        X.add(A)
        X.add(B)
        Y.add(C)
        tate.append((C, A, B))
    for _ in range(M):
        D, E, F = map(int, readline().split())
        X.add(D)
        Y.add(E)
        Y.add(F)
        yoko.append((D, E, F))

    X = sorted(list(X))
    Y = sorted(list(Y))
    H = len(X)
    W = len(Y)

    ix = {}
    for i, x in enumerate(X):
        ix[x] = i
    jy = {}
    for j, y in enumerate(Y):
        jy[y] = j

    grid = [[15] * W for _ in range(H)]

    for c, a, b in tate:
        ia = ix[a]
        ib = ix[b]
        jc = jy[c]
        for i in range(ia, ib):
            grid[i][jc - 1] &= 15 - (1 << 1)
            grid[i][jc] &= 15 - (1 << 3)
    for d, e, f in yoko:
        i_d = ix[d]
        je = jy[e]
        jf = jy[f]
        for j in range(je, jf):
            grid[i_d - 1][j] &= 15 - (1 << 2)
            grid[i_d][j] &= 15 - (1 << 0)

    q = deque()
    x0 = bisect_left(X, 0) - (0 not in ix)
    y0 = bisect_left(Y, 0) - (0 not in jy)
    path = [True] * (W * H)
    path[x0 * W + y0] = False
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        for i, (dx, dy) in enumerate(direction):
            if grid[x][y] & (1 << i):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                    if path[nx * W + ny]:
                        path[nx * W + ny] = False
                        q.append((nx, ny))
                else:
                    print('INF')
                    exit()
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    from collections import deque
    from bisect import bisect_left

    N, M = map(int, input().split())
    tate = []
    yoko = []
    X = set()
    Y = set()
    for _ in range(N):
        A, B, C = map(int, readline().split())
        X.add(A)
        X.add(B)
        Y.add(C)
        tate.append((C, A, B))
    for _ in range(M):
        D, E, F = map(int, readline().split())
        X.add(D)
        Y.add(E)
        Y.add(F)
        yoko.append((D, E, F))

    X = sorted(list(X))
    Y = sorted(list(Y))
    H = len(X)
    W = len(Y)

    ix = {}
    for i, x in enumerate(X):
        ix[x] = i
    jy = {}
    for j, y in enumerate(Y):
        jy[y] = j

    grid = [[15] * W for _ in range(H)]

    for c, a, b in tate:
        ia = ix[a]
        ib = ix[b]
        jc = jy[c]
        for i in range(ia, ib):
            grid[i][jc - 1] &= 15 - (1 << 1)
            grid[i][jc] &= 15 - (1 << 3)
    for d, e, f in yoko:
        i_d = ix[d]
        je = jy[e]
        jf = jy[f]
        for j in range(je, jf):
            grid[i_d - 1][j] &= 15 - (1 << 2)
            grid[i_d][j] &= 15 - (1 << 0)

    q = deque()
    x0 = bisect_left(X, 0) - (0 not in ix)
    y0 = bisect_left(Y, 0) - (0 not in jy)
    path = [True] * (W * H)
    path[x0 * H + y0] = False
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        for i, (dx, dy) in enumerate(direction):
            if grid[x][y] & (1 << i):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                    if path[nx * H + ny]:
                        path[nx * H + ny] = False
                        q.append((nx, ny))
                else:
                    print('INF')
                    exit()
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    from collections import deque
    from bisect import bisect_left

    N, M = map(int, input().split())
    tate = []
    yoko = []
    X = set()
    Y = set()
    for _ in range(N):
        A, B, C = map(int, readline().split())
        X.add(A)
        X.add(B)
        Y.add(C)
        tate.append((C, A, B))
    for _ in range(M):
        D, E, F = map(int, readline().split())
        X.add(D)
        Y.add(E)
        Y.add(F)
        yoko.append((D, E, F))

    X = sorted(list(X))
    Y = sorted(list(Y))
    H = len(X)
    W = len(Y)

    ix = {}
    for i, x in enumerate(X):
        ix[x] = i
    jy = {}
    for j, y in enumerate(Y):
        jy[y] = j

    grid = [[15] * W for _ in range(H)]

    for c, a, b in tate:
        ia = ix[a]
        ib = ix[b]
        jc = jy[c]
        for i in range(ia, ib):
            grid[i][jc - 1] &= 15 - (1 << 1)
            grid[i][jc] &= 15 - (1 << 3)
    for d, e, f in yoko:
        i_d = ix[d]
        je = jy[e]
        jf = jy[f]
        for j in range(je, jf):
            grid[i_d - 1][j] &= 15 - (1 << 2)
            grid[i_d][j] &= 15 - (1 << 0)

    q = deque()
    x0 = bisect_left(X, 0) - (0 not in ix)
    y0 = bisect_left(Y, 0) - (0 not in jy)
    path = [[True] * W for _ in range(H)]
    path[x0][y0] = False
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        for i, (dx, dy) in enumerate(direction):
            if grid[x][y] & (1 << i):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                    if path[nx][ny]:
                        path[nx][ny] = False
                        q.append((nx, ny))
                else:
                    print('INF')
                    exit()
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    from collections import deque
    from bisect import bisect_left

    N, M = map(int, input().split())
    tate = []
    yoko = []
    X = set()
    Y = set()
    for _ in range(N):
        A, B, C = map(int, readline().split())
        X.add(A)
        X.add(B)
        Y.add(C)
        tate.append((C, A, B))
    for _ in range(M):
        D, E, F = map(int, readline().split())
        X.add(D)
        Y.add(E)
        Y.add(F)
        yoko.append((D, E, F))

    X = sorted(list(X))
    Y = sorted(list(Y))
    H = len(X)
    W = len(Y)
    # tate.sort()
    # yoko.sort()
    grid = [[[True] * 4 for _ in range(W)] for _ in range(H)]

    ix = {}
    for i, x in enumerate(X):
        ix[x] = i
    jy = {}
    for j, y in enumerate(Y):
        jy[y] = j

    for c, a, b in tate:
        ia = ix[a]
        ib = ix[b]
        jc = jy[c]
        for i in range(ia, ib):
            if jc - 1 >= 0:
                grid[i][jc - 1][1] = 0
            grid[i][jc][3] = 0
    for d, e, f in yoko:
        i_d = ix[d]
        je = jy[e]
        jf = jy[f]
        for j in range(je, jf):
            if i_d - 1 >= 0:
                grid[i_d - 1][j][2] = 0
            grid[i_d][j][0] = 0

    q = deque()
    x0 = bisect_left(X, 0) - (0 not in ix)
    y0 = bisect_left(Y, 0) - (0 not in jy)
    path = [[True] * W for _ in range(H)]
    path[x0][y0] = False
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        for i, (dx, dy) in enumerate(direction):
            if grid[x][y][i]:
                nx = x + dx
                ny = y + dy
                if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                    if path[nx][ny]:
                        path[nx][ny] = False
                        q.append((nx, ny))
                else:
                    print('INF')
                    exit()
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
from bisect import bisect_left


N, M = map(int, input().split())
tate = []
yoko = []
X = set()
Y = set()
for _ in range(N):
    A, B, C = map(int, input().split())
    X.add(A)
    X.add(B)
    Y.add(C)
    tate.append((C, A, B))
for _ in range(M):
    D, E, F = map(int, input().split())
    X.add(D)
    Y.add(E)
    Y.add(F)
    yoko.append((D, E, F))

X = sorted(list(X))
Y = sorted(list(Y))
H = len(X)
W = len(Y)
tate.sort()
yoko.sort()
grid = [[[1] * 4 for _ in range(W)] for _ in range(H)]

ix = {}
for i, x in enumerate(X):
    ix[x] = i
jy = {}
for j, y in enumerate(Y):
    jy[y] = j

for c, a, b in tate:
    ia = ix[a]
    ib = ix[b]
    jc = jy[c]
    for i in range(ia, ib):
        if jc - 1 >= 0:
            grid[i][jc - 1][1] = 0
        grid[i][jc][3] = 0
for d, e, f in yoko:
    i_d = ix[d]
    je = jy[e]
    jf = jy[f]
    for j in range(je, jf):
        if i_d - 1 >= 0:
            grid[i_d - 1][j][2] = 0
        grid[i_d][j][0] = 0

q = deque()
x0 = bisect_left(X, 0) - (0 not in ix)
y0 = bisect_left(Y, 0) - (0 not in jy)
path = [[1] * W for _ in range(H)]
path[x0][y0] = 0
q.append((x0, y0))
direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
cnt = 0
while q:
    x, y = q.popleft()
    cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
    for i, (dx, dy) in enumerate(direction):
        if grid[x][y][i]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                if path[nx][ny]:
                    path[nx][ny] = 0
                    q.append((nx, ny))
            else:
                print('INF')
                exit()
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
from bisect import bisect_left


N, M = map(int, input().split())
tate = []
yoko = []
X = set()
Y = set()
for _ in range(N):
    A, B, C = map(int, input().split())
    X.add(A)
    X.add(B)
    Y.add(C)
    tate.append((C, A, B))
for _ in range(M):
    D, E, F = map(int, input().split())
    X.add(D)
    Y.add(E)
    Y.add(F)
    yoko.append((D, E, F))

X = sorted(list(X))
Y = sorted(list(Y))
H = len(X)
W = len(Y)
tate.sort()
yoko.sort()
grid = [[[1] * 4 for _ in range(W)] for _ in range(H)]

ix = {}
for i, x in enumerate(X):
    ix[x] = i
jy = {}
for j, y in enumerate(Y):
    jy[y] = j

for c, a, b in tate:
    ia = ix[a]
    ib = ix[b]
    jc = jy[c]
    for i in range(ia, ib):
        if jc - 1 >= 0:
            grid[i][jc - 1][1] = 0
        grid[i][jc][3] = 0
for d, e, f in yoko:
    i_d = ix[d]
    je = jy[e]
    jf = jy[f]
    for j in range(je, jf):
        if i_d - 1 >= 0:
            grid[i_d - 1][j][2] = 0
        grid[i_d][j][0] = 0

q = deque()
x0 = bisect_left(X, 0)
y0 = bisect_left(Y, 0)
path = [[1] * W for _ in range(H)]
path[x0][y0] = 0
q.append((x0, y0))
direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
cnt = 0
while q:
    x, y = q.popleft()
    cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
    for i, (dx, dy) in enumerate(direction):
        if grid[x][y][i]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                if path[nx][ny]:
                    path[nx][ny] = 0
                    q.append((nx, ny))
            else:
                print('INF')
                exit()
print(cnt)