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] = [(a,b)]
  else:
    hor_dic[c].append((a,b))
  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] = [(e,f)]
  else:
    ver_dic[d].append((e,f))
  now = d

hor_wall = [-INF] + [k for k in hor_dic] + [INF]
ver_wall = [-INF] + [k for k in ver_dic] + [INF]

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      p = bisect.bisect_left(hor_wall,y+1)
      if hor_wall[p] != y+1:
        return True
      else:
        for l,r in hor_dic[y+1]:
          if l<=x<r:
            return False
        return True
    else:
      p = bisect.bisect_left(hor_wall,y)
      if hor_wall[p] != y:
        return True
      else:
        for l,r in hor_dic[y]:
          if l<=x<r:
            return False
        return True
  else:
    if dx > 0:
      p = bisect.bisect_left(ver_wall,x+1)
      if ver_wall[p] != x+1:
        return True
      else:
        for l,r in ver_dic[x+1]:
          if l<=y<r:
            return False
        return True
    else:
      p = bisect.bisect_left(ver_wall,x)
      if ver_wall[p] != x:
        return True
      else:
        for l,r in ver_dic[x]:
          if l<=y<r:
            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[H] = INF
gyaku_ydic[W] = INF

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:
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] = [(a,b)]
  else:
    hor_dic[c].append((a,b))
  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] = [(e,f)]
  else:
    ver_dic[d].append((e,f))
  now = d

hor_wall = [-INF] + [k for k in hor_dic] + [INF]
ver_wall = [-INF] + [k for k in ver_dic] + [INF]

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      p = bisect.bisect_left(hor_wall,y+1)
      if hor_wall[p] != y+1:
        return True
      else:
        for l,r in hor_dic[y+1]:
          if l<=x<r:
            return False
        return True
    else:
      p = bisect.bisect_left(hor_wall,y)
      if hor_wall[p] != y:
        return True
      else:
        for l,r in hor_dic[y]:
          if l<=x<r:
            return False
        return True
  else:
    if dx > 0:
      p = bisect.bisect_left(ver_wall,x+1)
      if ver_wall[p] != x+1:
        return True
      else:
        for l,r in ver_dic[x+1]:
          if l<=y<r:
            return False
        return True
    else:
      p = bisect.bisect_left(ver_wall,x)
      if ver_wall[p] != x:
        return True
      else:
        for l,r in ver_dic[x]:
          if l<=y<r:
            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[H] = INF
gyaku_ydic[W] = INF

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:
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] = [(a,b)]
  else:
    hor_dic[c].append((a,b))
  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] = [(e,f)]
  else:
    ver_dic[d].append((e,f))
  now = d

hor_wall = [-INF] + [k for k in hor_dic] + [INF]
ver_wall = [-INF] + [k for k in ver_dic] + [INF]

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      p = bisect.bisect_left(hor_wall,y+1)
      if hor_wall[p] != y+1:
        return True
      else:
        for l,r in hor_dic[y+1]:
          if l<=x<r:
            return False
        return True
    else:
      p = bisect.bisect_left(hor_wall,y)
      if hor_wall[p] != y:
        return True
      else:
        for l,r in hor_dic[y]:
          if l<=x<r:
            return False
        return True
  else:
    if dx > 0:
      p = bisect.bisect_left(ver_wall,x+1)
      if ver_wall[p] != x+1:
        return True
      else:
        for l,r in ver_dic[x+1]:
          if l<=y<r:
            return False
        return True
    else:
      p = bisect.bisect_left(ver_wall,x)
      if ver_wall[p] != x:
        return True
      else:
        for l,r in ver_dic[x]:
          if l<=y<r:
            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[H] = INF
gyaku_ydic[W] = INF

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()
  nd = d[y][x]+1
  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[nx][ny] == 0:
          d[nx][ny] = 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] = [(a,b)]
  else:
    hor_dic[c].append((a,b))
  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] = [(e,f)]
  else:
    ver_dic[d].append((e,f))
  now = d

hor_wall = [-INF] + [k for k in hor_dic] + [INF]
ver_wall = [-INF] + [k for k in ver_dic] + [INF]

