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.

Solution (Python):
def main():
    import sys
    input = lambda : sys.stdin.readline().rstrip()
    sys.setrecursionlimit(max(1000, 10**9))
    write = lambda x: sys.stdout.write(x+"\n")


    n,m = map(int, input().split())

    lx = [None] * n
    ly = [None] * m
    sx = set([-10**10, 0, 10**10])
    sy = set([-10**10, 0, 10**10])
    ix = 0
    iy = 0
    for i in range(n):
        t = tuple(map(int, input().split()))
        lx[i] = t
        sx.add(t[0])
        sx.add(t[1])
        sy.add(t[2])

    for i in range(m):
        t = tuple(map(int, input().split()))
        ly[i] = t
        sx.add(t[0])
        sy.add(t[1])
        sy.add(t[2])

    xs = list(sx)
    ys = list(sy)
    xs.sort(); ys.sort()

    wx = len(xs)*2
    wy = len(ys)*2
    bs = [[1]*wy for _ in range(wx)]
    import bisect
    for i, (x0, x1, y) in enumerate(lx):
        ix0 = bisect.bisect_left(xs, x0)*2
        ix1 = bisect.bisect_left(xs, x1)*2
        iy = bisect.bisect_left(ys, y)*2
        for ii in range(ix0+1, ix1, 2):
            bs[ii][iy] = -1
    for i, (x, y0, y1) in enumerate(ly):
        ix = bisect.bisect_left(xs, x)*2
        iy0 = bisect.bisect_left(ys, y0)*2
        iy1 = bisect.bisect_left(ys, y1)*2
        for ii in range(iy0+1, iy1, 2):
            bs[ix][ii] = -1

    xx = bisect.bisect_left(xs, 0)*2+1
    yy = bisect.bisect_left(ys, 0)*2+1
    s = [(xx,yy)]
    dx = [-1,0,1,0]
    dy = [0,-1,0,1]
    ans = 0
    end = False
    while s:
        ix,iy = s.pop()
        for x, y in zip(dx,dy):
            ixx = ix + x
            iyy = iy + y
            if bs[ixx][iyy]!=1:
                continue
            ixx += x
            iyy += y
            if bs[ixx][iyy]!=1:
                continue
            if ixx<0 or ixx>=wx-1 or iyy<0 or iyy>=wy-1:
                print("INF")
                s = []
                end = True
                break
            bs[ixx][iyy] = 2
            ans += (xs[ixx//2+1] - xs[ixx//2]) * (ys[iyy//2+1] - ys[iyy//2])
            s.append((ixx, iyy))

    if not end:
        print(ans)
main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = map(int, input().split())

lx = [None] * n
ly = [None] * m
sx = set([-10**10, 0, 10**10])
sy = set([-10**10, 0, 10**10])
ix = 0
iy = 0
for i in range(n):
    t = tuple(map(int, input().split()))
    lx[i] = t
    sx.add(t[0])
    sx.add(t[1])
    sy.add(t[2])
        
for i in range(m):
    t = tuple(map(int, input().split()))
    ly[i] = t
    sx.add(t[0])
    sy.add(t[1])
    sy.add(t[2])
        
xs = list(sx)
ys = list(sy)
xs.sort(); ys.sort()

wx = len(xs)*2
wy = len(ys)*2
bs = [[1]*wy for _ in range(wx)]
import bisect
for i, (x0, x1, y) in enumerate(lx):
    ix0 = bisect.bisect_left(xs, x0)*2
    ix1 = bisect.bisect_left(xs, x1)*2
    iy = bisect.bisect_left(ys, y)*2
    for ii in range(ix0, ix1):
        bs[ii][iy] = -1
for i, (x, y0, y1) in enumerate(ly):
    ix = bisect.bisect_left(xs, x)*2
    iy0 = bisect.bisect_left(ys, y0)*2
    iy1 = bisect.bisect_left(ys, y1)*2
    for ii in range(iy0, iy1):
        bs[ix][ii] = -1

xx = bisect.bisect_left(xs, 0)*2+1
yy = bisect.bisect_left(ys, 0)*2+1
s = [(xx,yy)]
dx = [-1,0,1,0]
dy = [0,-1,0,1]
ans = 0
end = False
while s:
    ix,iy = s.pop()
    for x, y in zip(dx,dy):
        ixx = ix + x
        iyy = iy + y
        if bs[ixx][iyy]!=1:
            continue
        ixx += x
        iyy += y
        if bs[ixx][iyy]!=1:
            continue
        if ixx<0 or ixx>=wx-1 or iyy<0 or iyy>=wy-1:
            print("INF")
            s = []
            end = True
            break
        bs[ixx][iyy] = 2
        ans += (xs[ixx//2+1] - xs[ixx//2]) * (ys[iyy//2+1] - ys[iyy//2])
        s.append((ixx, iyy))
            
if not end:
    print(ans)

Solution (Python):
from bisect import bisect_left as bl
from collections import deque
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
n,m = map(int,input().split())
H = set([float("INF"),-float("INF")])
V = set([float("INF"),-float("INF")])
nl = []
ml = []
for i in range(n):
    a,b,c = map(int,input().split())
    H |= set([a,b])
    V.add(c)
    nl.append([a,b,c])
for i in range(m):
    c,a,b = map(int,input().split())
    V |= set([a,b])
    H.add(c)
    ml.append([a,b,c])
def compress(L):
    L2 = list(set(L))
    L2.sort()
    C = {v : k for k, v in enumerate(L2)}
    return L2, C
H ,dx = compress(H)
V, dy = compress(V)

h = len(H)+2
w = len(V)+2
d = [[0]*3001 for i in range(3001)]
checkh = [[0]*3001 for i in range(3001)]
checkv = [[0]*3001 for i in range(3001)]

for a,b,c in nl:
    s = dx[a]
    g = dx[b]
    t = dy[c]
    
    for x in range(s,g):
        checkh[x][t] = 1

for a,b,c in ml:
    s = dy[a]
    g = dy[b]
    t = dx[c]
    
    for y in range(s,g):
        checkv[t][y] = 1


def bfs(x,y):
    ans = 0
    sx = bl(H,0)-1
    sy = bl(V,0)-1
    q = deque([])
    q.append((sx,sy))
    d[sx][sy] = 1
    while q:
        h,v = q.popleft()
        ans += (H[h+1]-H[h])*(V[v+1]-V[v])
        if h != 0 and checkv[h][v] == 0 and d[h-1][v] == 0:
            d[h-1][v] = 1
            q.append((h-1,v))
        if v != 0 and checkh[h][v] == 0 and d[h][v-1] == 0:
            d[h][v-1] = 1
            q.append((h,v-1))
        if h != len(H)-2 and checkv[h+1][v] == 0 and d[h+1][v] == 0:
            d[h+1][v] = 1
            q.append((h+1,v))
        if v != len(V)-2 and checkh[h][v+1] == 0 and d[h][v+1] == 0:
            d[h][v+1] = 1
            q.append((h,v+1))
    print("INF" if ans ==float("INF") else ans)

bfs(0,0)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = map(int, input().split())

lx = [None] * n
ly = [None] * m
sx = set([-10**10, 0, 10**10])
sy = set([-10**10, 0, 10**10])
ix = 0
iy = 0
for i in range(n):
    t = tuple(map(int, input().split()))
    lx[i] = t
    sx.add(t[0])
    sx.add(t[1])
    sy.add(t[2])
        
for i in range(m):
    t = tuple(map(int, input().split()))
    ly[i] = t
    sx.add(t[0])
    sy.add(t[1])
    sy.add(t[2])
        
xs = list(sx)
ys = list(sy)
xs.sort(); ys.sort()

wx = len(xs)*2
wy = len(ys)*2
bs = [[1]*wy for _ in range(wx)]
for i, (x0, x1, y) in enumerate(lx):
    ix0 = xs.index(x0)*2
    ix1 = xs.index(x1)*2
    iy = ys.index(y)*2
    for ii in range(ix0+1, ix1, 2):
        bs[ii][iy] = -1
for i, (x, y0, y1) in enumerate(ly):
    ix = xs.index(x)*2
    iy0 = ys.index(y0)*2
    iy1 = ys.index(y1)*2
    for ii in range(iy0+1, iy1, 2):
        bs[ix][ii] = -1

xx = xs.index(0)*2+1
yy = ys.index(0)*2+1
s = [(xx,yy)]
dx = [-1,0,1,0]
dy = [0,-1,0,1]
ans = 0
end = False
while s:
    ix,iy = s.pop()
    for x, y in zip(dx,dy):
        ixx = ix + x
        iyy = iy + y
        if bs[ixx][iyy]!=1:
            continue
        ixx += x
        iyy += y
        if bs[ixx][iyy]!=1:
            continue
        if ixx<0 or ixx>=wx-1 or iyy<0 or iyy>=wy-1:
            print("INF")
            s = []
            end = True
            break
        bs[ixx][iyy] = 2
        ans += (xs[ixx//2+1] - xs[ixx//2]) * (ys[iyy//2+1] - ys[iyy//2])
        s.append((ixx, iyy))
            
if not end:
    print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,m = map(int, input().split())

lx = [None] * n
ly = [None] * m
sx = set([-10**10, 0, 10**10])
sy = set([-10**10, 0, 10**10])
ix = 0
iy = 0
for i in range(n):
    t = tuple(map(int, input().split()))
    lx[i] = t
    sx.add(t[0])
    sx.add(t[1])
    sy.add(t[2])
        
for i in range(m):
    t = tuple(map(int, input().split()))
    ly[i] = t
    sx.add(t[0])
    sy.add(t[1])
    sy.add(t[2])
        
xs = list(sx)
ys = list(sy)
xs.sort(); ys.sort()

wx = len(xs)*2
wy = len(ys)*2
bs = [[1]*wy for _ in range(wx)]
for i, (x0, x1, y) in enumerate(lx):
    ix0 = xs.index(x0)*2
    ix1 = xs.index(x1)*2
    iy = ys.index(y)*2
    for ii in range(ix0, ix1):
        bs[ii][iy] = -1
for i, (x, y0, y1) in enumerate(ly):
    ix = xs.index(x)*2
    iy0 = ys.index(y0)*2
    iy1 = ys.index(y1)*2
    for ii in range(iy0, iy1):
        bs[ix][ii] = -1

xx = xs.index(0)*2+1
yy = ys.index(0)*2+1
s = [(xx,yy)]
dx = [-1,0,1,0]
dy = [0,-1,0,1]
ans = 0
end = False
while s:
    ix,iy = s.pop()
    for x, y in zip(dx,dy):
        ixx = ix + x
        iyy = iy + y
        if bs[ixx][iyy]!=1:
            continue
        ixx += x
        iyy += y
        if bs[ixx][iyy]!=1:
            continue
        if ixx<0 or ixx>=wx-1 or iyy<0 or iyy>=wy-1:
            print("INF")
            s = []
            end = True
            break
        bs[ixx][iyy] = 2
        ans += (xs[ixx//2+1] - xs[ixx//2]) * (ys[iyy//2+1] - ys[iyy//2])
        s.append((ixx, iyy))
            
if not end:
    print(ans)

Solution (Python):
def f_single_dot():
    
    import sys
    from collections import deque
    input = sys.stdin.readline
    N, M = [int(i) for i in input().split()]

    x_set, y_set = set([0]), set([0])  

    vertical_lines = []
    for _ in range(N):
        a, b, c = [int(i) for i in input().split()]
        vertical_lines.append((a, b, c))
        x_set |= {a, b}
        y_set |= {c}

    horizontal_lines = []
    for _ in range(M):
        d, e, f = [int(i) for i in input().split()]
        horizontal_lines.append((d, e, f))
        x_set |= {d}
        y_set |= {e, f}

    x_set = sorted(x_set)
    y_set = sorted(y_set)

    
    x_encoded = {n: i * 2 for i, n in enumerate(x_set)}
    y_encoded = {n: i * 2 for i, n in enumerate(y_set)}
    
    x_length = [x_set[i // 2 + 1] - x_set[i // 2] if i %
                2 else 0 for i in range(len(x_set) * 2 - 1)]
    y_length = [y_set[i // 2 + 1] - y_set[i // 2] if i %
                2 else 0 for i in range(len(y_set) * 2 - 1)]

    
    x_size, y_size = 2 * len(x_set) - 1, 2 * len(y_set) - 1
    grid = [[0] * y_size for _ in range(x_size)]
    for a, b, c in vertical_lines:
        a, b, c = x_encoded[a], x_encoded[b], y_encoded[c]
        for x in range(a, b + 1):
            grid[x][c] = 1
    for d, e, f in horizontal_lines:
        d, e, f = x_encoded[d], y_encoded[e], y_encoded[f]
        for y in range(e, f + 1):
            grid[d][y] = 1

    queue = deque()
    is_visited = [[0] * (y_size + 1) for _ in range(x_size + 1)]

    
    for dx, dy in ((-1, -1), (-1, 1), (1, 1), (1, -1)):
        nx, ny = x_encoded[0] + dx, y_encoded[0] + dy
        if 0 <= nx < x_size and 0 <= ny < y_size:
            queue.append((nx, ny))
            is_visited[nx][ny] = 1

    diff = ((-1, 0), (1, 0), (0, -1), (0, 1))
    ans = 0
    while queue:
        x, y = queue.pop()
        if (x % 2) & (y % 2):
            ans += x_length[x] * y_length[y]
        
        for dx, dy in diff:
            nx, ny = x + dx, y + dy
            nnx, nny = nx + dx, ny + dy
            if grid[nx][ny] == 0:
                if not ((0 <= nnx < x_size) and (0 <= nny < y_size)):
                    return 'INF'
                if not is_visited[nnx][nny]:
                    queue.appendleft((nnx, nny))
                    is_visited[nnx][nny] = 1
    return ans

print(f_single_dot())

Solution (Python):
N,M = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*N ]
DEF = [list(map(int,input().split())) for _ in [0]*M ]

X = [0]*3003
Y = [0]*3003
INF = 10**9 + 10
X[-1] = -INF
Y[-1] = -INF
X[-2] = INF
Y[-2] = INF
i = 0
j = 0
for a,b,c in ABC:
    X[i] = a
    X[i+1] = b
    Y[j] = c
    i += 2
    j += 1
for d,e,f in DEF:
    X[i] = d
    Y[j] = e
    Y[j+1] = f
    i += 1
    j += 2
X = sorted(set(X))
Y = sorted(set(Y))
Xdict = {x:i for i,x in enumerate(X)}
Ydict = {y:i for i,y in enumerate(Y)}

nx = len(X)
ny = len(Y)
nxy = nx * ny

E = [[15]*nx for _ in [0]*ny]

E[0] = [7]*nx
E[-1] = [13]*nx
for i in range(ny):
    E[i][0] -= 4
    E[i][-1] -= 1

for a,b,c in ABC:
    y1 = Ydict[c]
    y0 = y1-1
    for x in range(Xdict[a],Xdict[b]):
        E[y0][x] &= 15-2
        E[y1][x] &= 15-8

for d,e,f in DEF:
    x1 = Xdict[d]
    x0 = x1-1
    for y in range(Ydict[e],Ydict[f]):
        E[y][x0] &= 15-1
        E[y][x1] &= 15-4

dX = [x2-x for x,x2 in zip(X,X[1:])]
dY = [y2-y for y,y2 in zip(Y,Y[1:])]
burst = False
x0 = Xdict[0]
y0 = Ydict[0]
ans = dX[x0]*dY[y0]
done = [[False]*nx for _ in [0]*ny]
done[y0][x0] = True
q = [(x0,y0)]
dxy = [(1,0),(0,1),(-1,0),(0,-1)]
while q:
    qq = []
    for x,y in q:
        e = E[y][x]
        for i in range(4):
            if (e>>i)&1 == 0 : continue
            dx,dy = dxy[i]
            jx = x+dx
            jy = y+dy
            if done[jy][jx]:continue
            done[jy][jx] = True

            if jx==0 or jx==nx-1 or jy==0 or jy==ny-1:
                burst = True
                ans = "INF"
                qq = []
                break
            ans += dX[jx]*dY[jy]
            qq.append((jx,jy))
        if burst:break
    q = qq

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,m = map(int, readline().split())
data = list(map(int, read().split()))

point = [{0,10**9,-1*10**9} for _ in range(2)]
dic_en = [{} for _ in range(2)]
dic_de = [{} for _ in range(2)]

it = iter(data[:3*n])
for a,b,c in zip(it,it,it):
    point[0].add(a)
    point[0].add(b)
    point[1].add(c)

it = iter(data[3*n:])
for d,e,f in zip(it,it,it):
    point[0].add(d)
    point[1].add(e)
    point[1].add(f)

for i,p in enumerate(point):
    p = sorted(list(p))
    for after,before in enumerate(p):
        dic_en[i][before] = after
        dic_de[i][after] = before

len_x = len(point[0])
len_y = len(point[1])

wall_x = [[0] * (len_y-1) for _ in range(len_x)]
wall_y = [[0] * (len_y) for _ in range(len_x-1)]

it = iter(data[:3*n])
for a,b,c in zip(it,it,it):
    a,b,c = dic_en[0][a],dic_en[0][b],dic_en[1][c]
    wall_y[a][c] += 1
    if(b != len_x-1):
        wall_y[b][c] -= 1

it = iter(data[3*n:])
for d,e,f in zip(it,it,it):
    d,e,f = dic_en[0][d],dic_en[1][e],dic_en[1][f]
    wall_x[d][e] += 1
    if(f != len_y-1):
        wall_x[d][f] -= 1

for i in range(len_x):
    for j in range(1,len_y-1):
        wall_x[i][j] += wall_x[i][j-1]

for i in range(1,len_x-1):
    for j in range(len_y):
        wall_y[i][j] += wall_y[i-1][j]

start = (dic_en[0][0], dic_en[1][0])
area = [[0] * (len_y-1) for _ in range(len_x-1)]

ans = 0
stack = [start]
i,j = start
area[i][j] = 1
while(stack):
    i,j = stack.pop()
    ans += (dic_de[0][i+1] - dic_de[0][i])*(dic_de[1][j+1] - dic_de[1][j])
    
    if(wall_x[i+1][j]==0):
        if(i==len_x-2):
            print('INF')
            exit()
        if(area[i+1][j] == 0):
            stack.append((i+1,j))
            area[i+1][j] = 1
    
    if(wall_x[i][j]==0):
        if(i==0):
            print('INF')
            exit()
        if(area[i-1][j] == 0):
            stack.append((i-1,j))
            area[i-1][j] = 1
    
    if(wall_y[i][j+1]==0):
        if(j==len_y-2):
            print('INF')
            exit()
        if(area[i][j+1] == 0):
            stack.append((i,j+1))
            area[i][j+1] = 1

    
    if(wall_y[i][j]==0):
        if(j==0):
            print('INF')
            exit()
        if(area[i][j-1] == 0):
            stack.append((i,j-1))
            area[i][j-1] = 1

print(ans)

Solution (Python):
import bisect
import sys

lower_bound = bisect.bisect_left
upper_bound = bisect.bisect_right
INF = 0x3fffffff

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 = lower_bound(X, x1)
    y1 = lower_bound(Y, y1)
    x2 = upper_bound(X, x2) - 1
    for i in range(x1, x2):
        wally[i][y1] = True

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

cow = [[False] * m for i in range(n)]
cx = upper_bound(X, 0) - 1
cy = upper_bound(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)

Solution (Python):
import bisect
import sys

lower_bound = bisect.bisect_left
upper_bound = bisect.bisect_right
INF = 0x3fffffff

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 = lower_bound(X, x1)
    y1 = lower_bound(Y, y1)
    x2 = upper_bound(X, x2) - 1
    for i in range(x1, x2):
        wally[i][y1] = True

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

cow = [[False] * m for i in range(n)]
cx = upper_bound(X, 0) - 1
cy = upper_bound(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)

Solution (Python):
def f_single_dot():
    
    import sys
    from collections import deque
    input = sys.stdin.readline
    N, M = [int(i) for i in input().split()]

    x_set, y_set = set([0]), set([0])  

    vertical_lines = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        vertical_lines.append((a, b, c))
        x_set |= {a, b}
        y_set |= {c}

    horizontal_lines = []
    for _ in range(M):
        d, e, f = map(int, input().split())
        horizontal_lines.append((d, e, f))
        x_set |= {d}
        y_set |= {e, f}

    x_set = sorted(x_set)
    y_set = sorted(y_set)

    
    x_encoded = {n: i * 2 for i, n in enumerate(x_set)}
    y_encoded = {n: i * 2 for i, n in enumerate(y_set)}
    
    x_length = [x_set[i // 2 + 1] - x_set[i // 2] if i %
                2 else 0 for i in range(len(x_set) * 2 - 1)]
    y_length = [y_set[i // 2 + 1] - y_set[i // 2] if i %
                2 else 0 for i in range(len(y_set) * 2 - 1)]

    
    x_size, y_size = 2 * len(x_set) - 1, 2 * len(y_set) - 1
    grid = [[0] * y_size for _ in range(x_size)]
    for a, b, c in vertical_lines:
        a, b, c = x_encoded[a], x_encoded[b], y_encoded[c]
        for x in range(a, b + 1):
            grid[x][c] = 1
    for d, e, f in horizontal_lines:
        d, e, f = x_encoded[d], y_encoded[e], y_encoded[f]
        for y in range(e, f + 1):
            grid[d][y] = 1

    queue = deque()
    is_visited = [[0] * (y_size + 1) for _ in range(x_size + 1)]
    dy = [1, 1, -1, -1]
    dx = [1, -1, 1, -1]
    for k in range(4):
        nnx, nny = x_encoded[0] + dx[k], y_encoded[0] + dy[k]
        if 1 <= nnx < len(x_set) * 2 - 1 and 1 <= nny < len(y_set) * 2 - 1:
            queue.append((nnx, nny))
            is_visited[nnx][nny] = 1
    ans = 0
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    while queue:
        x, y = queue.pop()
        if (x % 2) & (y % 2):
            ans += x_length[x] * y_length[y]
        for k in range(4):
            nx, ny = x + dx[k], y + dy[k]
            nnx, nny = nx + dx[k], ny + dy[k]
            if grid[nx][ny] == 0:
                if nnx < 0 or nnx >= len(x_set) * 2 - 1 or nny < 0 or nny >= len(y_set) * 2 - 1:
                    return 'INF'
                if not is_visited[nnx][nny]:
                    queue.appendleft((nnx, nny))
                    is_visited[nnx][nny] = 1
    return ans

print(f_single_dot())

Solution (Python):
def f_single_dot():
    
    import sys
    from collections import deque
    input = sys.stdin.readline
    N, M = [int(i) for i in input().split()]

    x_set, y_set = set([0]), set([0])  

    vertical_lines = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        vertical_lines.append((a, b, c))
        x_set |= {a, b}
        y_set |= {c}

    horizontal_lines = []
    for _ in range(M):
        d, e, f = map(int, input().split())
        horizontal_lines.append((d, e, f))
        x_set |= {d}
        y_set |= {e, f}

    x_set = sorted(x_set)
    y_set = sorted(y_set)

    
    x_encoded = {n: i * 2 for i, n in enumerate(x_set)}
    y_encoded = {n: i * 2 for i, n in enumerate(y_set)}
    
    x_length = [x_set[i // 2 + 1] - x_set[i // 2] if i %
                2 else 0 for i in range(len(x_set) * 2 - 1)]
    y_length = [y_set[i // 2 + 1] - y_set[i // 2] if i %
                2 else 0 for i in range(len(y_set) * 2 - 1)]

    grid = [[0] * (len(y_set) * 2 - 1) for _ in range(len(x_set) * 2 - 1)]
    for a, b, c in vertical_lines:
        a, b, c = x_encoded[a], x_encoded[b], y_encoded[c]
        for x in range(a, b + 1):
            grid[x][c] = 1
    for d, e, f in horizontal_lines:
        d, e, f = x_encoded[d], y_encoded[e], y_encoded[f]
        for y in range(e, f + 1):
            grid[d][y] = 1

    que = deque()
    used = [[0] * len(y_set) * 2 for _ in range(len(x_set) * 2)]
    dy = [1, 1, -1, -1]
    dx = [1, -1, 1, -1]
    for k in range(4):
        nnx, nny = x_encoded[0] + dx[k], y_encoded[0] + dy[k]
        if 1 <= nnx < len(x_set) * 2 - 1 and 1 <= nny < len(y_set) * 2 - 1:
            que.append((nnx, nny))
            used[nnx][nny] = 1
    ans = 0
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    while que:
        x, y = que.popleft()
        if x % 2 and y % 2:
            ans += x_length[x] * y_length[y]
        for k in range(4):
            nx, ny = x + dx[k], y + dy[k]
            nnx, nny = nx + dx[k], ny + dy[k]
            if grid[nx][ny] == 0:
                if nnx < 0 or nnx >= len(x_set) * 2 - 1 or nny < 0 or nny >= len(y_set) * 2 - 1:
                    return 'INF'
                if not used[nnx][nny]:
                    que.append((nnx, nny))
                    used[nnx][nny] = 1
    return ans

print(f_single_dot())

Solution (Python):
def f_single_dot():
    
    import sys
    from collections import deque
    input = sys.stdin.readline
    N, M = [int(i) for i in input().split()]

    x_set, y_set = set([0]), set([0])  

    vertical_lines = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        vertical_lines.append((a, b, c))
        x_set |= {a, b}
        y_set |= {c}

    horizontal_lines = []
    for _ in range(M):
        d, e, f = map(int, input().split())
        horizontal_lines.append((d, e, f))
        x_set |= {d}
        y_set |= {e, f}

    x_set = sorted(x_set)
    y_set = sorted(y_set)

    
    x_encoded = {n: i * 2 for i, n in enumerate(x_set)}
    y_encoded = {n: i * 2 for i, n in enumerate(y_set)}
    
    x_length = [x_set[i // 2 + 1] - x_set[i // 2] if i %
                2 else 0 for i in range(len(x_set) * 2 - 1)]
    y_length = [y_set[i // 2 + 1] - y_set[i // 2] if i %
                2 else 0 for i in range(len(y_set) * 2 - 1)]

    grid = [[0] * (len(x_set) * 2 - 1) for _ in range(len(y_set) * 2 - 1)]
    for a, b, c in vertical_lines:
        a, b, c = x_encoded[a], x_encoded[b], y_encoded[c]
        for x in range(a, b + 1):
            grid[c][x] = 1
    for d, e, f in horizontal_lines:
        d, e, f = x_encoded[d], y_encoded[e], y_encoded[f]
        for y in range(e, f + 1):
            grid[y][d] = 1

    que = deque()
    used = [[0] * len(x_set) * 2 for _ in range(len(y_set) * 2)]
    dy = [1, 1, -1, -1]
    dx = [1, -1, 1, -1]
    for k in range(4):
        nny, nnx = y_encoded[0] + dy[k], x_encoded[0] + dx[k]
        if 1 <= nny < len(y_set) * 2 - 1 and 1 <= nnx < len(x_set) * 2 - 1:
            que.append((nny, nnx))
            used[nny][nnx] = 1
    ans = 0
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    while que:
        y, x = que.popleft()
        if y % 2 and x % 2:
            ans += y_length[y] * x_length[x]
        for k in range(4):
            ny, nx = y + dy[k], x + dx[k]
            nny, nnx = ny + dy[k], nx + dx[k]
            if grid[ny][nx] == 0:
                if nny < 0 or nny >= len(y_set) * 2 - 1 or nnx < 0 or nnx >= len(x_set) * 2 - 1:
                    return 'INF'
                if not used[nny][nnx]:
                    que.append((nny, nnx))
                    used[nny][nnx] = 1
    return ans

print(f_single_dot())

Solution (Python):
def f_single_dot():
    
    import sys
    from collections import deque
    input = sys.stdin.readline
    N, M = [int(i) for i in input().split()]

    X, Y = set([0]), set([0])
    vertical_lines = []
    for _ in range(N):
        a, b, grid = map(int, input().split())
        vertical_lines.append((a, b, grid))
        X |= {a, b}
        Y |= {grid}
    horizontal_lines = []
    for _ in range(M):
        d, e, f = map(int, input().split())
        horizontal_lines.append((d, e, f))
        X |= {d}
        Y |= {e, f}
    X = sorted(X)
    Y = sorted(Y)
    dictx, dicty = {n: i * 2 for i, n in enumerate(X)}, {n: i * 2 for i, n in enumerate(Y)}
    LX = [X[i // 2 + 1] - X[i // 2] if i % 2 else 0 for i in range(len(X) * 2 - 1)]
    LY = [Y[i // 2 + 1] - Y[i // 2] if i % 2 else 0 for i in range(len(Y) * 2 - 1)]
    grid = [[0] * (len(X) * 2 - 1) for _ in range(len(Y) * 2 - 1)]
    for a, b, c in vertical_lines:
        a, b, c = dictx[a], dictx[b], dicty[c]
        for x in range(a, b + 1):
            grid[c][x] = 1
    for d, e, f in horizontal_lines:
        d, e, f = dictx[d], dicty[e], dicty[f]
        for y in range(e, f + 1):
            grid[y][d] = 1
    que = deque()
    used = [[0] * len(X) * 2 for _ in range(len(Y) * 2)]
    dy = [1, 1, -1, -1]
    dx = [1, -1, 1, -1]
    for k in range(4):
        nny, nnx = dicty[0] + dy[k], dictx[0] + dx[k]
        if 1 <= nny < len(Y) * 2 - 1 and 1 <= nnx < len(X) * 2 - 1:
            que.append((nny, nnx))
            used[nny][nnx] = 1
    ans = 0
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    while que:
        y, x = que.popleft()
        if y % 2 and x % 2:
            ans += LY[y] * LX[x]
        for k in range(4):
            ny, nx = y + dy[k], x + dx[k]
            nny, nnx = ny + dy[k], nx + dx[k]
            if grid[ny][nx] == 0:
                if nny < 0 or nny >= len(Y) * 2 - 1 or nnx < 0 or nnx >= len(X) * 2 - 1:
                    return 'INF'
                if not used[nny][nnx]:
                    que.append((nny, nnx))
                    used[nny][nnx] = 1
    return ans

print(f_single_dot())

Solution (Python):
def f_single_dot():
    
    import sys
    from collections import deque
    input = sys.stdin.readline
    N, M = [int(i) for i in input().split()]

    X, Y = set([0]), set([0])
    A, B, C = [0] * N, [0] * N, [0] * N
    for i in range(N):
        A[i], B[i], C[i] = map(int, input().split())
        X |= {A[i], B[i]}
        Y |= {C[i]}
    D, E, F = [0] * M, [0] * M, [0] * M
    for i in range(M):
        D[i], E[i], F[i] = map(int, input().split())
        X |= {D[i]}
        Y |= {E[i], F[i]}
    X = sorted(X)
    Y = sorted(Y)
    dictx, dicty = {n: i * 2 for i, n in enumerate(X)}, {n: i * 2 for i, n in enumerate(Y)}
    LX = [X[i // 2 + 1] - X[i // 2] if i % 2 else 0 for i in range(len(X) * 2 - 1)]
    LY = [Y[i // 2 + 1] - Y[i // 2] if i % 2 else 0 for i in range(len(Y) * 2 - 1)]
    c = [[0] * (len(X) * 2 - 1) for _ in range(len(Y) * 2 - 1)]
    for i in range(N):
        y = dicty[C[i]]
        for x in range(dictx[A[i]], dictx[B[i]] + 1):
            c[y][x] = 1
    for i in range(M):
        x = dictx[D[i]]
        for y in range(dicty[E[i]], dicty[F[i]] + 1):
            c[y][x] = 1
    que = deque()
    used = [[0] * len(X) * 2 for _ in range(len(Y) * 2)]
    dy = [1, 1, -1, -1]
    dx = [1, -1, 1, -1]
    for k in range(4):
        nny, nnx = dicty[0] + dy[k], dictx[0] + dx[k]
        if 1 <= nny < len(Y) * 2 - 1 and 1 <= nnx < len(X) * 2 - 1:
            que.append((nny, nnx))
            used[nny][nnx] = 1
    ans = 0
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    while que:
        y, x = que.popleft()
        if y % 2 and x % 2:
            ans += LY[y] * LX[x]
        for k in range(4):
            ny, nx = y + dy[k], x + dx[k]
            nny, nnx = ny + dy[k], nx + dx[k]
            if c[ny][nx] == 0:
                if nny < 0 or nny >= len(Y) * 2 - 1 or nnx < 0 or nnx >= len(X) * 2 - 1:
                    return 'INF'
                if not used[nny][nnx]:
                    que.append((nny, nnx))
                    used[nny][nnx] = 1
    return ans

print(f_single_dot())