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: Runtime Error

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

hor = []
for _ in range(N):
  a,b,c = map(int,input().split())
  hor.append((c,a,b))
  xtic.add(a)
  xtic.add(b)
  ytic.add(c)
  
ver = []
for _ in range(M):
  d,e,f = map(int,input().split())
  ver.append((d,e,f))
  xtic.add(d)
  ytic.add(e)
  ytic.add(f)

xtic = list(xtic)
ytic = list(ytic)
xtic.sort()
ytic.sort()
xdic = {xtic[i]:i+1 for i in range(len(xtic))}
ydic = {ytic[i]:i+1 for i in range(len(ytic))}

gyaku_xdic = {xdic[k]:k for k in xdic}
gyaku_ydic = {ydic[k]:k for k in ydic}

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

now = None
for c,a,b in hor:
  c,a,b = ydic[c],xdic[a],xdic[b]
  if c != now:
    hor_dic[c] = (1<<b) - (1<<a)
  else:
    hor_dic[c] |= (1<<b) - (1<<a)
  now = c

now = None
for d,e,f in ver:
  d,e,f = xdic[d],ydic[e],ydic[f]
  if d != now:
    ver_dic[d] = (1<<f) - (1<<e)
  else:
    ver_dic[d] |= (1<<f) - (1<<e)
  now = d

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      if hor_wall[y+1][H-1-x]:
        return False
      return True
    else:
      if hor_wall[y][H-1-x]:
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1][W-1-y]:
        return False
      return True
    else:
      if ver_wall[x][W-1-y]:
        return False
      return True
      
def calc_area(x,y):
  X = gyaku_xdic[x+1] - gyaku_xdic[x]
  Y = gyaku_ydic[y+1] - gyaku_ydic[y]
  return X*Y

offset = [(1,0),(0,1),(-1,0),(0,-1)]
x0 = bisect.bisect(xtic,0)
y0 = bisect.bisect(ytic,0)
W,H = len(xtic)+1 ,len(ytic)+1

hor_wall = [0]*H
ver_wall = [0]*W

for k in hor_dic:
  hor_wall[k] = hor_dic[k]
for k in ver_dic:
  ver_wall[k] = ver_dic[k]
  
for k in range(H):
  listb = list(map(int,list(bin(hor_wall[k])[2:])))
  hor_wall[k] = [0]*(H-len(listb)) + listb

for k in range(W):
  listb = list(map(int,list(bin(ver_wall[k])[2:])))
  ver_wall[k] = [0]*(W-len(listb)) + listb

d = [[0 for i in range(W)] for j in range(H)]
d[y0][x0] = 1
q = deque([(x0,y0)])

area = 0

while q:
  x,y = q.pop()
  area += calc_area(x,y)
  for dx,dy in offset:
    nx,ny = x+dx,y+dy
    if 0<=nx<W and 0<=ny<H:
      if is_connected(x,y,dx,dy):
        if nx == 0 or ny == 0 or nx == W-1 or ny == H-1:
          print("INF")
          exit()
        if d[ny][nx] == 0:
          d[ny][nx] = 1
          q.append((nx,ny))

print(area)

Submission status: Runtime Error

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

hor = []
for _ in range(N):
  a,b,c = map(int,input().split())
  hor.append((c,a,b))
  xtic.add(a)
  xtic.add(b)
  ytic.add(c)
  
ver = []
for _ in range(M):
  d,e,f = map(int,input().split())
  ver.append((d,e,f))
  xtic.add(d)
  ytic.add(e)
  ytic.add(f)

xtic = list(xtic)
ytic = list(ytic)
xtic.sort()
ytic.sort()
xdic = {xtic[i]:i+1 for i in range(len(xtic))}
ydic = {ytic[i]:i+1 for i in range(len(ytic))}

gyaku_xdic = {xdic[k]:k for k in xdic}
gyaku_ydic = {ydic[k]:k for k in ydic}

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

now = None
for c,a,b in hor:
  c,a,b = ydic[c],xdic[a],xdic[b]
  if c != now:
    hor_dic[c] = (1<<b) - (1<<a)
  else:
    hor_dic[c] |= (1<<b) - (1<<a)
  now = c