def is_connected(x,y,dx,dy):
  if dx == 0:
    if dy > 0:
      p = bisect.bisect_left(hor_wall,y+1)
      if hor_wall[p] != y+1:
        return True
      else:
        for l,r in hor_dic[y+1]:
          if l<=x<r:
            return False
        return True
    else:
      p = bisect.bisect_left(hor_wall,y)
      if hor_wall[p] != y:
        return True
      else:
        for l,r in hor_dic[y]:
          if l<=x<r:
            return False
        return True
  else:
    if dx > 0:
      p = bisect.bisect_left(ver_wall,x+1)
      if ver_wall[p] != x+1:
        return True
      else:
        for l,r in ver_dic[x+1]:
          if l<=y<r:
            return False
        return True
    else:
      p = bisect.bisect_left(ver_wall,x)
      if ver_wall[p] != x:
        return True
      else:
        for l,r in hor_dic[x]:
          if l<=y<r:
            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[H] = INF
gyaku_ydic[W] = INF

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()
  nd = d[y][x]+1
  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[nx][ny] == 0:
          d[nx][ny] = 1
          q.append((nx,ny))

print(area)

Submission status: Runtime Error

Solution code:
import sys
from numba import njit

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

ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2

banned_up = [0] * (row * col)
banned_down = [0] * (row * col)
banned_left = [0] * (row * col)
banned_right = [0] * (row * col)


@njit
def register_ver_lines(a, b, c):
    global banned_left, banned_right
    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):
    global banned_up, banned_down
    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


for a, b, c in ver_lines:
    register_ver_lines(a, b, c)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f)

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]

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

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:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])


ans = 0
for i in range(col):
    ri = i * row
    for j in range(row):
        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)
print(ans)

Submission status: Runtime Error

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

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

nx = len(X)
ny = len(Y)
nxy = nx * ny
E = {(x,y):{(X[i-1],y),(X[i+1],y),(x,Y[j-1]),(x,Y[j+1])} for i,x in enumerate(X[1:-1],1) for j,y in enumerate(Y[1:-1],1)}

x0 = X[0]
x1 = X[-1]
for y in Y:
    E[(x0,y)] = set()
    E[(x1,y)] = set()
y0 = Y[0]
y1 = Y[-1]
for x in X:
    E[(x,y0)] = set()
    E[(x,y1)] = set()

for a,b,c in ABC:
    y = Y[Ydict[c]-1]
    for x in X[Xdict[a]:Xdict[b]]:
        E[(x,c)].discard((x,y))
        E[(x,y)].discard((x,c))

for d,e,f in DEF:
    x = X[Xdict[d]-1]
    for y in Y[Ydict[e]:Ydict[f]]:
        E[(d,y)].discard((x,y))
        E[(x,y)].discard((d,y))

dX = {x:x2-x for x,x2 in zip(X[:-1],X[1:])}
dY = {y:y2-y for y,y2 in zip(Y[:-1],Y[1:])}
burst = False
ans = 0
done = {(0,0)}
q = [(0,0)]
while q:
    qq = []
    for i in q:
        di = d[i]
        for j in E[i]:
            if j in done:continue
            done.add(j)
            x,y = j
            if abs(x)==INF or abs(y)==INF:
                burst = True
                ans = "INF"
                qq = []
                break
            ans += dX[x]*dY[y]
            qq.append(j)
        if burst:break
    q = qq

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from itertools import chain, accumulate

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
banned_up_ij = [[0] * row for _ in range(col)]
banned_down_ij = [[0] * row for _ in range(col)]
banned_left_ij = [[0] * col for _ in range(row)]
banned_right_ij = [[0] * col for _ in range(row)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    banned_left_ij[j][ai] += 1
    banned_left_ij[j][bi] -= 1
    banned_right_ij[j - 1][ai] += 1
    banned_right_ij[j - 1][bi] -= 1
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up_ij[i][ej] += 1
    banned_up_ij[i][fj] -= 1
    banned_down_ij[i - 1][ej] += 1
    banned_down_ij[i - 1][fj] -= 1

banned_up = list(chain.from_iterable(map(accumulate, banned_up_ij)))
banned_down = list(chain.from_iterable(map(accumulate, banned_down_ij)))
banned_left = list(chain.from_iterable(zip(*map(accumulate, banned_left_ij))))
banned_right = list(chain.from_iterable(zip(*map(accumulate, banned_right_ij))))

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-1] * row

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

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:
        print('INF')
        exit()
    enable[c] = 1
    for dc, banned in moves:
        if banned[c]:
            continue
        nc = c + dc
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        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)
print(ans)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
import sys
import bisect
from itertools import accumulate
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LIR(n):
    return [LI() for i in range(n)]

