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:
import sys
from collections import deque
import numpy as np

def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())

def main():
    n, m = mi()
    xd, yd = {0}, {0}
    lh = []
    lv = []
    for i in range(n):
        a, b, c = mi()
        lh.append((a, b, c))
        xd.add(a)
        xd.add(b)
        yd.add(c)
    for i in range(m):
        c, a, b = mi()
        lv.append((a, b, c))
        yd.add(a)
        yd.add(b)
        xd.add(c)
    xd = list(sorted(xd))
    yd = list(sorted(yd))
    mx, my = {}, {}
    for i, x in enumerate(xd):
        mx[x] = i
    for i, y in enumerate(yd):
        my[y] = i
    h = 2*len(xd)
    w = 2*len(yd)

    g = np.full((h, w), False, dtype=bool)
    for a, b, c in lh:
        a = mx[a]*2
        b = mx[b]*2
        c = my[c]*2
        g[a:b+1, c] = True
    for a, b, c in lv:
        a = my[a]*2
        b = my[b]*2
        c = mx[c]*2
        g[c, a:b+1] = True
    
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    xs, ys = mx[0]*2, my[0]*2
    q = deque([(xs, ys)])
    ans = 0
    while q:
        x, y = q.popleft()
        if x == 0 or x == h-1 or y == 0 or y == w-1:
            print('INF')
            return
        if x%2 == 1 and y%2 == 1:
            ans += (xd[x//2+1]-xd[x//2])*(yd[y//2+1]-yd[y//2])

        for v in range(4):
            nx = x + dx[v]
            ny = y + dy[v]
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            if g[nx,ny]:
                continue
            g[nx,ny] = True
            q.append((nx, ny))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
from collections import deque
import numpy as np
from bisect import bisect_left as bl

def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())

def main():
    n, m = mi()
    xd, yd = {0}, {0}
    lh = []
    lv = []
    for i in range(n):
        a, b, c = mi()
        lh.append((a, b, c))
        xd.add(a)
        xd.add(b)
        yd.add(c)
    for i in range(m):
        c, a, b = mi()
        lv.append((a, b, c))
        yd.add(a)
        yd.add(b)
        xd.add(c)
    xd = list(sorted(xd))
    yd = list(sorted(yd))
    h = 2*len(xd)
    w = 2*len(yd)
    g = np.zeros((h, w), dtype=int)
    for a, b, c in lh:
        a = bl(xd, a)*2
        b = bl(xd, b)*2
        c = bl(yd, c)*2
        g[a:b+1, c] = -1
    for a, b, c in lv:
        a = bl(yd, a)*2
        b = bl(yd, b)*2
        c = bl(xd, c)*2
        g[c, a:b+1] = -1
    
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]
    xs, ys = bl(xd, 0)*2, bl(yd, 0)*2
    q = deque([(xs, ys)])
    ans = 0
    while q:
        x, y = q.popleft()
        if x == 0 or x == h-1 or y == 0 or y == w-1:
            print('INF')
            return
        if x%2 == 1 and y%2 == 1:
            ans += (xd[x//2+1]-xd[x//2])*(yd[y//2+1]-yd[y//2])

        for v in range(4):
            nx = x + dx[v]
            ny = y + dy[v]
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            if g[nx,ny] != 0:
                continue
            g[nx,ny] = 1
            q.append((nx, ny))
    print(ans)
        
    
    
        
        
    
        



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())

from collections import defaultdict
from collections import deque
def main():
    n, m = mi()
    mx = defaultdict(int)
    my = defaultdict(int)
    mx[0] = 0
    my[0] = 0
    lh = []
    lv = []
    for i in range(n):
        a, b, c = mi()
        lh.append((a, b, c))
        mx[a] = 0
        mx[b] = 0
        my[c] = 0
    for i in range(m):
        c, a, b = mi()
        lv.append((a, b, c))
        my[a] = 0
        my[b] = 0
        mx[c] = 0
    xd, yd = [], []
    for key in sorted(list(mx.keys())):
        mx[key] = len(xd)
        xd.append(key)
    for key in sorted(list(my.keys())):
        my[key] = len(yd)
        yd.append(key)

    h = 2*len(xd)
    w = 2*len(yd)
    g = [[0]*w for _ in range(h)]
    for a, b, c in lh:
        a = mx[a]*2
        b = mx[b]*2
        c = my[c]*2
        for i in range(a, b+1):
            g[i][c] = -1
    for a, b, c in lv:
        a = my[a]*2
        b = my[b]*2
        c = mx[c]*2
        for i in range(a, b+1):
            g[c][i] = -1
    
    dx= [-1, 0, 1, 0]
    dy= [0, 1, 0, -1]
    xs, ys = mx[0]*2, my[0]*2
    q = deque([(xs, ys)])
    ans = 0
    while q:
        x, y = q.popleft()
        if x == 0 or x == h-1 or y == 0 or y == w-1:
            print('INF')
            return
        if x%2 == 1 and y%2 == 1:
            ans += (xd[x//2+1]-xd[x//2])*(yd[y//2+1]-yd[y//2])

        for v in range(4):
            nx = x + dx[v]
            ny = y + dy[v]
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            if g[nx][ny] != 0:
                continue
            g[nx][ny] = 1
            q.append((nx, ny))
    print(ans)
        
    
    
        
        
    
        



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())

from collections import defaultdict
from collections import deque
def main():
    n, m = mi()
    mx = defaultdict(int)
    my = defaultdict(int)
    mx[0] = 0
    my[0] = 0
    lh = []
    lv = []
    for i in range(n):
        a, b, c = mi()
        lh.append((a, b, c))
        mx[a] = 0
        mx[b] = 0
        my[c] = 0
    for i in range(m):
        c, a, b = mi()
        lv.append((a, b, c))
        my[a] = 0
        my[b] = 0
        mx[c] = 0
    xd, yd = [], []
    for key in sorted(list(mx.keys())):
        mx[key] = len(xd)
        xd.append(key)
    for key in sorted(list(my.keys())):
        my[key] = len(yd)
        yd.append(key)

    h = 2*len(xd)
    w = 2*len(yd)
    g = [[0]*w for _ in range(h)]
    for a, b, c in lh:
        a = mx[a]*2
        b = mx[b]*2
        c = my[c]*2
        for i in range(a, b+1):
            g[i][c] = -1
    for a, b, c in lv:
        a = my[a]*2
        b = my[b]*2
        c = mx[c]*2
        for i in range(a, b+1):
            g[c][i] = -1
    
    dx= [-1, 0, 1, 0]
    dy= [0, 1, 0, -1]
    xs, ys = mx[0]*2, my[0]*2
    q = deque([(xs, ys)])
    ans = 0
    while q:
        x, y = q.popleft()
        if x == 0 or x == h-1 or y == 0 or y == w-1:
            print('INF')
            return
        if x%2 == 1 and y%2 == 1:
            ans += (xd[x//2+1]-xd[x//2])*(yd[y//2+1]-yd[y//2])

        for v in range(4):
            nx = x + dx[v]
            ny = y + dy[v]
            if nx < 0 or nx >= h or ny < 0 or ny >= w:
                continue
            if g[nx][ny] != 0:
                continue
            g[nx][ny] = 1
            q.append((nx, ny))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

X = [-10**9, 0, 10**9]
Y = [-10**9, 0, 10**9]

tate = []
yoko = []

for _ in range(N):
    a, b, c = map(int, input().split())
    X.append(a)
    X.append(b)
    Y.append(c)
    tate.append((a, b, c))

for _ in range(M):
    d, e, f = map(int, input().split())
    X.append(d)
    Y.append(e)
    Y.append(f)
    yoko.append((d, e, f))

X = sorted(set(X))
Y = sorted(set(Y))

Cx = {val: idx * 2 for idx, val in enumerate(X)}
Cy = {val: idx * 2 for idx, val in enumerate(Y)}

H = len(X) * 2 - 1
W = len(Y) * 2 - 1

G = [0] * (H * W)

for a, b, c in tate:
    j = Cy[c]
    for i in range(Cx[a], Cx[b] + 1):
        G[i * W + j] = -1

for d, e, f in yoko:
    i = Cx[d]
    for j in range(Cy[e], Cy[f] + 1):
        G[i * W + j] = -1

for i in range(1, H, 2):
    for j in range(1, W, 2):
        G[i * W + j] = (X[(i + 1) // 2] - X[(i - 1) // 2]) * (Y[(j + 1) // 2] - Y[(j - 1) // 2])

stack = [Cx[0] * W + Cy[0]]
visited = [0] * (H * W)
visited[Cx[0] * W + Cy[0]] = 1
dz = [1, -1, W, -W]
res = 0

while stack:
    z = stack.pop()
    for k in range(4):
        nz = z + dz[k]
        if G[nz] == -1:
            continue
        nx, ny = divmod(nz, W)
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            break
        if visited[nz]:
            continue
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    else:
        continue
    print('INF')
    break
else:
    print(res)

Submission status: Time Limit Exceeded

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

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

X = [-10**9, 0, 10**9]
Y = [-10**9, 0, 10**9]

tate = []
yoko = []

for _ in range(N):
    a, b, c = map(int, input().split())
    X.append(a)
    X.append(b)
    Y.append(c)
    tate.append((a, b, c))

for _ in range(M):
    d, e, f = map(int, input().split())
    X.append(d)
    Y.append(e)
    Y.append(f)
    yoko.append((d, e, f))

X = sorted(set(X))
Y = sorted(set(Y))

Cx = {val: idx * 2 for idx, val in enumerate(X)}
Cy = {val: idx * 2 for idx, val in enumerate(Y)}

H = len(X) * 2 - 1
W = len(Y) * 2 - 1

G = [0 for _ in range(H * W)]

for a, b, c in tate:
    j = Cy[c]
    for i in range(Cx[a], Cx[b] + 1):
        G[i * W + j] = -1

for d, e, f in yoko:
    i = Cx[d]
    for j in range(Cy[e], Cy[f] + 1):
        G[i * W + j] = -1

for i in range(1, H, 2):
    for j in range(1, W, 2):
        if G[i * W + j] == -1:
            continue
        G[i * W + j] = (X[(i + 1) // 2] - X[(i - 1) // 2]) * (Y[(j + 1) // 2] - Y[(j - 1) // 2])

stack = [Cx[0] * W + Cy[0]]
visited = [0 for _ in range(H * W)]
visited[Cx[0] * W + Cy[0]] = 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
res = 0

while stack:
    z = stack.pop()
    x, y = divmod(z, W)
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        nz = nx * W + ny
        if G[nz] == -1:
            continue
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            break
        if visited[nz]:
            continue
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    else:
        continue
    print('INF')
    break
else:
    print(res)

Submission status: Time Limit Exceeded

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

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

X = [-10**9, 0, 10**9]
Y = [-10**9, 0, 10**9]

tate = []
yoko = []

for _ in range(N):
    a, b, c = map(int, input().split())
    X.append(a)
    X.append(b)
    Y.append(c)
    tate.append((a, b, c))

for _ in range(M):
    d, e, f = map(int, input().split())
    X.append(d)
    Y.append(e)
    Y.append(f)
    yoko.append((d, e, f))

X = sorted(set(X))
Y = sorted(set(Y))

Cx = {val: idx * 2 for idx, val in enumerate(X)}
Cy = {val: idx * 2 for idx, val in enumerate(Y)}

H = len(X) * 2 - 1
W = len(Y) * 2 - 1

G = [0 for _ in range(H * W)]

for a, b, c in tate:
    j = Cy[c]
    for i in range(Cx[a], Cx[b] + 1):
        G[i * W + j] = -1

for d, e, f in yoko:
    i = Cx[d]
    for j in range(Cy[e], Cy[f] + 1):
        G[i * W + j] = -1

for i in range(1, H, 2):
    for j in range(1, W, 2):
        if G[i * W + j] == -1:
            continue
        G[i * W + j] = (X[(i + 1) // 2] - X[(i - 1) // 2]) * (Y[(j + 1) // 2] - Y[(j - 1) // 2])

stack = [Cx[0] * W + Cy[0]]
visited = [0 for _ in range(H * W)]
visited[Cx[0] * W + Cy[0]] = 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
res = 0

while stack:
    z = stack.pop()
    x, y = divmod(z, W)
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if G[nx * W + ny] == -1:
            continue
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            break
        if visited[nx * W + ny]:
            continue
        visited[nx * W + ny] = 1
        res += G[nx * W + ny]
        stack.append(nx * W + ny)
    else:
        continue
    print('INF')
    break
else:
    print(res)

Submission status: Time Limit Exceeded

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

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

    X = [-10**9, 0, 10**9]
    Y = [-10**9, 0, 10**9]

    tate = []
    yoko = []

    for _ in range(N):
        a, b, c = map(int, input().split())
        X.append(a)
        X.append(b)
        Y.append(c)
        tate.append((a, b, c))

    for _ in range(M):
        d, e, f = map(int, input().split())
        X.append(d)
        Y.append(e)
        Y.append(f)
        yoko.append((d, e, f))

    X = sorted(set(X))
    Y = sorted(set(Y))

    Cx = {val: idx * 2 for idx, val in enumerate(X)}
    Cy = {val: idx * 2 for idx, val in enumerate(Y)}

    H = len(X) * 2 - 1
    W = len(Y) * 2 - 1

    G = [[0 for j in range(W)] for i in range(H)]

    for a, b, c in tate:
        for i in range(Cx[a], Cx[b] + 1):
            G[i][Cy[c]] = -1

    for d, e, f in yoko:
        for j in range(Cy[e], Cy[f] + 1):
            G[Cx[d]][j] = -1

    for i in range(1, H, 2):
        for j in range(1, W, 2):
            if G[i][j] == -1:
                continue
            G[i][j] = (X[(i + 1) // 2] - X[(i - 1) // 2]) * (Y[(j + 1) // 2] - Y[(j - 1) // 2])

    stack = [Cx[0] * W + Cy[0]]
    visited = [[0 for j in range(W)] for i in range(H)]
    visited[Cx[0]][Cy[0]] = 1
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]
    res = 0

    while stack:
        z = stack.pop()
        x, y = divmod(z, W)
        for k in range(4):
            nx = x + dx[k]
            ny = y + dy[k]
            if G[nx][ny] == -1:
                continue
            if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
                print('INF')
                break
            if visited[nx][ny]:
                continue
            visited[nx][ny] = 1
            res += G[nx][ny]
            stack.append(nx * W + ny)
        else:
            continue
        break
    else:
        print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

X = [-10**9, 0, 10**9]
Y = [-10**9, 0, 10**9]

tate = []
yoko = []

for _ in range(N):
    a, b, c = map(int, input().split())
    X.append(a)
    X.append(b)
    Y.append(c)
    tate.append((a, b, c))

for _ in range(M):
    d, e, f = map(int, input().split())
    X.append(d)
    Y.append(e)
    Y.append(f)
    yoko.append((d, e, f))

X = sorted(set(X))
Y = sorted(set(Y))

Cx = {val: idx * 2 for idx, val in enumerate(X)}
Cy = {val: idx * 2 for idx, val in enumerate(Y)}

H = len(X) * 2 - 1
W = len(Y) * 2 - 1

G = [[0 for j in range(W)] for i in range(H)]

for a, b, c in tate:
    for i in range(Cx[a], Cx[b] + 1):
        G[i][Cy[c]] = -1

for d, e, f in yoko:
    for j in range(Cy[e], Cy[f] + 1):
        G[Cx[d]][j] = -1

for i in range(1, H, 2):
    for j in range(1, W, 2):
        if G[i][j] == -1:
            continue
        G[i][j] = (X[(i + 1) // 2] - X[(i - 1) // 2]) * (Y[(j + 1) // 2] - Y[(j - 1) // 2])

stack = [Cx[0] * W + Cy[0]]
visited = [[0 for j in range(W)] for i in range(H)]
visited[Cx[0]][Cy[0]] = 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
res = 0

while stack:
    z = stack.pop()
    x, y = divmod(z, W)
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if G[nx][ny] == -1:
            continue
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            print('INF')
            break
        if visited[nx][ny]:
            continue
        visited[nx][ny] = 1
        res += G[nx][ny]
        stack.append(nx * W + ny)
    else:
        continue
    break
else:
    print(res)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())

X = [-10**9, 0, 10**9]
Y = [-10**9, 0, 10**9]

tate = []
yoko = []

for _ in range(N):
    a, b, c = map(int, input().split())
    X.append(a)
    X.append(b)
    Y.append(c)
    tate.append((a, b, c))

for _ in range(M):
    d, e, f = map(int, input().split())
    X.append(d)
    Y.append(e)
    Y.append(f)
    yoko.append((d, e, f))

X = sorted(set(X))
Y = sorted(set(Y))

Cx = {val: idx * 2 for idx, val in enumerate(X)}
Cy = {val: idx * 2 for idx, val in enumerate(Y)}

H = len(X) * 2 - 1
W = len(Y) * 2 - 1

G = [[0 for j in range(W)] for i in range(H)]

for a, b, c in tate:
    for i in range(Cx[a], Cx[b] + 1):
        G[i][Cy[c]] = -1

for d, e, f in yoko:
    for j in range(Cy[e], Cy[f] + 1):
        G[Cx[d]][j] = -1

for i in range(1, H, 2):
    for j in range(1, W, 2):
        if G[i][j] == -1:
            continue
        G[i][j] = (X[(i + 1) // 2] - X[(i - 1) // 2]) * (Y[(j + 1) // 2] - Y[(j - 1) // 2])

stack = [(Cx[0], Cy[0])]
visited = [[0 for j in range(W)] for i in range(H)]
visited[Cx[0]][Cy[0]] = 1
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
res = 0

while stack:
    x, y = stack.pop()
    for k in range(4):
        nx = x + dx[k]
        ny = y + dy[k]
        if G[nx][ny] == -1:
            continue
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            print('INF')
            break
        if visited[nx][ny]:
            continue
        visited[nx][ny] = 1
        res += G[nx][ny]
        stack.append((nx, ny))
    else:
        continue
    break
else:
    print(res)

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, -10**9-2])
ys = set([0, -10**9-2])
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

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
xappend = stack_x.append
yappend = stack_y.append
xpop = stack_x.pop
ypop = stack_y.pop
while stack_x:
    x = xpop()
    y = ypop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        xappend(x+2)
        yappend(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        xappend(x)
        yappend(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        xappend(x-2)
        yappend(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        xappend(x)
        yappend(y-2)
        field[x][y-2] = False

    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(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)

Submission status: Time Limit Exceeded

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

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 = [-10**9-2] + sorted(xs)
scatter_y = [-10**9-2] + 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

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
while stack_x:
    x = stack_x.pop()
    y = stack_y.pop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        stack_x.append(x+2)
        stack_y.append(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        stack_x.append(x)
        stack_y.append(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        stack_x.append(x-2)
        stack_y.append(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        stack_x.append(x)
        stack_y.append(y-2)
        field[x][y-2] = False

    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(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)

Submission status: Time Limit Exceeded

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

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, -10**9-2])
ys = set([0, -10**9-2])
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

#print(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

area = 0
sx = gather_x[0] * 2 + 1
sy = gather_y[0] * 2 + 1
field[sx][sy] = False
stack_x = [sx]
stack_y = [sy]
xappend = stack_x.append
yappend = stack_y.append
xpop = stack_x.pop
ypop = stack_y.pop
while stack_x:
    x = xpop()
    y = ypop()
    if x == 1 or y == 1 or x == x_size-1 or y == y_size-1:
        print("INF")
        exit()
    
    if field[x+1][y] and field[x+2][y]:
        xappend(x+2)
        yappend(y)
        field[x+2][y] = False
    if field[x][y+1] and field[x][y+2]:
        xappend(x)
        yappend(y+2)
        field[x][y+2] = False
    if field[x-1][y] and field[x-2][y]:
        xappend(x-2)
        yappend(y)
        field[x-2][y] = False
    if field[x][y-1] and field[x][y-2]:
        xappend(x)
        yappend(y-2)
        field[x][y-2] = False

    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(*[''.join(map(lambda c: '.' if c else '#', row)) for row in field], sep = '\n')

print(area)

Submission status: Time Limit Exceeded

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

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 = deque([(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.buffer.readline
#'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

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

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

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

#'''

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

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

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

x_size = 2 * len(scatter_x)
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:
#'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

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

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

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

#'''

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

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

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

x_size = 2 * len(scatter_x)
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)