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:
#!/usr/bin/python3
from collections import defaultdict

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

xti = {}
yti = {}
xti[0] = -1
yti[0] = -1

lvs = []
for i in range(n):
    (a, b, c) = map(int, input().split())
    yti[a] = -1
    yti[b] = -1   
    xti[c] = -1
    lvs.append((a, b, c))

lhs = []
for i in range(m):
    (d, e, f) = map(int, input().split())
    yti[d] = -1
    xti[e] = -1   
    xti[f] = -1
    lhs.append((d, e, f))

xs = sorted(xti.keys())
for (i, x) in enumerate(xs):
    xti[x] = i

ys = sorted(yti.keys())
for (i, y) in enumerate(ys):
    yti[y] = i

fi = [ [ 0 for x in range(len(xs) * 2) ] for y in range(len(ys) * 2) ]

for (a, b, c) in lvs:
    x = xti[c] * 2
    for y in range(yti[a] * 2, yti[b] * 2 + 1):
        fi[y][x] = -1

for (d, e, f) in lhs:
    y = yti[d] * 2
    for x in range(xti[e] * 2, xti[f] * 2 + 1):
        fi[y][x] = -1

q = [(xti[0] * 2, yti[0] * 2)]

while q:
    (cx, cy) = q.pop(-1)
    if cx < 0 or cx >= len(xs) * 2 or cy < 0 or cy >= len(ys) * 2:
        continue
    
    if fi[cy][cx] != 0:
        continue

    fi[cy][cx] = 1
    q.append((cx, cy - 1))
    q.append((cx + 1, cy))
    q.append((cx, cy + 1))
    q.append((cx - 1, cy))

#for xl in fi:
#    print(xl)

