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: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

n, m = LI()
horizontal = []
vertical = []
x_co = {INF, -INF}
y_co = {INF, -INF}
for _ in range(n):
    a, b, c = LI()
    x_co.add(a)
    x_co.add(b)
    y_co.add(c)
    vertical += [(a, b, c)]

for _ in range(m):
    d, e, f = LI()
    x_co.add(d)
    y_co.add(e)
    y_co.add(f)
    horizontal += [(d, e, f)]

x_len = len(x_co) * 2 - 1
y_len = len(y_co) * 2 - 1
x_co = sorted(list(x_co))
y_co = sorted(list(y_co))
x_comp = {val:i for i, val in enumerate(x_co)}
y_comp = {val:i for i, val in enumerate(y_co)}
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(x_co, x_co[1:])):
    x_range[i * 2 + 1] = x2 - x1
    if x1 <= 0 <= x2:
        sx = i * 2 + 1

for i, (y1, y2) in enumerate(zip(y_co, y_co[1:])):
    y_range[i * 2 + 1] = y2 - y1
    if y1 <= 0 <= y2:
        sy = i * 2 + 1


for a, b, c in vertical:
    y = y_comp[c] * 2
    for x in range(x_comp[a] * 2 + 1, x_comp[b] * 2 + 1, 2):
        grid[x][y] = 1

for d, e, f in horizontal:
    x = x_comp[d] * 2
    for y in range(y_comp[e] * 2 + 1, y_comp[f] * 2 + 1, 2):
        grid[x][y] = 1

ans = 0
dq = deque([(sx, sy)])
grid[sx][sy] = 1
diff = ((0, 1), (1, 0), (-1, 0), (0, -1))
ans += x_range[sx] * y_range[sy]
while dq:
    x, y = dq.popleft()
    for dx, dy in diff:
        nx, ny = dx + x, dy + y
        if grid[nx][ny]:
            continue
        nx += dx;
        ny += dy
        if grid[nx][ny]:
            continue
        grid[nx][ny] = 1
        dq.append((nx, ny))
        ans += x_range[nx] * y_range[ny]
        if ans >= INF:
            print("INF")
            exit()

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from heapq import heappush, heappop
import sys
import numpy as np

sys.setrecursionlimit(10**6)
input = sys.stdin.buffer.readline
# INF = sys.maxsize
INF = 10 ** 9 + 1
# INF = float("inf")


def dp(*x):  # debugprint
    print(*x)


try:
    profile
except:
    def profile(f): return f


@profile
def main(N, M, data):
    ABC = data[:3 * N]
    DEF = data[3 * N:]
    A, B, C = [ABC[i::3] for i in range(3)]
    D, E, F = [DEF[i::3] for i in range(3)]

    xticks = np.unique(np.concatenate((E, F, C, np.array([-INF, 0, INF]))))
    yticks = np.unique(np.concatenate((A, B, D, np.array([-INF, 0, INF]))))
    width = xticks[1:] - xticks[:-1]
    height = yticks[1:] - yticks[:-1]

    # compress
    A = np.searchsorted(yticks, A).astype(np.int16)
    B = np.searchsorted(yticks, B).astype(np.int16)
    C = np.searchsorted(xticks, C).astype(np.int16)
    D = np.searchsorted(yticks, D).astype(np.int16)
    E = np.searchsorted(xticks, E).astype(np.int16)
    F = np.searchsorted(xticks, F).astype(np.int16)

    GRAPH_WIDTH = len(xticks)
    GRAPH_HEIGHT = len(yticks)
    NUM_VERTEXES = GRAPH_HEIGHT * GRAPH_WIDTH

    ng_edges = np.zeros((NUM_VERTEXES, 4), dtype=np.uint8)
    direction = (-1, +1, -GRAPH_WIDTH, +GRAPH_WIDTH)

    # vertical lines A,C-B,C
    for i in range(N):
        x = C[i]
        for y in range(A[i], B[i]):
            pos = y * GRAPH_WIDTH + x
            ng_edges[pos, 0] = 1  # left
            ng_edges[pos - 1, 1] = 1  # right
    A = B = C = 0

    # horizontal lines D,E-D,F
    for i in range(M):
        y = D[i]
        for x in range(E[i], F[i]):
            pos = y * GRAPH_WIDTH + x
            ng_edges[pos, 2] = 1
            ng_edges[pos - GRAPH_WIDTH, 3] = 1
    D = E = F = 0

    total_area = 0
    visited = np.zeros(NUM_VERTEXES)

    x = np.searchsorted(xticks, 0)
    y = np.searchsorted(yticks, 0)
    start = y * GRAPH_WIDTH + x
    to_visit = [start]

    while to_visit:
        pos = to_visit.pop()
        y, x = divmod(pos, GRAPH_WIDTH)
        if visited[pos]:
            continue
        if y == 0 or y == GRAPH_HEIGHT - 1 or x == 0 or x == GRAPH_WIDTH - 1:
            print("INF")
            break

        total_area += width[x] * height[y]
        visited[pos] = 1
        # plan to visit neighbors
        for i in range(4):
            if not ng_edges[pos][i]:
                next = pos + direction[i]
                if visited[next]:
                    continue
                to_visit.append(next)
    else:
        print(total_area)