def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    fx = [-float("inf"),float("inf")]
    fy = [-float("inf"),float("inf")]
    for i in range(n):
        p[i][0] *= 10
        p[i][1] *= 10
        p[i][2] *= 10
        a,b,c = p[i]
        fy.append(a)
        fy.append(b)
        fy.append(b+1)
        fx.append(c)
        fx.append(c+1)

    for i in range(m):
        q[i][0] *= 10
        q[i][1] *= 10
        q[i][2] *= 10
        d,e,f = q[i]
        fy.append(d)
        fy.append(d+1)
        fx.append(e)
        fx.append(f)
        fx.append(f+1)
    fy = list(set(fy))
    fx = list(set(fx))
    fy.sort()
    fx.sort()
    h = len(fy)+1
    w = len(fx)+1
    s = [0]*h*w
    for a,b,c in p:
        ai = bisect.bisect_left(fy,a)
        bi = bisect.bisect_left(fy,b)
        ci = bisect.bisect_left(fx,c)
        ka = ai*w
        kb = (bi+1)*w
        s[ka+ci] += 1
        s[kb+ci] -= 1
        s[ka+ci+1] -= 1
        s[kb+ci+1] += 1
    for d,e,f in q:
        di = bisect.bisect_left(fy,d)
        ei = bisect.bisect_left(fx,e)
        fi = bisect.bisect_left(fx,f)
        kd = di*w
        kdd = kd+w
        s[kd+ei] += 1
        s[kd+fi+1] -= 1
        s[kdd+ei] -= 1
        s[kdd+fi+1] += 1
    for i in range(h):
        for j in range(len(fx)):
            ind = i*w+j
            s[ind+1] += s[ind]
    for i in range(len(fy)):
        for j in range(w):
            ind = i*w+j
            s[ind+w] += s[ind]
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    fx[1] //= 10
    fy[1] //= 10
    for i in range(1,nw-1):
        fx[i+1] = fx[i+1]//10
        fx[i] = fx[i+1]-fx[i]
    for i in range(1,nh-1):
        fy[i+1] = fy[i+1]//10
        fy[i] = fy[i+1]-fy[i]
    fx[0] = -fx[0]
    fy[0] = -fy[0]
    ans = fx[sx]*fy[sy]
    q = [(sy,sx)]
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    while q:
        y,x = q.pop()
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = ny*nw+nx
                if f[ind] and not s[ind+2*ny]:
                    k = fx[nx]*fy[ny]
                    if k >= float("inf"):
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import sys

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
walls = [set() for _ in range(row * col)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    for ri in range(ai * row, bi * row, row):
        walls[ri + j].add(ri + j - 1)
        walls[ri + j - 1].add(ri + j)
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    ri0 = row * i
    ri1 = row * (i - 1)
    for j in range(ej, fj):
        walls[ri0 + j].add(ri1 + j)
        walls[ri1 + j].add(ri0 + j)

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for ri in range(0, row * col, row):
    enable[ri] = -1
    enable[ri + row - 1] = -1
ri_last = row * (col - 1)
for j in range(row):
    enable[j] = -1
    enable[ri_last + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        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)
print(ans)

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.array(read().split(), np.int64)
A = data[::3]
B = data[1::3]
C = data[2::3]

D = A[N:]
E = B[N:]
F = C[N:]
A = A[:N]
B = B[:N]
C = C[:N]

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 = np.searchsorted(X, A)
B = np.searchsorted(X, B)
C = np.searchsorted(Y, C)
D = np.searchsorted(X, D)
E = np.searchsorted(Y, E)
F = np.searchsorted(Y, F)

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

@njit
def set_ng(A, B, C, D, E, F):
    p = 0
    head = np.full(N, -1, np.int32)
    ng = np.empty(4 * N, np.int32)
    nxt = np.empty(4 * N, np.int32)

    def add(v, w):
        nonlocal p
        nxt[p] = head[v]
        head[v] = p
        ng[p] = w
        p += 1

    for i in range(len(A)):
        a, b, c = A[i], B[i], C[i]
        for x in range(a, b):
            v = x * W + c
            add(v, v - 1)
            add(v - 1, v)
    for i in range(len(D)):
        d, e, f = D[i], E[i], F[i]
        for y in range(e, f):
            v = d * W + y
            add(v, v - W)
            add(v - W, v)
    return head, ng[:p], nxt[:p]

head, ng, nxt = set_ng(A, B, C, D, E, F)

@njit
def next_w(head, ng, nxt, v):
    p = head[v]
    V = [v - W, v + W, v - 1, v + 1]
    while p != -1:
        V.discard(ng[p])
        p = nxt[p]
    return V

x0, y0 = np.searchsorted(X, 0), np.searchsorted(Y, 0)
v0 = x0 * W + y0

@njit
def solve():
    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 DX[x] * 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)
    while p:
        v = pop()
        for w in next_w(head, ng, nxt, v):
            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

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())
ver_lines = []
hor_lines = []
x_list = set()
y_list = set()
n3 = n * 3
for a, b, c in zip(abcdef[0:n3:3], abcdef[1:n3:3], abcdef[2:n3:3]):
    y_list.add(a)
    y_list.add(b)
    x_list.add(c)
    ver_lines.append((a, b, c))