now = None
for d,e,f in ver:
  d,e,f = xdic[d],ydic[e],ydic[f]
  if d != now:
    ver_dic[d] = (1<<f) - (1<<e)
  else:
    ver_dic[d] |= (1<<f) - (1<<e)
  now = d

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      if hor_wall[y+1][x]:
        return False
      return True
    else:
      if hor_wall[y][x]:
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1][y]:
        return False
      return True
    else:
      if ver_wall[x][y]:
        return False
      return True
      
def calc_area(x,y):
  X = gyaku_xdic[x+1] - gyaku_xdic[x]
  Y = gyaku_ydic[y+1] - gyaku_ydic[y]
  return X*Y

offset = [(1,0),(0,1),(-1,0),(0,-1)]
x0 = bisect.bisect(xtic,0)
y0 = bisect.bisect(ytic,0)
W,H = len(xtic)+1 ,len(ytic)+1

gyaku_xdic[0] = -INF
gyaku_ydic[0] = -INF
gyaku_xdic[W] = INF
gyaku_ydic[H] = INF

hor_wall = [0] + [0 for i in range(1,H)]
ver_wall = [0] + [0 for i in range(1,W)]

for k in hor_dic:
  hor_wall[k] = hor_dic[k]
for k in ver_dic:
  ver_wall[k] = ver_dic[k]
  
for k in range(len(hor_wall)):
  listb = list(map(int,list(bin(hor_wall[k])[2:])))
  hor_wall[k] = [0 for j in range(H)]
  hor_wall[k][:len(listb)] = listb[::-1]

for k in range(len(ver_wall)):
  listb = list(map(int,list(bin(ver_wall[k])[2:])))
  ver_wall[k] = [0 for j in range(W)]
  ver_wall[k][:len(listb)] = listb[::-1]
  


d = [[0 for i in range(W)] for j in range(H)]
d[y0][x0] = 1
q = deque([(x0,y0)])

area = 0

while q:
  x,y = q.pop()
  area += calc_area(x,y)
  for dx,dy in offset:
    nx,ny = x+dx,y+dy
    if 0<=nx<W and 0<=ny<H:
      if is_connected(x,y,dx,dy):
        if nx == 0 or ny == 0 or nx == W-1 or ny == H-1:
          print("INF")
          exit()
        if d[ny][nx] == 0:
          d[ny][nx] = 1
          q.append((nx,ny))

print(area)

Submission status: Runtime Error

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

hor = []
for _ in range(N):
  a,b,c = map(int,input().split())
  hor.append((c,a,b))
  xtic.add(a)
  xtic.add(b)
  ytic.add(c)
  
ver = []
for _ in range(M):
  d,e,f = map(int,input().split())
  ver.append((d,e,f))
  xtic.add(d)
  ytic.add(e)
  ytic.add(f)

xtic = list(xtic)
ytic = list(ytic)
xtic.sort()
ytic.sort()
xdic = {xtic[i]:i+1 for i in range(len(xtic))}
ydic = {ytic[i]:i+1 for i in range(len(ytic))}

gyaku_xdic = {xdic[k]:k for k in xdic}
gyaku_ydic = {ydic[k]:k for k in ydic}

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

now = None
for c,a,b in hor:
  c,a,b = ydic[c],xdic[a],xdic[b]
  if c != now:
    hor_dic[c] = (1<<b) - (1<<a)
  else:
    hor_dic[c] |= (1<<b) - (1<<a)
  now = c

now = None
for d,e,f in ver:
  d,e,f = xdic[d],ydic[e],ydic[f]
  if d != now:
    ver_dic[d] = (1<<f) - (1<<e)
  else:
    ver_dic[d] |= (1<<f) - (1<<e)
  now = d

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      if hor_wall[y+1][H-1-x]:
        return False
      return True
    else:
      if hor_wall[y][H-1-x]:
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1][W-1-y]:
        return False
      return True
    else:
      if ver_wall[x][W-1-y]:
        return False
      return True
      
def calc_area(x,y):
  X = gyaku_xdic[x+1] - gyaku_xdic[x]
  Y = gyaku_ydic[y+1] - gyaku_ydic[y]
  return X*Y

offset = [(1,0),(0,1),(-1,0),(0,-1)]
x0 = bisect.bisect(xtic,0)
y0 = bisect.bisect(ytic,0)
W,H = len(xtic)+1 ,len(ytic)+1