if sys.argv[-1] == 'ONLINE_JUDGE' or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export(
        'main',
        "void(i4, i4, i4[:])"
    )(main)
    # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :]
    cc.compile()
    exit()
else:
    if sys.argv[-1] != '-p':
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import main  # pylint: disable=all

    # read parameter
    import numba
    N, M = map(int, input().split())
    data = np.int32(sys.stdin.read().split())
    main(N, M, data)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from heapq import heappush, heappop
import sys
import numpy as np

sys.setrecursionlimit(10**6)
input = sys.stdin.buffer.readline
# INF = sys.maxsize
INF = 10 ** 9 + 1
# INF = float("inf")


def dp(*x):  # debugprint
    print(*x)


try:
    profile
except:
    def profile(f): return f


@profile
def main(N, M, data):
    ABC = data[:3 * N]
    DEF = data[3 * N:]
    A, B, C = [ABC[i::3] for i in range(3)]
    D, E, F = [DEF[i::3] for i in range(3)]

    xticks = np.unique(np.concatenate((E, F, C, np.array([-INF, 0, INF]))))
    yticks = np.unique(np.concatenate((A, B, D, np.array([-INF, 0, INF]))))
    width = xticks[1:] - xticks[:-1]
    height = yticks[1:] - yticks[:-1]

    # compress
    A = np.searchsorted(yticks, A).astype(np.int16)
    B = np.searchsorted(yticks, B).astype(np.int16)
    C = np.searchsorted(xticks, C).astype(np.int16)
    D = np.searchsorted(yticks, D).astype(np.int16)
    E = np.searchsorted(xticks, E).astype(np.int16)
    F = np.searchsorted(xticks, F).astype(np.int16)

    GRAPH_WIDTH = len(xticks)
    GRAPH_HEIGHT = len(yticks)
    NUM_VERTEXES = GRAPH_HEIGHT * GRAPH_WIDTH

#    ng_egdes = np.zeros((NUM_VERTEXES, 4), dtype=np.uint8)
    if 0:
        direction = (-1, +1, -GRAPH_WIDTH, +GRAPH_WIDTH)

        ng_edges = np.zeros(NUM_VERTEXES, dtype=np.uint8)

        # dp(": NUM_VERTEX", NUM_VERTEXES)
        # dp(": N,M", N, M)
        # vertical lines A,C-B,C
        for i in range(N):
            x = C[i]
            for y in range(A[i], B[i]):
                pos = y * GRAPH_WIDTH + x
                # ng_egdes[pos, 0] = 1  # left
                # ng_egdes[pos - 1, 1] = 1  # right
                ng_edges[pos] |= 0b0001  # left
                ng_edges[pos - 1] |= 0b0010  # right
        A = B = C = 0

        # horizontal lines D,E-D,F
        for i in range(M):
            y = D[i]
            for x in range(E[i], F[i]):
                pos = y * GRAPH_WIDTH + x
                ng_edges[pos] |= 0b0100
                ng_edges[pos - GRAPH_WIDTH] |= 0b1000
        D = E = F = 0
    else:
        direction = (1, -1, GRAPH_WIDTH, -GRAPH_WIDTH)

        def set_ng(A, B, C, D, E, F, H, W):
            ng = np.zeros((H * W, 4), np.bool_)

            for i in range(len(A)):
                for x in range(A[i], B[i]):
                    v = x * W + C[i]
                    ng[v][1] = 1
                    ng[v - 1][0] = 1

            for i in range(len(D)):
                for y in range(E[i], F[i]):
                    v = D[i] * W + y
                    ng[v][3] = 1
                    ng[v - W][2] = 1
            return ng

        ng_edges = set_ng(A, B, C, D, E, F, GRAPH_HEIGHT, GRAPH_WIDTH)
    total_area = 0
    visited = np.zeros(NUM_VERTEXES)
    #visited = set()

    x = np.searchsorted(xticks, 0)
    y = np.searchsorted(yticks, 0)
    start = y * GRAPH_WIDTH + x
    to_visit = [start]

    stack = np.empty(NUM_VERTEXES, np.int32)
    p = 0

    def push(x):
        nonlocal p
        stack[p] = x
        p += 1

    def pop():
        nonlocal p
        p -= 1
        return stack[p]

    push(start)
    MASK = np.array([0b0001, 0b0010, 0b0100, 0b1000], dtype=np.uint8)
    # while to_visit:
    while p:
        #pos = to_visit.pop()
        pos = pop()
        y, x = divmod(pos, GRAPH_WIDTH)
        if visited[pos]:
            continue
        if y == 0 or y == GRAPH_HEIGHT - 1 or x == 0 or x == GRAPH_WIDTH - 1:
            print("INF")
            break

        total_area += width[x] * height[y]
        # visited.add(pos)
        visited[pos] = 1
        # plan to visit neighbors
        for i in range(4):
            # if not (ng_edges[pos] & MASK[i]):
            if not ng_edges[pos][i]:
                next = pos + direction[i]
                if visited[next]:
                    continue
                # to_visit.append(next)
                push(next)
    else:
        print(total_area)