s = 0
for y in range(len(ys) * 2):
    for x in range(len(xs) * 2):
        if fi[y][x] <= 0:
            continue
        if x == 0 or x == len(xs) * 2 - 1 or y == 0 or y == len(ys) * 2 - 1:
            print('INF')
            exit()

        s += (xs[(x + 1) // 2] - xs[x // 2]) * (ys[(y + 1) // 2] - ys[y // 2])

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
  n, m = nm()
  ver = [tuple(nm()) for _ in range(n)]
  hor = [tuple(nm()) for _ in range(m)]
  xli = [x[0] for x in ver+hor] + [0]
  xli += [x[1] for x in ver]
  yli = [x[2] for x in hor+ver] + [0] 
  yli += [x[1] for x in hor]
  xli = sorted(set(xli))
  yli = sorted(set(yli))
  xdic = {x:i for i, x in enumerate(xli)}
  ydic = {x:i for i, x in enumerate(yli)}
  H = len(yli)*2 + 1
  W = len(xli)*2 + 1
  G = [[0]*W for _ in range(H)]
  for a, b, c in ver:
    a = xdic[a]; b = xdic[b]
    c = ydic[c]
    if a > b:
      a, b = b, a
    for i in range(2*a+1, 2*b+2):
      G[2*c+1][i] = 1
  for a, b, c in hor:
    a = xdic[a]
    b = ydic[b]; c = ydic[c]
    if b > c:
      b, c = c, b
    for i in range(2*b+1, 2*c+2):
      G[i][2*a+1] = 1
  # print(xli)
  # print(yli)
  # prn(G)
  q = deque()
  sx = xdic[0]*2+1; sy = ydic[0]*2+1
  G[sy][sx] = 1
  q.append((sy, sx))
  ans = 0
  while q:
    y, x = q.popleft()
    for ny, nx in ((y+1, x), (y-1, x), (y, x+1), (y, x-1)):
      if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == 0:
        G[ny][nx] = -1
        q.append((ny, nx))
        if ny == 0 or ny == H-1 or nx == 0 or nx == W-1:
          print('INF')
          return
        if ny % 2 == nx % 2 == 0:
          ans += (yli[ny//2] - yli[ny//2-1]) * (xli[nx//2] - xli[nx//2-1])
  print(ans)
  return

solve()

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
NS = []
X_num = set()
EW = []
Y_num = set()

INF = 10 ** 36

X_num.add(INF)
X_num.add(0)
X_num.add(-INF)
Y_num.add(INF)
Y_num.add(0)
Y_num.add(-INF)

cant_inc_x = [[0] * 3300 for i in range(3300)]
cant_inc_y = [[0] * 3300 for i in range(3300)]

for i in range(n):
    a, b, c = map(int, input().split())
    X_num.add(a)
    X_num.add(b)
    Y_num.add(c)
    NS.append((a,b,c))

for i in range(m):
    d, e, f = map(int, input().split())
    X_num.add(d)
    Y_num.add(e)
    Y_num.add(f)
    EW.append((d,e,f))

X_dict = {}
Y_dict = {}

X_num = sorted(list(X_num))
Y_num = sorted(list(Y_num))


for xp, x in enumerate(X_num):
    X_dict[x] = xp
    xp += 1

for yp, y in enumerate(Y_num):
    Y_dict[y] = yp
    yp += 1

#(a,b) (a,c)
for a, b, c in NS:
    a = X_dict[a]
    b = X_dict[b]
    c = Y_dict[c]
    cant_inc_y[a][c-1] += 1
    cant_inc_y[b][c-1] -= 1
    

#(d,e) (d,f)
for d, e, f in EW:
    d = X_dict[d]
    e = Y_dict[e]
    f = Y_dict[f]
    cant_inc_x[d-1][e] += 1
    cant_inc_x[d-1][f] -= 1

for i in range(3003):
    for j in range(3003):
        if(j):cant_inc_x[i][j] += cant_inc_x[i][j-1]
        if(i):cant_inc_y[i][j] += cant_inc_y[i-1][j]

from collections import deque
q = deque()
q.append((X_dict[0], Y_dict[0]))
done = [[False] * 3003 for _ in range(3003)]

ans = 0


while len(q) != 0:
    (nowx, nowy) = q.pop()
    if nowx < 0 or nowy < 0 or nowx+1 >= len(X_num) or nowy+1 >= len(Y_num):
        continue
    if done[nowx][nowy]:continue
    done[nowx][nowy] = True
    ans += (X_num[nowx+1] - X_num[nowx]) * (Y_num[nowy+1] - Y_num[nowy])
    if ans > INF:
      print("INF")
      exit()
    if cant_inc_x[nowx][nowy] == 0:q.append((nowx+1, nowy))
    if cant_inc_y[nowx][nowy] == 0:q.append((nowx, nowy+1))
    if nowx > 0 and cant_inc_x[nowx-1][nowy] == 0:q.append((nowx-1, nowy))
    if nowy > 0 and cant_inc_y[nowx][nowy-1] == 0:q.append((nowx, nowy-1))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
NS = []
X_num = set()
EW = []
Y_num = set()

INF = 10 ** 36

X_num.add(INF)
X_num.add(0)
X_num.add(-INF)
Y_num.add(INF)
Y_num.add(0)
Y_num.add(-INF)

cant_inc_x = [[0] * 3300 for i in range(3300)]
cant_inc_y = [[0] * 3300 for i in range(3300)]

for i in range(n):
    a, b, c = map(int, input().split())
    X_num.add(a)
    X_num.add(b)
    Y_num.add(c)
    NS.append((a,b,c))

for i in range(m):
    d, e, f = map(int, input().split())
    X_num.add(d)
    Y_num.add(e)
    Y_num.add(f)
    EW.append((d,e,f))

X_dict = {}
Y_dict = {}

X_num = sorted(list(X_num))
Y_num = sorted(list(Y_num))


for xp, x in enumerate(X_num):
    X_dict[x] = xp
    xp += 1

for yp, y in enumerate(Y_num):
    Y_dict[y] = yp
    yp += 1

#(a,b) (a,c)
for a, b, c in NS:
    a = X_dict[a]
    b = X_dict[b]
    c = Y_dict[c]
    cant_inc_y[a][c-1] += 1
    cant_inc_y[b][c-1] -= 1
    

#(d,e) (d,f)
for d, e, f in EW:
    d = X_dict[d]
    e = Y_dict[e]
    f = Y_dict[f]
    cant_inc_x[d-1][e] += 1
    cant_inc_x[d-1][f] -= 1

for i in range(3003):
    for j in range(3003):
        if(j):cant_inc_x[i][j] += cant_inc_x[i][j-1]
        if(i):cant_inc_y[i][j] += cant_inc_y[i-1][j]

from collections import deque
q = deque()
q.append((X_dict[0], Y_dict[0]))
done = [[False] * 3003 for _ in range(3003)]

ans = 0


while len(q) != 0:
    (nowx, nowy) = q.pop()
    if nowx < 0 or nowy < 0 or nowx+1 >= len(X_num) or nowy+1 >= len(Y_num):
        continue
    if done[nowx][nowy]:continue
    done[nowx][nowy] = True
    ans += (X_num[nowx+1] - X_num[nowx]) * (Y_num[nowy+1] - Y_num[nowy])
    if cant_inc_x[nowx][nowy] == 0:q.append((nowx+1, nowy))
    if cant_inc_y[nowx][nowy] == 0:q.append((nowx, nowy+1))
    if nowx > 0 and cant_inc_x[nowx-1][nowy] == 0:q.append((nowx-1, nowy))
    if nowy > 0 and cant_inc_y[nowx][nowy-1] == 0:q.append((nowx, nowy-1))

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0,-INF,INF}
ytic = {0,-INF,INF}

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] & (1<<x):
        return False
      return True
    else:
      if hor_wall[y] & (1<<x):
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1] & (1<<y):
        return False
      return True
    else:
      if ver_wall[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

hor_wall = [0 for i in range(H)]
ver_wall = [0 for i in range(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.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: Time Limit Exceeded

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0,-INF,INF}
ytic = {0,-INF,INF}

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] & (1<<x):
        return False
      return True
    else:
      if hor_wall[y] & (1<<x):
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1] & (1<<y):
        return False
      return True
    else:
      if ver_wall[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.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: Time Limit Exceeded

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] & (1<<x):
        return False
      return True
    else:
      if hor_wall[y] & (1<<x):
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1] & (1<<y):
        return False
      return True
    else:
      if ver_wall[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.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: Time Limit Exceeded

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(q, enable, x_list, y_list, moves):
    c = q.pop()

    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
        q.append(nc)
        enable[nc] = 1

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


@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)
    enable[s] = 1
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]

    q = [s]
    ans = 0
    while q:
        ret = dfs(q, enable, x_list, y_list, moves)
        if ret == -1:
            return -1
        ans += ret

    return ans


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: Time Limit Exceeded

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