for d, e, f in zip(abcdef[n3 + 0::3], abcdef[n3 + 1::3], abcdef[n3 + 2::3]):
    y_list.add(d)
    x_list.add(e)
    x_list.add(f)
    hor_lines.append((d, e, f))
x_list.add(0)
y_list.add(0)

x_list = sorted(x_list)
y_list = sorted(y_list)
x_dict = {x: i for i, x in enumerate(x_list, start=1)}
y_dict = {y: i for i, y in enumerate(y_list, start=1)}
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
walls = [set() for _ in range(row * col)]
for a, b, c in ver_lines:
    if a > b:
        a, b = b, a
    ai = y_dict[a]
    bi = y_dict[b]
    j = x_dict[c]
    for i in range(ai, bi):
        walls[row * i + j].add(row * i + j - 1)
        walls[row * i + j - 1].add(row * i + j)
for d, e, f in hor_lines:
    if e > f:
        e, f = f, e
    i = y_dict[d]
    ej = x_dict[e]
    fj = x_dict[f]
    for j in range(ej, fj):
        walls[row * i + j].add(row * (i - 1) + j)
        walls[row * (i - 1) + j].add(row * i + j)

# for i in range(col):
#     print(walls[i * row:(i + 1) * row])

s = row * y_dict[0] + x_dict[0]
enable = [0] * (row * col)
for i in range(col):
    enable[row * i] = -1
    enable[row * (i + 1) - 1] = -1
for j in range(row):
    enable[j] = -1
    enable[row * (col - 1) + j] = -1

# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

q = [s]
moves = [-row, -1, 1, row]
while q:
    c = q.pop()
    if enable[c] == 1:
        continue
    elif enable[c] == -1:
        print('INF')
        exit()
    enable[c] = 1
    for dc in moves:
        nc = c + dc
        if nc in walls[c]:
            continue
        if enable[nc] == 1:
            continue
        q.append(nc)
# for i in range(col):
#     print(enable[i * row:(i + 1) * row])

ans = 0
for i in range(row):
    ri = i * row
    for j in range(col):
        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)
print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(n)]
DEF = [list(map(int,input().split())) for i in range(m)]
xset = set([0])
yset = set([0])
for a,b,c in ABC:
  xset.add(a)
  xset.add(b)
  yset.add(c)
for d,e,f in DEF:
  xset.add(d)
  yset.add(e)
  yset.add(f)
xls = list(xset)
yls = list(yset)
xls.sort()
yls.sort()
dcx = {xls[i]:i for i in range(len(xls))}
dcy = {yls[i]:i for i in range(len(yls))}
xmax = len(xls)+1
ymax = len(yls)+1
banned = set()
for a,b,c in ABC:
  da,db,dc = dcx[a],dcx[b],dcy[c]
  for i in range(da,db):
    banned.add((i*xmax+dc-1,i*xmax+dc))
for d,e,f in DEF:
  dd,de,df = dcx[d],dcy[e],dcy[f]
  for i in range(de,df):
    banned.add(((dd-1)*xmax+i,dd*xmax+i))
sx,sy = dcx[0],dcy[0]
graph = [[] for i in range((xmax*ymax+1))]
for i in range(xmax):
  for j in range(ymax):
    for dx,dy in (1,0),(0,1),(-1,0),(0,-1):
      if 0 <= i+dx <= xmax-1 and 0 <= j+dy <= ymax-1:
        nx,ny = i+dx,j+dy
        if (i*xmax+j,nx*xmax+ny) not in banned and (nx*xmax+ny,i*xmax+j) not in banned:
          graph[i*xmax+j].append(nx*xmax+ny)

grid = [0 for i in range(xmax*ymax+1)]
stack = [sx*xmax+sy]
while stack:
  x = stack.pop()
  if grid[x] == 0:
    grid[x] = 1
  for y in graph[x]:
    if grid[y] == 0:
      grid[y] = 1
      stack.append(y)
if grid.count(1) >= xmax*ymax:
  print("INF")
  exit()

ans = 0
for i in range(xmax):
  for j in range(ymax):
    if grid[i*xmax+j] == 1:
      ans += (xls[i+1]-xls[i])*(yls[j+1]-yls[j])
print(ans)