gyaku_xdic[0] = -INF
gyaku_ydic[0] = -INF
gyaku_xdic[W] = INF
gyaku_ydic[H] = INF

hor_wall = [0] + [0 for i in range(1,H)]
ver_wall = [0] + [0 for i in range(1,W)]

for k in hor_dic:
  hor_wall[k] = hor_dic[k]
for k in ver_dic:
  ver_wall[k] = ver_dic[k]
  
for i in range(len(hor_wall)):
  listb = list(map(int,(list(bin(hor_wall[i])[2:]))))
  hor_wall[i] = [0 for j in range(W)]
  hor_wall[i][-len(listb):] = listb
for i in range(len(ver_wall)):
  listb = list(map(int,(list(bin(ver_wall[i])[2:]))))
  ver_wall[i] = [0 for j in range(H)]
  ver_wall[i][-len(listb):] = listb

d = [[0 for i in range(W)] for j in range(H)]
d[y0][x0] = 1
q = deque([(x0,y0)])

area = 0

while q:
  x,y = q.pop()
  area += calc_area(x,y)
  for dx,dy in offset:
    nx,ny = x+dx,y+dy
    if 0<=nx<W and 0<=ny<H:
      if is_connected(x,y,dx,dy):
        if nx == 0 or ny == 0 or nx == W-1 or ny == H-1:
          print("INF")
          exit()
        if d[ny][nx] == 0:
          d[ny][nx] = 1
          q.append((nx,ny))

print(area)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

def precompute(N, M, data):
    data = data.reshape(-1, 3)
    A, B, C = data[:N].T
    D, E, F = data[N:].T
    X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
    Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
    A = np.searchsorted(X, A).astype(np.int32)
    B = np.searchsorted(X, B).astype(np.int32)
    D = np.searchsorted(X, D).astype(np.int32)
    C = np.searchsorted(Y, C).astype(np.int32)
    E = np.searchsorted(Y, E).astype(np.int32)
    F = np.searchsorted(Y, F).astype(np.int32)
    return A, B, C, D, E, F, X, Y

def solve(A, B, C, D, E, F, X, Y):
    H, W = len(X), len(Y)
    N = H * W
    DX = X[1:] - X[:-1]
    DY = Y[1:] - Y[:-1]

    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 = set_ng(A, B, C, D, E, F, H, W)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int32)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return np.int64(DX[x]) * np.int64(DY[y])

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

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

    push(v0)
    move = (1, -1, W, -W)
    while p:
        v = pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    signature = '(i4[:],i4[:],i4[:],i4[:],i4[:],i4[:],i4[:],i4[:])'
    cc.export('solve', signature)(solve)
    cc.compile()

if sys.argv[-1] == 'ONLINE_JUDGE':
    cc_export()
    exit()

from my_module import solve

N, M = map(int, readline().split())
data = np.int32(read().split())

x = solve(*precompute(N, M, data))
if x == 0:
    print('INF')
else:
    print(x)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

def precompute(N, M, data):
    data = data.reshape(-1, 3)
    A, B, C = data[:N].T
    D, E, F = data[N:].T
    X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
    Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
    A = np.searchsorted(X, A).astype(np.int32)
    B = np.searchsorted(X, B).astype(np.int32)
    D = np.searchsorted(X, D).astype(np.int32)
    C = np.searchsorted(Y, C).astype(np.int32)
    E = np.searchsorted(Y, E).astype(np.int32)
    F = np.searchsorted(Y, F).astype(np.int32)
    return A, B, C, D, E, F, X, Y

def solve(A, B, C, D, E, F, X, Y):
    H, W = len(X), len(Y)
    N = H * W
    DX = X[1:] - X[:-1]
    DY = Y[1:] - Y[:-1]

    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 = set_ng(A, B, C, D, E, F, H, W)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = np.empty(N, np.int32)
    p = 0
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return np.int64(DX[x]) * np.int64(DY[y])

    def push(x):
        nonlocal p, ret
        stack[p] = x
        visited[x] = 1
        ret += area(x)
        p += 1

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

    push(v0)
    move = (1, -1, W, -W)
    while p:
        v = pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret

def cc_export():
    from numba.pycc import CC
    cc = CC('my_module')
    signature = '(i4[::1],i4[::1],i4[::1],i4[::1],i4[::1],i4[::1],i4[::1],i4[::1])'
    cc.export('solve', signature)(solve)
    cc.compile()

