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: Time Limit Exceeded

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


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)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
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]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            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: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
for i_2 in range(len(X)+1):
  for j_2 in range(len(Y)+1):
    #if i % 2 == 1 or j % 2 == 1:
    #  continue #G[i][j] = 0, 奇数の箇所が面積0の要素を表す
    #else:
    if 1 <= i_2 < len(X) and 1 <= j_2 < len(Y):
      G[i_2*2][j_2*2] = (X[i_2]-X[i_2-1])*(Y[j_2]-Y[j_2-1])
    else:
      G[i_2*2][j_2*2] = INF #各ノードが持つ面積を記録、最外殻は面積無限大 

#print(G[X_dict[0]])
for (A, B, C) in X_line:
  yd = Y_dict[C] 
  for xd in range(X_dict[A], X_dict[B]+1):
    G[xd][yd] = -1 #壁は面積-1で区別
    #xd += 1
    
for (D, E, F) in Y_line:
  xd = X_dict[D]
  for yd in range(Y_dict[E], Y_dict[F]+1):
    G[xd][yd] = -1 #壁は面積-1で区別
    #yd += 1

#print(G[X_dict[0]][Y_dict[0]])
#探索により面積導出
ans = 0
que = []
visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
#print(visited, len(Y))
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
for i_2 in range(len(X)+1):
  for j_2 in range(len(Y)+1):
    #if i % 2 == 1 or j % 2 == 1:
    #  continue #G[i][j] = 0, 奇数の箇所が面積0の要素を表す
    #else:
    if 1 <= i_2 < len(X) and 1 <= j_2 < len(Y):
      G[i_2*2][j_2*2] = (X[i_2]-X[i_2-1])*(Y[j_2]-Y[j_2-1])
    else:
      G[i_2*2][j_2*2] = INF #各ノードが持つ面積を記録、最外殻は面積無限大 

#print(G[X_dict[0]])
for (A, B, C) in X_line:
  yd = Y_dict[C] 
  for xd in range(X_dict[A], X_dict[B]+1):
    G[xd][yd] = -1 #壁は面積-1で区別
    #xd += 1
    
for (D, E, F) in Y_line:
  xd = X_dict[D]
  for yd in range(Y_dict[E], Y_dict[F]+1):
    G[xd][yd] = -1 #壁は面積-1で区別
    #yd += 1

#print(G[X_dict[0]][Y_dict[0]])
#探索により面積導出
ans = 0
que = []
visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
#print(visited, len(Y))
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
for i_2 in range(len(X)+1):
  for j_2 in range(len(Y)+1):
    #if i % 2 == 1 or j % 2 == 1:
    #  continue #G[i][j] = 0, 奇数の箇所が面積0の要素を表す
    #else:
    if 1 <= i_2 < len(X) and 1 <= j_2 < len(Y):
      G[i_2*2][j_2*2] = (X[i_2]-X[i_2-1])*(Y[j_2]-Y[j_2-1])
    else:
      G[i_2*2][j_2*2] = INF #各ノードが持つ面積を記録、最外殻は面積無限大 

#print(G[X_dict[0]])
for (A, B, C) in X_line:
  xd = X_dict[A]
  yd = Y_dict[C] 
  while xd <= X_dict[B]:
    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

#print(G[X_dict[0]][Y_dict[0]])
#探索により面積導出
ans = 0
que = []
visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
#print(visited, len(Y))
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for j in range(2*len(Y)+1)] for i in range(2*len(X)+1)]
for i_2 in range(len(X)+1):
  for j_2 in range(len(Y)+1):
    #if i % 2 == 1 or j % 2 == 1:
    #  continue #G[i][j] = 0, 奇数の箇所が面積0の要素を表す
    #else:
    if 1 <= i_2 < len(X) and 1 <= j_2 < len(Y):
      G[i_2*2][j_2*2] = (X[i_2]-X[i_2-1])*(Y[j_2]-Y[j_2-1])
    else:
      G[i_2*2][j_2*2] = 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 = []
visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

N, M = map(int, input().split())
INF = 5 * 10**18 + 1

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for j in range(2*len(Y)+1)] for i 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:
      continue #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] = -2*INF #壁は面積-2*INFで区別
    yd += 1
    
#探索により面積導出
ans = 0
que = deque([])
que_append = que.append
que_pop = que.pop

#visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que_append((X_dict[0],Y_dict[0]))
  G[X_dict[0]][Y_dict[0]] *= -1
  G[X_dict[0]][Y_dict[0]] -= 1
  #visited[X_dict[0]][Y_dict[0]] = 1
while que and ans < INF:
  xd, yd = que_pop()
  #print(xd, yd, G[xd][yd])
  #print(que)
  if G[xd][yd] > -2*INF: 
    ans -= (1 + G[xd][yd])
    G[xd][yd] = -INF
    if xd < 2*len(X) and G[xd+1][yd] >= 0: #visited[xd+1][yd] == 0:
      que_append((xd+1,yd))
      G[xd+1][yd] *= -1 #訪れた印として-1倍-1する
      G[xd+1][yd] -= 1
      #visited[xd+1][yd] = 1
    if xd > 0 and G[xd-1][yd] >= 0: #visited[xd-1][yd] == 0:
      que_append((xd-1,yd))
      G[xd-1][yd] *= -1 #訪れた印として-1倍-1する
      G[xd-1][yd] -= 1
      #visited[xd-1][yd] = 1
    if yd < 2*len(Y) and G[xd][yd+1] >= 0: #visited[xd][yd+1] == 0:
      que_append((xd,yd+1))
      G[xd][yd+1] *= -1 #訪れた印として-1倍-1する
      G[xd][yd+1] -= 1
      #visited[xd][yd+1] = 1
    if yd > 0 and G[xd][yd-1] >= 0:#visited[xd][yd-1] == 0:
      que_append((xd,yd-1))
      G[xd][yd-1] *= -1 #訪れた印として-1倍-1する
      G[xd][yd-1] -= 1
      #visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
INF = 5 * 10**18 + 1

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for j in range(2*len(Y)+1)] for i 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:
      continue #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] = -2*INF #壁は面積-2*INFで区別
    yd += 1
    
#探索により面積導出
ans = 0
que = []
que_append = que.append
que_pop = que.pop

#visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que_append((X_dict[0],Y_dict[0]))
  G[X_dict[0]][Y_dict[0]] *= -1
  G[X_dict[0]][Y_dict[0]] -= 1
  #visited[X_dict[0]][Y_dict[0]] = 1
while que and ans < INF:
  xd, yd = que_pop(-1)
  
  #print(xd, yd, G[xd][yd])
  #print(que)
  if G[xd][yd] > -2*INF: 
    ans -= (1 + G[xd][yd])
    G[xd][yd] = -INF
    if xd < 2*len(X) and G[xd+1][yd] >= 0: #visited[xd+1][yd] == 0:
      que_append((xd+1,yd))
      G[xd+1][yd] *= -1 #訪れた印として-1倍-1する
      G[xd+1][yd] -= 1
      #visited[xd+1][yd] = 1
    if xd > 0 and G[xd-1][yd] >= 0: #visited[xd-1][yd] == 0:
      que_append((xd-1,yd))
      G[xd-1][yd] *= -1 #訪れた印として-1倍-1する
      G[xd-1][yd] -= 1
      #visited[xd-1][yd] = 1
    if yd < 2*len(Y) and G[xd][yd+1] >= 0: #visited[xd][yd+1] == 0:
      que_append((xd,yd+1))
      G[xd][yd+1] *= -1 #訪れた印として-1倍-1する
      G[xd][yd+1] -= 1
      #visited[xd][yd+1] = 1
    if yd > 0 and G[xd][yd-1] >= 0:#visited[xd][yd-1] == 0:
      que_append((xd,yd-1))
      G[xd][yd-1] *= -1 #訪れた印として-1倍-1する
      G[xd][yd-1] -= 1
      #visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for j in range(2*len(Y)+1)] for i 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:
      continue #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 = []