if xs[0] < len(x_diff) and ys[0] < len(y_diff):
    while Q:
        # x, y = Q.popleft()
        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(y_diff):
                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(x_diff):
                new_point = (x + 1, y)
                check_add(new_point)
            else:
                ans = "INF"
                break
else:
    ans = "INF"


print(ans)
pass

Submission status: Time Limit Exceeded

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

    q = [s]
    banned_right = np.roll(banned_left, -1)
    banned_down = np.roll(banned_up, -row)
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    ans = 0
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            return -1
        enable[c] = 1

        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)

        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    return ans


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 = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_left)
print(ans if ans != -1 else 'INF')

Submission status: Time Limit Exceeded

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

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_left), (row, banned_up)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            enable[0] = -2
            return enable
        enable[c] = 1
        for dc, banned in moves:
            nc = c + dc
            if dc < 0:
                if banned[c]:
                    continue
            else:
                if banned[nc]:
                    continue
            if enable[nc] == 1:
                continue
            q.append(nc)
    return enable


@njit
def get_ans(enable, x_list, y_list):
    global col, row
    ans = 0
    for i in range(1, col - 1):
        ri = i * row
        for j in range(1, row - 1):
            if enable[ri + j] != 1:
                continue
            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


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)

enable = dfs(x_dict, y_dict, banned_up, banned_left)
if enable[0] == -2:
    print('INF')
else:
    ans = get_ans(enable, x_list, y_list)
    print(ans)

Submission status: Time Limit Exceeded

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_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, 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
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, 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
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    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_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    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)

    q = [s]
    moves = [(-row, banned_up), (-1, banned_left), (1, banned_right), (row, banned_down)]
    while q:
        c = q.pop()
        if enable[c] == 1:
            continue
        elif enable[c] == -1:
            enable[0] = -2
            return enable
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)
    return enable


@njit
def get_ans(enable, x_list, y_list):
    global col, row
    ans = 0
    for i in range(1, col - 1):
        ri = i * row
        for j in range(1, row - 1):
            if enable[ri + j] != 1:
                continue
            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


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

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

enable = dfs(x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
if enable[0] == -2:
    print('INF')
else:
    ans = get_ans(enable, x_list, y_list)
    print(ans)