if sys.argv[-1] == 'ONLINE_JUDGE':
    cc_export()
    exit()

from my_module import solve

N, M = map(int, readline().split())
data = np.int32(read().split())

x = solve(*precompute(N, M, data))
if x == 0:
    print('INF')
else:
    print(x)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

sys.setrecursionlimit(10 ** 7)

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(c, enable, x_list, y_list, moves):
    enable[c] = 1
    ans = 0

    for dc, banned in moves:
        nc = c + dc
        if dc < 0:
            if banned[c]:
                continue
        else:
            if banned[nc]:
                continue
        if enable[nc] == -1:
            return -1
        if enable[nc] == 1:
            continue
        ret = dfs(nc, enable, x_list, y_list, moves)
        if ret == -1:
            return -1
        ans += ret

    i, j = divmod(c, row)
    t = y_list[i - 1]
    b = y_list[i]
    l = x_list[j - 1]
    r = x_list[j]
    ans += (b - t) * (r - l)

    return ans


@njit
def solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]

    return dfs(s, enable, x_list, y_list, moves)


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

ans = solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_left):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]


@njit
def dfs(c, enable, x_list, y_list, moves):
    enable[c] = 1
    ans = 0

    for dc, banned in moves:
        nc = c + dc
        if dc < 0:
            if banned[c]:
                continue
        else:
            if banned[nc]:
                continue
        if enable[nc] == -1:
            return -1
        if enable[nc] == 1:
            continue
        ret = dfs(nc, enable, x_list, y_list, moves)
        if ret == -1:
            return -1
        ans += ret

    i, j = divmod(c, row)
    t = y_list[i - 1]
    b = y_list[i]
    l = x_list[j - 1]
    r = x_list[j]
    ans += (b - t) * (r - l)

    return ans


@njit
def solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = np.array([-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row, dtype=np.int8)

    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]

    return dfs(s, enable, x_list, y_list, moves)


for a, b, c in zip(aaa, bbb, ccc):
    register_ver_lines(a, b, c, banned_left, x_dict, y_dict)
for d, e, f in zip(ddd, eee, fff):
    register_hor_lines(d, e, f, banned_up, x_dict, y_dict)

accumulate_banned(banned_up, banned_left)