que_append = que.append
que_pop = que.pop

visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que_append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que_append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que_append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que_append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que_append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

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

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

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 = [[0 for j in range(2*len(Y)+1)] for i 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:
      continue #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 = []
visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

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 = []
visited = [[0 for _ in range(2*len(Y)+1)] for _ in range(2*len(X)+1)]
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
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 xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

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 = []
visited = set()
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited.add((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 (xd+1,yd) not in visited and xd < 2*len(X):
      que.append((xd+1,yd))
      visited.add((xd+1,yd))
    if (xd-1,yd) not in visited and xd > 0:
      que.append((xd-1,yd))
      visited.add((xd-1,yd))
    if (xd,yd+1) not in visited and yd < 2*len(Y):
      que.append((xd,yd+1))
      visited.add((xd,yd+1))
    if (xd,yd-1) not in visited and yd > 0:
      que.append((xd,yd-1))
      visited.add((xd,yd-1))
    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict

input = sys.stdin.readline
n, m = map(int, input().split())

xx = set([0]); yy = set([0]);
aa = []; bb = []; cc = []; dd = []; ee = []; ff = []; 
for _ in range(n):
    a, b, c = map(int, input().split())
    xx.add(a)
    xx.add(b)
    yy.add(c)
    aa.append(a); bb.append(b); cc.append(c); 
for _ in range(m):
    d, e, f = map(int, input().split())
    xx.add(d)
    yy.add(e)
    yy.add(f)
    dd.append(d); ee.append(e); ff.append(f); 
xx = sorted(list(xx))
yy = sorted(list(yy))
xp = defaultdict(int)
yp = defaultdict(int)

for i, v in enumerate(xx):
    xp[v] = i
for i, v in enumerate(yy):
    yp[v] = i

h = len(xx) - 1
w = len(yy) - 1
field = [[[0, 0, 0, 0] for _ in range(w)] for _ in range(h)]

for i in range(n):
    a, b, c = aa[i], bb[i], cc[i]
    for x in range(xp[a], xp[b]):
        if yp[c] < w:
            field[x][yp[c]][1] = 1
        if yp[c] - 1 >= 0:
            field[x][yp[c] - 1][3] = 1

for i in range(m):
    d, e, f = dd[i], ee[i], ff[i]
    for y in range(yp[e], yp[f]):
        if xp[d] < h:
            field[xp[d]][y][0] = 1
        if xp[d] - 1 >= 0:
            field[xp[d] - 1][y][2] = 1

visited = [[False for _ in range(w)] for _ in range(h)]
sx, sy = min(xp[0], h - 1), min(yp[0], w - 1)
visited[sx][sy] = True
q = [(sx, sy)]
ans = 0

while q:
    x, y = q.pop()
    ans += (xx[x + 1] - xx[x]) * (yy[y + 1] - yy[y])
    if field[x][y][0] == 0:
        if x == 0:
            print('INF'); exit();
        if not visited[x - 1][y]:
            visited[x - 1][y] = True
            q.append((x - 1, y))
    if field[x][y][1] == 0:
        if y == 0:
            print('INF'); exit();
        if not visited[x][y - 1]:
            visited[x][y - 1] = True
            q.append((x, y - 1))
    if field[x][y][2] == 0:
        if x == h - 1:
            print('INF'); exit();
        if not visited[x + 1][y]:
            visited[x + 1][y] = True
            q.append((x + 1, y))
    if field[x][y][3] == 0:
        if y == w - 1:
            print('INF'); exit();
        if not visited[x][y + 1]:
            visited[x][y + 1] = True
            q.append((x, y + 1))

print(ans)