if sys.argv[-1] == 'ONLINE_JUDGE' or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export(
        'main',
        "void(i4, i4, i4[:])"
    )(main)
    # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :]
    cc.compile()
    exit()
else:
    if sys.argv[-1] != '-p':
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import main  # pylint: disable=all

    # read parameter
    import numba
    N, M = map(int, input().split())
    data = np.int32(sys.stdin.read().split())
    main(N, M, data)

Submission status: Accepted

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


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
dx = [+1, 0, -1, 0]
dy = [0, +1, 0, -1]

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)
grid = [[False] * (ny + 1) for _ in range(nx + 1)]
q = deque([(x_zipped[0], y_zipped[0])])
while q:
    x0, y0 = q.popleft()
    if grid[x0][y0]:
        continue
    grid[x0][y0] = True
    for k in range(4):
        x1 = x0 + dx[k]
        y1 = y0 + dy[k]
        if x1 < 0 or x1 >= nx or y1 < 0 or y1 >= ny:
            continue
        if grid[x1][y1]:
            continue

        if k == 0 and h_ng[x1][y1] == 0:
            # move x-dir
            q.append((x1, y1))
        elif k == 2 and h_ng[x0][y0] == 0:
            # move x-dir
            q.append((x1, y1))
        elif k == 1 and v_ng[x1][y1] == 0:
            # move y-dir
            q.append((x1, y1))
        elif k == 3 and v_ng[x0][y0] == 0:
            # move y-dir
            q.append((x1, y1))

# print(*grid, sep='\n')
if grid[0][0]:
    ans = 'INF'
else:
    ans = 0
    for x in range(nx):
        for y in range(ny):
            if grid[x][y]:
                ans += (x_unzipped[x + 1] -
                        x_unzipped[x]) * (y_unzipped[y + 1] - y_unzipped[y])

print(ans)