ans = solve(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')

Submission status: Runtime Error

Solution code:
from collections import OrderedDict, deque

N, M = map(int, input().split())
xs = set([0])
ys = set([0])

ABCs = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABCs.append([a, b, c])
    xs.add(a)
    xs.add(b)
    ys.add(c)
DEFs = []
for _ in range(M):
    d, e, f = map(int, input().split())
    DEFs.append([d, e, f])
    xs.add(d)
    ys.add(e)
    ys.add(f)

xs = list(xs)
xs.sort()
x_diff = [x1 - x0 for x1, x0 in zip(xs[1:], xs)]
xs = dict(zip(xs, range(len(xs))))
ys = list(ys)
ys.sort()
y_diff = [y1 - y0 for y1, y0 in zip(ys[1:], ys)]
ys = dict(zip(ys, range(len(ys))))


ABC_saku = [[True for _ in range(len(ys))] for _ in range(len(xs) - 1)]
for a, b, c in ABCs:
    a = xs[a]
    b = xs[b]
    c = ys[c]
    for ab in range(a, b):
        ABC_saku[ab][c] = False
DEFs_saku = [[True for _ in range(len(ys) - 1)] for _ in range(len(xs))]
for d, e, f in DEFs:
    d = xs[d]
    e = ys[e]
    f = ys[f]
    for ef in range(e, f):
        DEFs_saku[d][ef] = False

Q = deque([(xs[0], ys[0])])
visited = set(Q)
ans = 0


def check_add(new_point):
    if new_point not in visited:
        Q.append(new_point)
        visited.add(new_point)


while Q:
    x, y = Q.popleft()
    ans += x_diff[x] * y_diff[y]
    # y--
    if ABC_saku[x][y]:
        if y - 1 >= 0:
            new_point = (x, y - 1)
            check_add(new_point)
        else:
            ans = "INF"
            break
    if ABC_saku[x][y + 1]:
        if y + 1 < len(ys) - 1:
            new_point = (x, y + 1)
            check_add(new_point)
        else:
            ans = "INF"
            break
    if DEFs_saku[x][y]:
        if x - 1 >= 0:
            new_point = (x - 1, y)
            check_add(new_point)
        else:
            ans = "INF"
            break
    if DEFs_saku[x + 1][y]:
        if x + 1 < len(xs) - 1:
            new_point = (x + 1, y)
            check_add(new_point)
        else:
            ans = "INF"
            break

print(ans)
pass

Submission status: Runtime Error

Solution code:
from collections import OrderedDict

N, M = map(int, input().split())
xs = set([0])
ys = set([0])

ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append([a, b, c])
    xs.add(a)
    xs.add(b)
    ys.add(c)
DEF = []
for _ in range(M):
    d, e, f = map(int, input().split())
    DEF.append([d, e, f])
    xs.add(d)
    ys.add(e)
    ys.add(f)

xs = list(xs)
xs.sort()
x_diff = [x1 - x0 for x1, x0 in zip(xs[1:], xs)]
xs = dict(zip(xs, range(len(xs))))
ys = list(ys)
ys.sort()
y_diff = [y1 - y0 for y1, y0 in zip(ys[1:], ys)]
ys = dict(zip(ys, range(len(ys))))


ABC_saku = [[True for _ in range(len(ys))] for _ in range(len(xs) - 1)]
for a, b, c in ABC:
    a = xs[a]
    b = xs[b]
    c = ys[c]
    for ab in range(a, b):
        ABC_saku[ab][c] = False
DEF_saku = [[True for _ in range(len(ys) - 1)] for _ in range(len(xs))]
for d, e, f in DEF:
    d = xs[d]
    e = ys[e]
    f = ys[f]
    for ef in range(e, f):
        DEF_saku[d][ef] = False

Q = [(xs[0], ys[0])]
visited = set(Q)
ans = 0


def check_add(new_point):
    if new_point not in visited:
        Q.append(new_point)
        visited.add(new_point)


while Q:
    x, y = Q.pop()
    ans += x_diff[x] * y_diff[y]
    # y--
    if ABC_saku[x][y]:
        if y - 1 >= 0:
            new_point = (x, y - 1)
            check_add(new_point)
        else:
            ans = "INF"
            break
    if ABC_saku[x][y + 1]:
        if y + 1 < len(ys) - 1:
            new_point = (x, y + 1)
            check_add(new_point)
        else:
            ans = "INF"
            break
    if DEF_saku[x][y]:
        if x - 1 >= 0:
            new_point = (x - 1, y)
            check_add(new_point)
        else:
            ans = "INF"
            break
    if DEF_saku[x + 1][y]:
        if x + 1 < len(xs) - 1:
            new_point = (x + 1, y)
            check_add(new_point)
        else:
            ans = "INF"
            break


print(ans)
pass

Submission status: Runtime Error

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

hor = []
for _ in range(N):
  a,b,c = map(int,input().split())
  hor.append((c,a,b))
  xtic.add(a)
  xtic.add(b)
  ytic.add(c)
  
ver = []
for _ in range(M):
  d,e,f = map(int,input().split())
  ver.append((d,e,f))
  xtic.add(d)
  ytic.add(e)
  ytic.add(f)

xtic = list(xtic)
ytic = list(ytic)
xtic.sort()
ytic.sort()
xdic = {xtic[i]:i+1 for i in range(len(xtic))}
ydic = {ytic[i]:i+1 for i in range(len(ytic))}

gyaku_xdic = {xdic[k]:k for k in xdic}
gyaku_ydic = {ydic[k]:k for k in ydic}

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

now = None
for c,a,b in hor:
  c,a,b = ydic[c],xdic[a],xdic[b]
  if c != now:
    hor_dic[c] = (1<<b) - (1<<a)
  else:
    hor_dic[c] |= (1<<b) - (1<<a)
  now = c

now = None
for d,e,f in ver:
  d,e,f = xdic[d],ydic[e],ydic[f]
  if d != now:
    ver_dic[d] = (1<<f) - (1<<e)
  else:
    ver_dic[d] |= (1<<f) - (1<<e)
  now = d

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      if hor_dic[y+1] & (1<<x):
        return False
      return True
    else:
      if hor_dic[y] & (1<<x):
        return False
      return True
  else:
    if dx > 0:
      if ver_dic[x+1] & (1<<y):
        return False
      return True
    else:
      if ver_dic[x] & (1<<y):
        return False
      return True
      
def calc_area(x,y):
  X = gyaku_xdic[x+1] - gyaku_xdic[x]
  Y = gyaku_ydic[y+1] - gyaku_ydic[y]
  return X*Y

offset = [(1,0),(0,1),(-1,0),(0,-1)]
x0 = bisect.bisect(xtic,0)
y0 = bisect.bisect(ytic,0)
W,H = len(xtic)+1 ,len(ytic)+1

gyaku_xdic[0] = -INF
gyaku_ydic[0] = -INF
gyaku_xdic[W] = INF
gyaku_ydic[H] = INF

hor_wall = [0] + [0 for i in range(1,H)]
ver_wall = [0] + [0 for i in range(1,W)]

for k in hor_dic:
  hor_wall[k] = hor_dic[k]
for k in ver_dic:
  ver_wall[k] = ver_dic[k]


d = [[0 for i in range(W)] for j in range(H)]
d[y0][x0] = 1
q = deque([(x0,y0)])

area = 0

while q:
  x,y = q.popleft()
  area += calc_area(x,y)
  if area == INF:
    print("INF")
    exit()
  for dx,dy in offset:
    nx,ny = x+dx,y+dy
    if 0<=nx<W and 0<=ny<H:
      if is_connected(x,y,dx,dy):
        if d[ny][nx] == 0:
          d[ny][nx] = 1
          q.append((nx,ny))

print(area)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

N, M = map(int, readline().split())
data = np.int64(read().split()).reshape(-1, 3)

A = data[:N, 0]
B = data[:N, 1]
C = data[:N, 2]
D = data[N:, 0]
E = data[N:, 1]
F = data[N:, 2]

X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
DX = X[1:] - X[:-1]
DY = Y[1:] - Y[:-1]

A, B, D = map(lambda x: np.searchsorted(X, x), (A, B, C))
C, E, F = map(lambda x: np.searchsorted(Y, x), (D, E, F))

H, W = len(X), len(Y)
N = H * W

@njit
def set_ng(A, B, C, D, E, F):
    ng = np.zeros((N, 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

@njit
def solve():
    ng = set_ng(A, B, C, D, E, F)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = [v0]
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return DX[x] * DY[y]

    def push(x):
        nonlocal ret
        stack.append(x)
        visited[x] = 1
        ret += area(x)

    push(v0)
    move = (1, -1, W, -W)
    while stack:
        v = stack.pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret

x = solve()
if x == 0:
    print('INF')
else:
    print(x)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from numba import njit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

INF = 10**9 + 1

N, M = map(int, readline().split())
data = np.int64(read().split()).reshape(-1, 3)

A = data[:N, 0]
B = data[:N, 1]
C = data[:N, 2]
D = data[N:, 0]
E = data[N:, 1]
F = data[N:, 2]

X = np.unique(np.concatenate([A, B, D, [0, -INF, INF]]))
Y = np.unique(np.concatenate([C, E, F, [0, -INF, INF]]))
DX = X[1:] - X[:-1]
DY = Y[1:] - Y[:-1]

A, B, C = map(lambda x: np.searchsorted(X, x), (A, B, C))
D, E, F = map(lambda x: np.searchsorted(Y, x), (D, E, F))

H, W = len(X), len(Y)
N = H * W

@njit
def set_ng(A, B, C, D, E, F):
    ng = np.zeros((N, 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

@njit
def solve():
    ng = set_ng(A, B, C, D, E, F)
    x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
    v0 = x0 * W + y0
    visited = np.zeros(N, np.bool_)
    visited[v0] = 1
    stack = [v0]
    ret = 0

    def area(x):
        x, y = divmod(x, W)
        return DX[x] * DY[y]

    def push(x):
        nonlocal ret
        stack.append(x)
        visited[x] = 1
        ret += area(x)

    push(v0)
    move = (1, -1, W, -W)
    while stack:
        v = stack.pop()
        for i in range(4):
            if ng[v][i]:
                continue
            w = v + move[i]
            if visited[w]:
                continue
            x, y = divmod(w, W)
            if x == 0 or x == H - 1 or y == 0 or y == W - 1:
                return 0
            push(w)
    return ret

x = solve()
if x == 0:
    print('INF')
else:
    print(x)