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: Wrong Answer

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]))

    
if ans >= INF:
  print("INF")
else:
  print(ans)

Submission status: Wrong Answer

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)):
        
        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: Wrong Answer

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)

    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 = deque()
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        if x == H - 1 or y == W - 1:
            print('INF')
            exit()
        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 and 0 <= ny < W and path[nx * W + ny]:
                    path[nx * W + ny] = False
                    q.append((nx, ny))
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


class UnionFind():
    def __init__(self, n):
        self.par = [-1] * n

    def root(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.root(self.par[x])  # contraction
            return self.par[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x != y:
            if self.par[x] > self.par[y]:  # merge technique
                x, y = y, x
            self.par[x] += self.par[y]
            self.par[y] = x

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.par[self.root(x)]


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(set(x), key=key, reverse=reverse)):
        zipped[xi] = i
        unzipped[i] = xi
    return zipped, unzipped


n, m = map(int, input().split())
line_v = [tuple(map(int, input().split())) for _ in range(n)]
line_h = [tuple(map(int, input().split())) for _ in range(m)]

INF = 10**9 + 10
x_list = [-INF, 0, INF]
y_list = [-INF, 0, INF]
for x1, x2, y in line_v:
    # abc
    x_list.append(x1)
    x_list.append(x2)
    y_list.append(y)
for x, y1, y2 in line_h:
    # def
    x_list.append(x)
    y_list.append(y1)
    y_list.append(y2)

x_zipped, x_unzipped = compress_coordinate(x_list)
y_zipped, y_unzipped = compress_coordinate(y_list)

nx = len(x_zipped)
ny = len(y_zipped)

v_ng = [[0] * (ny + 1) for _ in range(nx + 1)]
h_ng = [[0] * (ny + 1) for _ in range(nx + 1)]
for x1, x2, y in line_v:
    x1 = x_zipped[x1]
    x2 = x_zipped[x2]
    y = y_zipped[y]
    v_ng[x1][y] += 1
    v_ng[x2][y] -= 1
for x, y1, y2 in line_h:
    x = x_zipped[x]
    y1 = y_zipped[y1]
    y2 = y_zipped[y2]
    h_ng[x][y1] += 1
    h_ng[x][y2] -= 1

for x in range(nx):
    for y in range(ny):
        v_ng[x + 1][y] += v_ng[x][y]
        h_ng[x][y + 1] += h_ng[x][y]

# print(nx, ny)
uf = UnionFind(nx * ny)
for x in range(nx):
    for y in range(ny):
        c0 = x * ny + y
        # print(x, y, c0)
        if y + 1 < ny and v_ng[x][y + 1] == 0:
            c1 = x * ny + (y + 1)
            uf.unite(c0, c1)
        if x + 1 < nx and h_ng[x + 1][y] == 0:
            c2 = (x + 1) * ny + y
            uf.unite(c0, c2)

c_start = x_zipped[0] * nx + y_zipped[0]
if uf.same(c_start, 0):
    ans = 'INF'
else:
    ans = 0
    for x in range(nx):
        for y in range(ny):
            if uf.same(c_start, x * ny + y):
                ans += (x_unzipped[x + 1] -
                        x_unzipped[x]) * (y_unzipped[y + 1] - y_unzipped[y])

print(ans)

Submission status: Wrong Answer

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


class UnionFind():
    def __init__(self, n):
        self.par = [-1] * n

    def root(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.root(self.par[x])  # contraction
            return self.par[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x != y:
            if self.par[x] > self.par[y]:  # merge technique
                x, y = y, x
            self.par[x] += self.par[y]
            self.par[y] = x

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.par[self.root(x)]


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(set(x), key=key, reverse=reverse)):
        zipped[xi] = i
        unzipped[i] = xi
    return zipped, unzipped


n, m = map(int, input().split())
line_v = [tuple(map(int, input().split())) for _ in range(n)]
line_h = [tuple(map(int, input().split())) for _ in range(m)]

INF = 10**9 + 10
x_list = [-INF, 0, INF]
y_list = [-INF, 0, INF]
for x1, x2, y in line_v:
    # abc
    x_list.append(x1)
    x_list.append(x2)
    y_list.append(y)
for x, y1, y2 in line_h:
    # def
    x_list.append(x)
    y_list.append(y1)
    y_list.append(y2)

x_zipped, x_unzipped = compress_coordinate(x_list)
y_zipped, y_unzipped = compress_coordinate(y_list)

nx = len(x_zipped)
ny = len(y_zipped)

v_ng = [[0] * (ny + 1) for _ in range(nx + 1)]
h_ng = [[0] * (ny + 1) for _ in range(nx + 1)]
for x1, x2, y in line_v:
    x1 = x_zipped[x1]
    x2 = x_zipped[x2]
    y = y_zipped[y]
    v_ng[x1][y] += 1
    v_ng[x2][y] -= 1
for x, y1, y2 in line_h:
    x = x_zipped[x]
    y1 = y_zipped[y1]
    y2 = y_zipped[y2]
    h_ng[x][y1] += 1
    h_ng[x][y2] -= 1

for x in range(nx):
    for y in range(ny):
        v_ng[x + 1][y] += v_ng[x][y]
        h_ng[x][y + 1] += h_ng[x][y]

# print(nx, ny)
uf = UnionFind(nx * ny)
for x in range(nx):
    for y in range(ny):
        c0 = x * ny + y
        # print(x, y, c0)
        if y + 1 < ny and v_ng[x][y + 1] == 0:
            c1 = x * ny + (y + 1)
            uf.unite(c0, c1)
        if x + 1 < nx and h_ng[x + 1][y] == 0:
            c2 = (x + 1) * ny + y
            uf.unite(c0, c2)

c_start = x_zipped[0] * nx + y_zipped[0]
if uf.same(c_start, 0):
    ans = 'INF'
else:
    ans = 0
    for x in range(nx - 1):
        for y in range(ny - 1):
            if uf.same(c_start, x * ny + y):
                ans += (x_unzipped[x + 1] -
                        x_unzipped[x]) * (y_unzipped[y + 1] - y_unzipped[y])

print(ans)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.par = [-1] * n

    def root(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.root(self.par[x])  # contraction
            return self.par[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x != y:
            if self.par[x] > self.par[y]:  # merge technique
                x, y = y, x
            self.par[x] += self.par[y]
            self.par[y] = x

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.par[self.root(x)]


def compress_coordinate(x: list, key=None, reverse=False):
    zipped = {}
    unzipped = {}
    for i, xi in enumerate(sorted(set(x), key=key, reverse=reverse)):
        zipped[xi] = i
        unzipped[i] = xi
    return zipped, unzipped


n, m = map(int, input().split())
line_v = [tuple(map(int, input().split())) for _ in range(n)]
line_h = [tuple(map(int, input().split())) for _ in range(m)]

INF = 10**9 + 10
x_list = [-INF, 0, INF]
y_list = [-INF, 0, INF]
for x1, x2, y in line_v:
    # abc
    x_list.append(x1)
    x_list.append(x2)
    y_list.append(y)
for x, y1, y2 in line_h:
    # def
    x_list.append(x)
    y_list.append(y1)
    y_list.append(y2)

x_zipped, x_unzipped = compress_coordinate(x_list)
y_zipped, y_unzipped = compress_coordinate(y_list)

nx = len(x_zipped)
ny = len(y_zipped)

v_ng = [[0] * (ny + 1) for _ in range(nx + 1)]
h_ng = [[0] * (ny + 1) for _ in range(nx + 1)]
for x1, x2, y in line_v:
    x1 = x_zipped[x1]
    x2 = x_zipped[x2]
    y = y_zipped[y]
    v_ng[x1][y] += 1
    v_ng[x2][y] -= 1
for x, y1, y2 in line_h:
    x = x_zipped[x]
    y1 = y_zipped[y1]
    y2 = y_zipped[y2]
    h_ng[x][y1] += 1
    h_ng[x][y2] -= 1

for x in range(nx):
    for y in range(ny):
        v_ng[x + 1][y] += v_ng[x][y]
        h_ng[x][y + 1] += h_ng[x][y]

# print(nx, ny)
uf = UnionFind(nx * ny)
for x in range(nx):
    for y in range(ny):
        c0 = x * ny + y
        # print(x, y, c0)
        if y + 1 < ny and v_ng[x][y + 1] == 0:
            c1 = x * ny + (y + 1)
            uf.unite(c0, c1)
        if x + 1 < nx and h_ng[x + 1][y] == 0:
            c2 = (x + 1) * ny + y
            uf.unite(c0, c2)

c_start = x_zipped[0] * nx + y_zipped[0]
if uf.same(c_start, 0):
    ans = 'INF'
else:
    ans = 0
    for x in range(nx - 1):
        for y in range(ny - 1):
            if uf.same(c_start, x * ny + y):
                ans += (x_unzipped[x + 1] -
                        x_unzipped[x]) * (y_unzipped[y + 1] - y_unzipped[y])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**12 

n, m = map(int, input().split())
xseen = set()
xseen.add(INF); xseen.add(-INF)
yseen = set()
yseen.add(INF); yseen.add(-INF)
a_b_c = []
for _ in range(n):
    a, b, c = map(int, input().split())
    xseen.add(a)
    xseen.add(b)
    yseen.add(c)
    a_b_c.append((a, b, c))
d_e_f = []
for _ in range(m):
    d, e, f = map(int, input().split())
    xseen.add(d)
    yseen.add(e)
    yseen.add(f)
    d_e_f.append((d, e, f))

xlen = len(xseen) * 2 - 1
ylen = len(yseen) * 2 - 1

xseen = list(xseen)
xseen.sort()
xcomp = dict()
yseen = list(yseen)
yseen.sort()
ycomp = dict()
for i, item in enumerate(xseen):
    xcomp[item] = i
for i, item in enumerate(yseen):
    ycomp[item] = i

field = [["."] * ylen for _ in range(xlen)]
x_range = [0] * xlen
y_range = [0] * ylen
x_range[0] = x_range[-1] = INF
y_range[0] = y_range[-1] = INF
for i, (x1, x2) in enumerate(zip(xseen, xseen[1:])):
    x_range[i * 2 + 1] = x2 - x1
    if x1 <= 0 <= x2:
        sx = i * 2 + 1
for i, (y1, y2) in enumerate(zip(yseen, yseen[1:])):
    y_range[i * 2 + 1] = y2 - y1
    if y1 <= 0 <= y2:
        sy = i * 2 + 1
# print(sx, sy)

for a, b, c in a_b_c:
    y = ycomp[c] * 2
    for x in range(xcomp[a] * 2, xcomp[b] * 2 + 1):
        field[x][y] = "#"

for d, e, f in d_e_f:
    x = xcomp[d] * 2
    for y in range(ycomp[e] * 2, ycomp[f] * 2 + 1):
        field[x][y] = "#"

# for line in field:
#     print("".join([item for item in line]))

diff = [(0, 1), (1, 0), (-1, 0), (0, -1)]
ans = 0

st = []
field[sx][sy] = "#"
st.append((sx, sy))
ans += x_range[sx] * y_range[sy]
while st:
    x, y = st.pop()
    for dx, dy in diff:
        if field[x + dx][y + dy] == ".":
            field[x + dx][y + dy] = "#"
            st.append((x + dx, y + dy))
            ans += x_range[x + dx] * y_range[y + dy]
            if ans >= INF:
                print("INF")
                exit()

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from numba import njit
from bisect import *
n, m = map(int, input().split())
A, B, C = zip(*(map(int, input().split()) for _ in range(n)))
D, E, F = zip(*(map(int, input().split()) for _ in range(m)))
X = sorted(set(C + E + F))
Y = sorted(set(A + B + D))
H = len(Y)-1
W = len(X)-1
def TX(i):
    return bisect(X, i)-1
def TY(i):
    return bisect(Y, i)-1
U = np.ones((H+1, W))
for d, e, f in zip(D, E, F):
    d, e, f = TY(d), TX(e), TX(f)
    U[d, e:f] = False
L = np.ones((H, W+1))
for a ,b, c in zip(A, B, C):
    a, b, c = TY(a), TY(b), TX(c)
    L[a:b, c] = False
S = np.outer(np.diff(Y), np.diff(X))
@njit
def dfs(i, j):
    s = [(i,j)]
    v = np.zeros((H,W))
    r = 0
    while s:
        i, j = s.pop()
        if v[i,j]:
            continue
        v[i,j] = True
        if not (0<=i<H and 0<=j<W):
            return -1
        r += S[i,j]
        if U[i,j]:
            s.append((i-1,j))
        if U[i+1,j]:
            s.append((i+1,j))
        if L[i,j]:
            s.append((i,j-1))
        if L[i,j+1]:
            s.append((i,j+1))
    return r
r = dfs(TY(0), TX(0))
print('INF' if r<0 else r)

Submission status: Wrong Answer

Solution code:
import numpy as np
from bisect import *
n, m = map(int, input().split())
A, B, C = zip(*(map(int, input().split()) for _ in range(n)))
D, E, F = zip(*(map(int, input().split()) for _ in range(m)))
X = sorted(set(C + E + F))
Y = sorted(set(A + B + D))
H = len(Y)-1
W = len(X)-1
def TX(i):
    return bisect(X, i)-1
def TY(i):
    return bisect(Y, i)-1
U = np.ones((H+1, W))
for d, e, f in zip(D, E, F):
    d, e, f = TY(d), TX(e), TX(f)
    U[d, e:f] = False
L = np.ones((H, W+1))
for a ,b, c in zip(A, B, C):
    a, b, c = TY(a), TY(b), TX(c)
    L[a:b, c] = False
S = np.outer(np.diff(Y), np.diff(X)) 
i = bisect(Y, 0)-1
j = bisect(X, 0)-1
s = [(i,j)]
exit()
v = set()
r = 0
while s:
    i, j = s.pop()
    if (i,j) in v:
        continue
    v.add((i,j))
    if not (0<=i<H and 0<=j<W):
        print('INF')
        break
    r += S[i][j]
    if U[i][j]:
        s.append((i-1,j))
    if U[i+1][j]:
        s.append((i+1,j))
    if L[i][j]:
        s.append((i,j-1))
    if L[i][j+1]:
        s.append((i,j+1))
else:
    print(r)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

N, M = map(int, input().split())

LV = []
LH = []

mp_X = defaultdict(int)
mp_Y = defaultdict(int)

for _ in range(N):
    a,b,c = map(int, input().split())
    mp_X[a] = 0
    mp_X[b] = 0
    mp_Y[c] = 0
    LH.append((a,b,c))

for _ in range(M):
    d,e,f = map(int, input().split())
    mp_X[d] = 0
    mp_Y[e] = 0
    mp_Y[f] = 0
    LV.append((e,f,d))

sX = []
sY = []

for i, k in enumerate(sorted(mp_X)):
    mp_X[k] = i
    sX.append(k)
for i, k in enumerate(sorted(mp_Y)):
    mp_Y[k] = i
    sY.append(k)

sizeX = len(mp_X) * 2 - 1
sizeY = len(mp_Y) * 2 - 1

d = [[0 for _ in range(sizeY)] for _ in range(sizeX)]

for x1,x2,y in LH:
    for x in range(mp_X[x1]*2,mp_X[x2]*2+1):
        d[x][mp_Y[y]*2] = -1

for y1,y2,x in LV:
    for y in range(mp_Y[y1]*2,mp_Y[y2]*2+1):
        d[mp_X[x]*2][y] = -1

dx = [0,1,0,-1]
dy = [1,0,-1,0]

q = deque()
cx, cy = mp_X[0]*2, mp_Y[0]*2
d[cx][cy] = 1
q.append((cx,cy))
while q:
    cx, cy = q.popleft()
    for i in range(4):
        nx, ny = cx + dx[i], cy + dy[i]
        if abs(d[nx][ny]) == 1:
            continue
        d[nx][ny] = 1
        q.append((nx,ny))

ans = 0
for x in range(sizeX):
    if x % 2 == 0:
        continue

    if d[x][0] == 1 or d[x][sizeY-1] == 1:
        print("INF")
        break

    for y in range(sizeY):
        if d[0][y] == 1 or d[sizeX-1][y] == 1:
            print("INF")
            break
        if y % 2 == 0:
            continue
        if d[x][y] == 1:
            ans += (sX[x//2+1]-sX[x//2])*(sY[y//2+1]-sY[y//2])
    else:
        continue
    break
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

N, M = map(int, input().split())

LV = []
LH = []

mp_X = defaultdict(int)
mp_Y = defaultdict(int)

for _ in range(N):
    a,b,c = map(int, input().split())
    mp_X[a] = 0
    mp_X[b] = 0
    mp_Y[c] = 0
    LH.append((a,b,c))

for _ in range(M):
    d,e,f = map(int, input().split())
    mp_X[d] = 0
    mp_Y[e] = 0
    mp_Y[f] = 0
    LV.append((e,f,d))

sX = []
sY = []

for i, k in enumerate(sorted(mp_X)):
    mp_X[k] = i
    sX.append(k)
for i, k in enumerate(sorted(mp_Y)):
    mp_Y[k] = i
    sY.append(k)

sizeX = len(mp_X) * 2 - 1
sizeY = len(mp_Y) * 2 - 1

d = [[0 for _ in range(sizeY)] for _ in range(sizeX)]

for x1,x2,y in LH:
    for x in range(mp_X[x1]*2,mp_X[x2]*2+1):
        d[x][mp_Y[y]*2] = -1

for y1,y2,x in LV:
    for y in range(mp_Y[y1]*2,mp_Y[y2]*2+1):
        d[mp_X[x]*2][y] = -1

dx = [0,1,0,-1]
dy = [1,0,-1,0]

q = deque()
cx, cy = mp_X[0]*2, mp_Y[0]*2
d[cx][cy] = 1
q.append((cx,cy))
while q:
    cx, cy = q.popleft()
    for i in range(4):
        nx, ny = cx + dx[i], cy + dy[i]
        if abs(d[nx][ny]) == 1:
            continue
        d[nx][ny] = 1
        q.append((nx,ny))

ans = 0
for x in range(sizeX):
    for y in range(sizeY):
        if d[x][0] == 1 or d[x][sizeY-1] == 1 \
            or d[0][y] == 1 or d[sizeX-1][y] == 1:
            print("INF")
            break
        if x % 2 == 0 or y % 2 == 0:
            continue
        if d[x][y] == 1:
            ans += (sX[x//2+1]-sX[x//2])*(sY[y//2+1]-sY[y//2])
    else:
        continue
    break
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    N, M = map(int, input().split())
    area = N+M

    mapX = {0:0}
    mapY = {0:0}

    lh = list()
    lw = list()
    
    for i in range(area):
        if i <= N-1:
            a, b, c = map(int, input().split())
            mapX[a] = 0
            mapX[b] = 0
            mapY[c] = 0
            lh.append({'a':a, 'b':b, 'c':c})
        else:
            c, a, b = map(int, input().split())
            mapY[a] = 0
            mapY[b] = 0
            mapX[c] = 0
            lw.append({'a':a, 'b':b, 'c':c})

    mapXs = dict(sorted(mapX.items()))
    mapYs = dict(sorted(mapY.items()))

    xg = list()
    yg = list()

    for k in mapXs:
        mapXs[k] = len(xg)
        xg.append(k)

    for k in mapYs:
        mapYs[k] = len(yg)
        yg.append(k)

    #print(str(xg), '\n', str(yg))

    h = len(xg)*2
    w = len(yg)*2

    grid = np.zeros((h,w), int)

    for i in range(N):
        a = mapXs[lh[i]['a']]*2
        b = mapXs[lh[i]['b']]*2
        c = mapYs[lh[i]['c']]*2
        for j in range(a,b): grid[j][c] = -1

    for i in range(M):
        a = mapYs[lw[i]['a']]*2
        b = mapYs[lw[i]['b']]*2
        c = mapXs[lw[i]['c']]*2
        for j in range(a,b): grid[c][j] = -1

    #print('befor BFS\n', str(grid))

    cdX = mapXs[0]*2
    cdY = mapYs[0]*2
    grid[cdX][cdY] = 1

    que = [[cdX,cdY]]
    dx = [0,-1,0,1]
    dy = [-1,0,1,0]

    while len(que) != 0:
        q = que.pop(0)
        for i in range(4):
            currentX = q[0]+dx[i]
            currentY = q[1]+dy[i]
            if currentX < 0 or currentX >= h-1: continue
            if currentY < 0 or currentY >= w-1: continue
            if grid[currentX][currentY] != 0: continue

            grid[currentX][currentY]=1
            que.append([currentX,currentY])

    #print('after BFS\n', str(grid))

    ans = 0
    for x in range(h):
        for y in range(w):
            if grid[x][y] != 1: continue
            rx = x%2
            ry = y%2
            if rx == 0 or ry == 0: continue
            if (x == 0 or x == h-1) or (y == 0 or y == w-1):
                print('INF')
                return
            
            ans += (xg[rx+1]-xg[rx])*(yg[ry+1]-yg[ry])
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    N, M = map(int, input().split())
    area = N+M

    mapX = {0:0}
    mapY = {0:0}
    lh = list()
    lw = list()
    
    for i in range(area):
        if i <= N-1:
            a, b, c = map(int, input().split())
            mapX[a] = 0
            mapX[b] = 0
            mapY[c] = 0
            lh.append({'a':a, 'b':b, 'c':c})
        else:
            c, a, b = map(int, input().split())
            mapY[a] = 0
            mapY[b] = 0
            mapX[c] = 0
            lw.append({'a':a, 'b':b, 'c':c})

    mapXs = dict(sorted(mapX.items()))
    mapYs = dict(sorted(mapY.items()))

    xg = list()
    yg = list()

    for k in mapXs:
        mapXs[k] = len(xg)
        xg.append(k)

    for k in mapYs:
        mapYs[k] = len(yg)
        yg.append(k)

    #print(str(xg), '\n', str(yg))

    h = len(xg)*2
    w = len(yg)*2

    grid = np.zeros((h,w), int)

    for i in range(N):
        a = mapXs[lh[i]['a']]*2
        b = mapXs[lh[i]['b']]*2
        c = mapYs[lh[i]['c']]*2
        for j in range(a,b): grid[j][c] = -1

    for i in range(M):
        a = mapYs[lw[i]['a']]*2
        b = mapYs[lw[i]['b']]*2
        c = mapXs[lw[i]['c']]*2
        for j in range(a,b): grid[c][j] = -1

    #print('befor BFS\n', str(grid))

    que = dict()
    cdX = mapXs[0]*2
    cdY = mapYs[0]*2

    grid[cdX][cdY] = 1
    que = [[cdX,cdY]]

    dx = [0,-1,0,1]
    dy = [-1,0,1,0]

    while len(que) != 0:
        q = que.pop(0)
        for i in range(4):
            currentX = q[0]+dx[i]
            currentY = q[1]+dy[i]
            if currentX < 0 or currentX >= h: continue
            if currentY < 0 or currentY >= w: continue
            if grid[currentX][currentY] != 0: continue

            grid[currentX][currentY]=1
            que.append([currentX,currentY])

    #print('after BFS\n', str(grid))
    ans = 0
    for x in range(h):
        for y in range(w):
            if grid[x][y] != 1: continue
            rx = x%2
            ry = y%2
            if rx == 0 or ry == 0: continue
            if (x == 0 or x == h-1) or (y == 0 or y == w-1):
                print('INF')
                return
            
            ans += (xg[rx+1]-xg[rx])*(yg[ry+1]-yg[ry])
            
    print(ans)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    N, M = map(int, input().split())
    
    mapX = dict()
    mapY = dict()
    lh = list()
    lw = list()
    
    area = N+M
    mapX[0] = 0
    mapY[0] = 0
    for i in range(area):
        if i <= N-1:
            a, b, c = map(int, input().split())
            mapX[a] = 0
            mapX[b] = 0
            mapY[c] = 0
            lh.append({'a':a, 'b':b, 'c':c})
        else:
            c, a, b = map(int, input().split())
            mapY[a] = 0
            mapY[b] = 0
            mapX[c] = 0
            lw.append({'a':a, 'b':b, 'c':c})

    mapXs = dict(sorted(mapX.items()))
    mapYs = dict(sorted(mapY.items()))

    xg = list()
    yg = list()

    for k in mapXs:
        mapXs[k] = len(xg)
        xg.append(k)

    for k in mapYs:
        mapYs[k] = len(yg)
        yg.append(k)

    #print(str(xg), '\n', str(yg))

    h = len(xg)*2
    w = len(yg)*2

    grid = np.zeros((h,w), int)

    for i in range(N):
        a = mapXs[lh[i]['a']]*2
        b = mapXs[lh[i]['b']]*2
        c = mapYs[lh[i]['c']]*2
        for j in range(a,b+1): grid[j][c] = -1

    for i in range(M):
        a = mapYs[lw[i]['a']]*2
        b = mapYs[lw[i]['b']]*2
        c = mapXs[lw[i]['c']]*2
        for j in range(a,b+1): grid[c][j] = -1

    #print('befor BFS\n', str(grid))

    que = dict()
    cdX = mapXs[0]*2
    cdY = mapYs[0]*2

    grid[cdX][cdY] = 1
    que = [[cdX,cdY]]

    dx = [0,-1,0,1]
    dy = [-1,0,1,0]

    while len(que) != 0:
        q = que.pop(0)
        for i in range(4):
            currentX = q[0]+dx[i]
            currentY = q[1]+dy[i]
            if currentX < 0 or currentX >= h: continue
            if currentY < 0 or currentY >= w: continue
            if grid[currentX][currentY] != 0: continue

            grid[currentX][currentY]=1
            que.append([currentX,currentY])

    #print('after BFS\n', str(grid))

    ans = 0
    for x in range(h):
        for y in range(w):
            if grid[x][y] != 1: continue
            if x%2 == 0 or y%2 == 0: continue
            if (x == 0 or x == h-1) or (y == 0 or y == w-1):
                print('INF')
                return
            
            ans += (xg[x%2+1]-xg[x%2])*(yg[y%2+1]-yg[y%2])

    print(ans)

if __name__=='__main__':
    main()