Submission status: Accepted

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] * ny + 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: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from bisect import bisect_left, bisect_right

    INF = 10**18

    n, m = map(int, input().split())
    a = [list(map(int, input().split())) for i in range(n)]
    b = [list(map(int, input().split())) for i in range(m)]

    X = {-INF, INF}
    Y = {-INF, INF}
    for i in a:
        Y.add(i[2])
    for i in b:
        X.add(i[0])

    X = list(sorted(X))
    Y = list(sorted(Y))
    n = len(X) - 1
    m = len(Y) - 1
    wallx = [[False] * m for i in range(n)]
    wally = [[False] * m for i in range(n)]

    for x1, x2, y1 in a:
        x1 = bisect_left(X, x1)
        y1 = bisect_left(Y, y1)
        x2 = bisect_right(X, x2) - 1
        for i in range(x1, x2):
            wally[i][y1] = True

    for x1, y1, y2 in b:
        x1 = bisect_left(X, x1)
        y1 = bisect_left(Y, y1)
        y2 = bisect_right(Y, y2) - 1
        for i in range(y1, y2):
            wallx[x1][i] = True

    cow = [[False] * m for i in range(n)]
    cx = bisect_right(X, 0) - 1
    cy = bisect_right(Y, 0) - 1
    cow[cx][cy] = True
    q = [(cx, cy)]
    ans = 0

    while q:
        x, y = q.pop()
        if not x or not y:
            print("INF")
            sys.exit()
        ans += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        if x and not wallx[x][y] and not cow[x - 1][y]:
            cow[x - 1][y] = True
            q.append((x - 1, y))
        if y and not wally[x][y] and not cow[x][y - 1]:
            cow[x][y - 1] = True
            q.append((x, y - 1))
        if x + 1 < n and not wallx[x + 1][y] and not cow[x + 1][y]:
            cow[x + 1][y] = True
            q.append((x + 1, y))
        if y + 1 < m and not wally[x][y + 1] and not cow[x][y + 1]:
            cow[x][y + 1] = True
            q.append((x, y + 1))

    print(ans)

main()

Submission status: Accepted

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))
Y = sorted(set(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)
    d = TY(d)
    e = bisect_left(X, e)
    f = bisect_right(X, f)-1
    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)
    c = TX(c)
    a = bisect_left(Y, a)
    b = bisect_right(Y, b)-1
    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 not (0<=i<H and 0<=j<W):
            return -1
        if v[i,j]:
            continue
        v[i,j] = True
        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: Accepted

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

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 = sorted(list(xseen))
xcomp = {val:i for i, val in enumerate(xseen)}
yseen = sorted(list(yseen))
ycomp = {val:i for i, val in enumerate(yseen)}

field = [[0] * 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 + 1, xcomp[b] * 2 + 1, 2):
        field[x][y] = 1 

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

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

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

dq = deque([(sx, sy)])
field[sx][sy] = 1 
ans += x_range[sx] * y_range[sy]
while dq:
    x, y = dq.popleft()
    for dx, dy in diff:
        nx, ny = dx + x, dy + y
        if field[nx][ny]:
            continue
        nx += dx; ny += dy
        if field[nx][ny]:
            continue
        field[nx][ny] = 1 
        dq.append((nx, ny))
        ans += x_range[nx] * y_range[ny] 
        if ans >= INF:
            print("INF")
            exit()

print(ans)

Submission status: Accepted

Solution code:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す．
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,m = map(int, readline().split())
yoko = [list(map(int, readline().split())) for _ in range(n)]
tate = [list(map(int, readline().split())) for _ in range(m)]

INF = 10**9+5
xcoord = [-INF,0,INF]
ycoord = [-INF,0,INF]

for a,b,c in yoko: # (a,c) to (b,c)
    xcoord.append(a)
    xcoord.append(b)
    ycoord.append(c)
for d,e,f in tate: # (d,e) to (d,f)
    ycoord.append(e)
    ycoord.append(f)
    xcoord.append(d)

xcoord = list(sorted(set(xcoord)))
ycoord = list(sorted(set(ycoord)))

zaatu_x = {a:i for i,a in enumerate(xcoord)}
zaatu_y = {a:i for i,a in enumerate(ycoord)}

P = len(xcoord)
Q = len(ycoord)
QQ=Q-1


# (i,j) から右 or 上 に行けるか？
ok_up = [[0]*P for _ in range(Q)]
ok_ri = [[0]*Q for _ in range(P)]

for a,b,c in yoko: # (a,c) to (b,c)
    xa = zaatu_x[a]
    xb = zaatu_x[b]
    yc = zaatu_y[c]
    ok_up[yc-1][xa] += 1
    ok_up[yc-1][xb] -= 1


for d,e,f in tate: # (d,e) to (d,f)
    ye = zaatu_y[e]
    yf = zaatu_y[f]
    xd = zaatu_x[d]
    ok_ri[xd-1][ye] += 1
    ok_ri[xd-1][yf] -= 1

#print(ok_up)
#print(ok_ri)
"""
from itertools import accumulate
for i in range(Q):
    ok_up[i] = list(accumulate(ok_up[i]))
for j in range(P):
    ok_ri[j] = list(accumulate(ok_ri[j]))
"""
for i in range(Q):
    for j in range(P-1):
        ok_up[i][j+1] += ok_up[i][j]

for j in range(P):
    for i in range(Q-1):
        ok_ri[j][i+1] += ok_ri[j][i]

#print(*[i for i in ok_ri],sep="\n")

UF = UnionFind((P-1)*QQ)
for i in range(P-1):
    for j in range(Q-2):
        v = i*QQ+j
        if ok_up[j][i]==0: UF.merge(v,v+1)
for i in range(P-2):
    for j in range(Q-1):
        v = i*QQ+j
        if ok_ri[i][j]==0: UF.merge(v,v+QQ)
        
x0 = zaatu_x[0]
y0 = zaatu_y[0]

#print([UF.root(i) for i in range((P-1)*(Q-1))])
r = UF.root(x0*QQ+y0) 
if UF.root(0) == r:
    print("INF")
else:
    ans = 0
    for i in range(P-1):
        for j in range(Q-1):
            if UF.root(i*QQ+j) == r:
                ans += (xcoord[i+1]-xcoord[i])*(ycoord[j+1]-ycoord[j]) 
    print(ans)

#print(xcoord)
#print(ycoord)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float("inf")


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

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def compress(X, Y):
    dx = list(sorted(set(X)))
    dx_inv = {x: i for i, x in enumerate(dx)}
    dy = list(sorted(set(Y)))
    dy_inv = {y: i for i, y in enumerate(dy)}
    return (dx, dy), (dx_inv, dy_inv)


def main():
    # 1. 座圧します
    # 2. 座標ごとに線分が乗っているかどうかを示す配列を作ります
    # 3. 面ごとに同じ領域内かどうか、グルーピングします
    # 4. 牛と同じグループの面積を数え上げます

    # 入力
    N, M = map(int, readline().split())
    A, B, C = [0]*N, [0]*N, [0]*N
    for i in range(N):
        A[i], B[i], C[i] = map(int, readline().split())
    D, E, F = [0]*M, [0]*M, [0]*M
    for i in range(M):
        D[i], E[i], F[i] = map(int, readline().split())

    # 1. 座圧
    X = [-INF, 0, INF] + A + B + D
    Y = [-INF, 0, INF] + C + E + F
    (mx, my), (invx, invy) = compress(X, Y)
    H = len(my)
    W = len(mx)

    # 2. 線分ごとに線分が乗っているかどうかを示す配列を作ります(imos法)
    # segx[i][j]は座標(i, j)と(i+1, j)をつなぐ線分
    # segy[i][j]は座標(i, j)と(i, j+1)をつなぐ線分
    segx = [[0]*H for _ in range(W)]
    segy = [[0]*H for _ in range(W)]

    for a, b, c in zip(A, B, C):
        segx[invx[a]][invy[c]-1] += 1
        segx[invx[b]][invy[c]-1] -= 1
    for i in range(W-1):
        for j in range(H):
            segx[i+1][j] += segx[i][j]

    for d, e, f in zip(D, E, F):
        segy[invx[d]-1][invy[e]] += 1
        segy[invx[d]-1][invy[f]] -= 1
    for i in range(W):
        for j in range(H-1):
            segy[i][j+1] += segy[i][j]

    # 3. 面ごとに同じ領域内かどうか、グルーピングします
    # UnionFindで検査していく
    uf = UnionFind(W*H)

    # segx[i][j]は座標(i, j)と(i+1, j)をつなぐ線分であり
    # 面[i][j-1]と面[i][j]の境界にあたる
    # segy[i][j]は座標(i, j)と(i, j+1)をつなぐ線分であり
    # 面[i-1][j]と面[i][j]の境界にあたる
    for i in range(W):
        for j in range(H):
            if i+1 < W and segy[i][j] == 0:
                uf.union(i+j*W, i+1+j*W)
            if j+1 < H and segx[i][j] == 0:
                uf.union(i+(j+1)*W, i+j*W)

    # 4. 牛と同じグループの面積を数え上げます
    # 無限の彼方と同じグル-プなら、INF
    ushi = invx[0]+invy[0]*W
    if uf.same(ushi, 0):
        print("INF")
        return

    area = 0
    ushi = uf.find(ushi)
    for mem in range(uf.n):
        if uf.find(mem) == ushi:
            j, i = divmod(mem, W)
            area += (mx[i+1]-mx[i])*(my[j+1]-my[j])

    print(area)
    return


if __name__ == '__main__':
    main()