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:
def main():
n,m=map(int,input().split())
  def hashh(x1,y1,x2,y2):
      if x1>x2:
          x1,y1,x2,y2=x2,y2,x1,y1
      if x1==x2 and y1>y2:
          x1,y1,x2,y2=x2,y2,x1,y1
      return x1*27000000000+y1*9000000+x2*3000+y2
  abc_l=[list(map(int,input().split()))for _ in range(n)]
  def_l=[list(map(int,input().split()))for _ in range(m)]
  x=[0]
  y=[0]
  for a,b,c in abc_l:
      x.append(a)
      x.append(b)
      y.append(c)
  for d,e,f in def_l:
      x.append(d)
      y.append(e)
      y.append(f)
  x=sorted(list(set(x)))
  y=sorted(list(set(y)))
  h=len(x)+1
  w=len(y)+1
  dicx={}
  dicy={}
  for i in range(len(x)):dicx[x[i]]=i
  for i in range(len(y)):dicy[y[i]]=i
  grid=[[0]*w for _ in range(h)]
  dont=set()
  for a,b,c in abc_l:
      for i in range(dicx[a],dicx[b]):
          dont.add(hashh(i+1,dicy[c],i+1,dicy[c]+1))
  for d,e,f in def_l:
      for j in range(dicy[e],dicy[f]):
          dont.add(hashh(dicx[d],j+1,dicx[d]+1,j+1))
  if dicx[0]==0 or dicx[0]==h-1 or dicy[0]==0 or dicy[0]==w-1:
      exit(print("INF"))
  stack=[(dicx[0],dicy[0])]
  grid[dicx[0]][dicy[0]]=1
  ans=0
  for sx,sy in stack:
      ans+=(x[sx]-x[sx-1])*(y[sy]-y[sy-1])
      for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)):
          tx=sx+dx
          ty=sy+dy
          if grid[tx][ty]:continue
          if hashh(sx,sy,tx,ty) in dont:continue
          if tx==0 or tx==h-1 or ty==0 or ty==w-1:
              exit(print("INF"))
          grid[tx][ty]=1
          stack.append((tx,ty))
  print(ans)
main()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
def hashh(x1,y1,x2,y2):
    if x1>x2:
        x1,y1,x2,y2=x2,y2,x1,y1
    if x1==x2 and y1>y2:
        x1,y1,x2,y2=x2,y2,x1,y1
    return x1*1021147343+y1*1014049+x2*1007+y2
abc_l=[list(map(int,input().split()))for _ in range(n)]
def_l=[list(map(int,input().split()))for _ in range(m)]
x=[0]
y=[0]
for a,b,c in abc_l:
    x.append(a)
    x.append(b)
    y.append(c)
for d,e,f in def_l:
    x.append(d)
    y.append(e)
    y.append(f)
x=sorted(list(set(x)))
y=sorted(list(set(y)))
h=len(x)+1
w=len(y)+1
dicx={}
dicy={}
for i in range(len(x)):dicx[x[i]]=i
for i in range(len(y)):dicy[y[i]]=i
grid=[[0]*w for _ in range(h)]
dont=set()
for a,b,c in abc_l:
    for i in range(dicx[a],dicx[b]):
        dont.add(hashh(i+1,dicy[c],i+1,dicy[c]+1))
for d,e,f in def_l:
    for j in range(dicy[e],dicy[f]):
        dont.add(hashh(dicx[d],j+1,dicx[d]+1,j+1))
if dicx[0]==0 or dicx[0]==h-1 or dicy[0]==0 or dicy[0]==w-1:
    exit(print("INF"))
stack=[(dicx[0],dicy[0])]
grid[dicx[0]][dicy[0]]=1
ans=(x[dicx[0]+1]-x[dicx[0]])*(y[dicy[0]+1]-y[dicy[0]])
for sx,sy in stack:
    for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)):
        tx=sx+dx
        ty=sy+dy
        if grid[tx][ty]:continue
        if hashh(sx,sy,tx,ty) in dont:continue
        if tx==0 or tx==h-1 or ty==0 or ty==w-1:
            exit(print("INF"))
        grid[tx][ty]=1
        ans+=(x[tx+1]-x[tx])*(y[ty+1]-y[ty])
        stack.append((tx,ty))
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import numpy as np

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

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

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

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

from collections import deque

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

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

tate = []
yoko = []

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

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

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

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

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

G = [0] * (H * W)

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

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

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

stack = [Cx[0] * W + Cy[0]]

stack = deque(stack)

visited = [0] * (H * W)
visited[Cx[0] * W + Cy[0]] = 1
res = 0

while stack:
    z = stack.popleft()
    nz = z + 1
    if G[nz] != -1 and not visited[nz]:
        nx = nz // W
        if nx == H - 1:
            print('INF')
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    nz = z - 1
    if G[nz] != -1 and not visited[nz]:
        nx = nz // W
        if nx == 0:
            print('INF')
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    nz = z + W
    if G[nz] != -1 and not visited[nz]:
        ny = nz % W
        if ny == W - 1:
            print('INF')
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    nz = z - W
    if G[nz] != -1 and not visited[nz]:
        ny = nz % W
        if ny == 0:
            print('INF')
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
else:
    print(res)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = list(map(int,input().split()))
CAB = [[0] * 3 for _ in range(n)]
DEF = [[0] * 3 for _ in range(m)]
wl, wh = 0,0
hl, hh = 0,0
for i in range(n):
  a,b,c = list(map(int,input().split()))
  if a > b:
    CAB[i][1] = b -1
    CAB[i][2] = a -1
  else:
    CAB[i][1] = a -1
    CAB[i][2] = b -1
  CAB[i][0] = c -1 
  wl = min(wl, c -1)
  wh = max(wh, c -1)
for i in range(m):
  d,e,f = list(map(int,input().split()))
  if e > f:
    DEF[i][1] = F -1
    DEF[i][2] = e -1
  else:
    DEF[i][1] = e -1
    DEF[i][2] = f -1
  DEF[i][0] = d -1
  hl = min(hl, d -1)
  hh = max(hh, d -1)

w = (wh - wl) * 2 +3
h = (hh - hl) * 2 +3

fen = [[0] * w for _ in range(h)]
dp= [[0] * w for _ in range(h)]

for i in range(n):
  a = (CAB[i][1] - hl) *2 +1
  b = (CAB[i][2] - hl) *2 +1
  c = (CAB[i][0] - wl) *2 +1
  b = min(b, h-1)
  for j in range(a,b+1):
    fen[j][c] = 1
for i in range(m):
  e = (DEF[i][1] - wl) *2 +1
  f = (DEF[i][2] - wl) *2 +1
  d = (DEF[i][0] - hl) *2 +1
  f = min(f, w-1)
  for j in range(e,f+1):
    fen[d][j] = 1

que = deque([[-hl*2, -wl*2]])
dx = [2,0,-2,0]
dy = [0,2,0,-2]

fen[-hl*2][-wl*2] = 2
ans = 0
while que:
  x,y = que.popleft()
  for i in range(4):
    dxi = dx[i]
    dyi = dy[i]
    nx = x + dxi
    ny = y + dyi
    fx = x + dxi//2
    fy = y + dyi//2
    if 0 <= nx and nx <= w and 0 <= ny and ny <= h:
      nf = fen[fy][fx]
      nd = dp[ny][nx]
      if (nx == 0 or nx == w or ny == 0 or ny == h) and nf != 1:
        exit(print('INF'))
      if nd == 0 and nf != 1:
        dp[ny][nx] = 1
        ans += 1
        que.append([ny,nx])

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = list(map(int, input().split()))
c_ab = []
d_ef = []
ab = deque([])
ef = deque([])
wl = 0
wh = 0
hl = 0
hh = 0
for i in range(n):
  A,B,C= list(map(int, input().split()))
  A += A
  B += B
  C += C
  if A > B:
    t = A
    A = B
    B = t
  c_ab.append([C,A,B])
  wl = min(wl,C)
  wh = max(wh,C)

for i in range(m):
  D,E,F = list(map(int, input().split())) 
  D += D
  E += E
  F += F
  if E > F:
    t = E
    E = F
    F = t
  d_ef.append([D,E,F])
  hl = min(hl,D)
  hh = max(hh,D)

if wl > 0 or wh < 0 or hl > 0 or hh < 0:exit(print('INF'))  

wd = -wl+1
hd = -hl+1
wl = 1
wh += wd+1
hl = 1
hh += hd+1

map = [[0]*(wh+1) for _ in range(hh+1)]

for c,a,b in c_ab:
  a += hd
  b += hd
  c += wd
  if a < hl: a = hl
  if b > hh: b = hh
  for i in range(a,b+1):
    map[i][c] = 1

for d,e,f in d_ef:
  d += hd
  e += wd
  f += wd
  if e < wl: e = wl
  if f > wh: f = wh
  for i in range(e,f+1):
    map[d][i] = 1

dx = [2,0,-2,0]
dy = [0,2,0,-2]
map[hd+1][wd+1] = 9
que = deque([[wd+1,hd+1]])
cnt = 1
while que:
  x,y = que.popleft()
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    mx = x + dx[i]//2
    my = y + dy[i]//2
    
    if 0 < nx and nx <= wh+1 and 0 < ny and ny <= hh+1:
      if (nx == 1 and map[my][mx] != 1) or (ny == hh+1 and map[my][mx] != 1) or \
          (nx == wh+1 and map[my][mx] != 1) or (nx == hh+1 and map[my][mx] != 1):
        exit(print('INF'))
      elif map[ny][nx] == 0 and map[my][mx] != 1:
        map[ny][nx] = 2
        cnt += 1
        que.append([nx,ny])

print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = list(map(int, input().split()))
c_ab = []
d_ef = []
ab = deque([])
ef = deque([])
for i in range(n):
  A,B,C= list(map(int, input().split()))
  if A > B:
    t = A
    A = B
    B = t
  c_ab.append([C,A,B])

for i in range(m):
  D,E,F = list(map(int, input().split())) 
  if E > F:
    t = E
    E = F
    F = t
  d_ef.append([D,E,F])

c_ab.sort(key = lambda x:abs(x[0]))
d_ef.sort(key = lambda x:abs(x[0]))

wl = 0
wh = 0
for C,A,B in c_ab:
  wl = min(wl, C)
  wh = max(wh, C)
hl = 0
hh = 0
for D,E,F in d_ef:
  hl = min(hl, D)
  hh = max(hh, D)

hd= hl
wd = wl
wl = (wl-wd)*2
wh = (wh-wd)*2
hl = (hl-hd)*2
hh = (hh-hd)*2
start = [-wd*2+1, -hd*2+1]

map = [[0] * (hh+1) for _ in range(wh+1)]
for i in range(0, wh+1,2):
  for j in range(hh+1):
    map[i][j] = 2
  
for j in range(0, hh+1, 2):
  for i in range(wh+1):
    map[i][j] = 2

for C,A,B in c_ab:
  C = (C-wd)*2
  A = (A-hd)*2
  B = (B-hd)*2
  if A < hl:A = hl
  if B > hh:B = hh
  for y in range(A,B+1):
    map[C][y] = 1
for D,E,F in d_ef:
  D = (D-hd)*2
  E = (E-wd)*2
  F = (F-wd)*2
  if E < wl:E = wl
  if F > wh:F = wh
  for x in range(E,F+1):
    map[x][D] = 1

dx = [2,0,-2,0]
dy = [0,2,0,-2]
que =deque([start])
cnt = 0
while que:
  x,y = que.popleft()
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    if 0 < nx and nx < wh and 0 < ny and ny < hh:
      if (nx == 1  or ny == 1) and map[nx-1][ny-1] != 1:
        exit(print('INF'))
      elif (nx == (wh-1) or ny == (hh-1)) and map[nx+1][ny+1] != 1:
        exit(print('INF'))
      elif map[x + dx[i]//2][y + dy[i]//2] != 1 and map[nx][ny] == 0:
        cnt += 1
        map[nx][ny] = 1
        que.append([nx,ny])

print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m = list(map(int, input().split()))
c_ab = []
d_ef = []
ab = deque([])
ef = deque([])
for i in range(n):
  A,B,C= list(map(int, input().split()))
  if A > B:
    t = A
    A = B
    B = t
  c_ab.append([C,A,B])

for i in range(m):
  D,E,F = list(map(int, input().split())) 
  if E > F:
    t = E
    E = F
    F = t
  d_ef.append([D,E,F])

c_ab.sort(key = lambda x:abs(x[0]))
d_ef.sort(key = lambda x:abs(x[0]))

wl = 0
wh = 0
for C,A,B in c_ab:
  wl = min(wl, C)
  wh = max(wh, C)
hl = 0
hh = 0
for D,E,F in d_ef:
  hl = min(hl, D)
  hh = max(hh, D)


hd= hl
wd = wl
wl = (wl-wd)*2
wh = (wh-wd)*2
hl = (hl-hd)*2
hh = (hh-hd)*2
start = [-wd*2, -hd*2]

map = [[0] * (hh+1) for _ in range(wh+1)]
for C,A,B in c_ab:
  C = (C-wd)*2
  A = (A-hd)*2
  B = (B-hd)*2
  if A < hl:A = hl
  if B > hh:B = hh
  for y in range(A,B+1):
    map[C][y] = 1
for D,E,F in d_ef:
  D = (D-hd)*2
  E = (E-wd)*2
  F = (F-wd)*2
  if E < wl:E = wl
  if F > wh:F = wh
  for x in range(E,F+1):
    map[x][D] = 1

dx = [1,0,-1,0]
dy = [0,1,0,-1]
que =deque([start])
cnt = 0
while que:
  x,y = que.popleft()
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    if (nx == 0 or nx == wh or ny == 0 or ny == hh) and map[nx][ny] == 0:
      exit(print('INF'))
    if 0 <= nx and nx <= wh and 0 <= ny and ny <= hh and map[nx][ny] == 0:
      cnt += 1
      map[nx][ny] = 1
      que.append([nx,ny])


print(cnt//2)

Submission status: Runtime Error

Solution code:
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    dir = [(-1,0),(1,0),(0,-1),(0,1)]
    n,m = map(int,ipt().split())
    xb = []
    yb = []
    xs = []
    ys = []
    for i in range(n):
        a,b,c = map(int,ipt().split())
        xb.append((a,b,c))
        xs.append(a)
        xs.append(b)
        ys.append(c)
    for j in range(m):
        d,e,f = map(int,ipt().split())
        yb.append((d,e,f))
        xs.append(d)
        ys.append(e)
        ys.append(f)
    xs = sorted(list(set(xs)))
    ys = sorted(list(set(ys)))
    lx = len(xs)
    ly = len(ys)

    dix = dict()
    diy = dict()

    for i,xi in enumerate(xs):
        dix[xi] = i
    for i,yi in enumerate(ys):
        diy[yi] = i

    brd = [[1]*(ly*2-1) for _ in range(lx*2-1)]
    for a,b,c in xb:
        xa = dix[a]
        xb = dix[b]
        xc = diy[c]
        for i in range(xa,xb):
            brd[2*i+1][xc*2] = 0
    for d,e,f in yb:
        xd = dix[d]
        xe = diy[e]
        xf = diy[f]
        for i in range(xe,xf):
            brd[xd*2][2*i+1] = 0
    for i,xi in enumerate(xs[1::]):
        for j,yj in enumerate(ys[1::]):
            brd[2*i+1][2*j+1] = (yj-ys[j])*(xi-xs[i])

    sx = bisect.bisect_left(xs,0)
    sy = bisect.bisect_left(ys,0)
    if sx == 0 or sy == 0:
        print("INF")
        exit()
    q = []
    q.append((sx*2-1,sy*2-1))
    ans = brd[sx*2-1][sy*2-1]
    brd[sx*2-1][sy*2-1] = 0
    while q:
        qx,qy = q.pop()
        for dx,dy in dir:
            nx = qx+dx
            ny = qy+dy
            if brd[nx][ny]:
                if nx == 0 or nx == lx*2-2 or ny == 0 or ny == ly*2-2:
                    print("INF")
                    exit()
                nx += dx
                ny += dy
                if brd[nx][ny]:
                    ans += brd[nx][ny]
                    brd[nx][ny] = 0
                    q.append((nx,ny))
        #print(qx,qy,q)
    print(ans)


    return None

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, m = LI()
hor = []
ver = []
x_co = {-INF, INF}
y_co = {-INF, INF}
for _ in range(n):
    a, b, c = LI()
    x_co.add(a)
    x_co.add(b)
    y_co.add(c)
    ver += [(a, b, c)]

for _ in range(m):
    d, e, f = LI()
    x_co.add(d)
    y_co.add(e)
    y_co.add(f)
    hor += [(d, e, f)]


x_co = sorted(x_co)
y_co = sorted(y_co)
XD = {val:i for i,val in enumerate(x_co)}
YD = {val:i for i,val in enumerate(y_co)}
x_len = len(x_co) * 2 - 1
y_len = len(y_co) * 2 - 1
grid = [[0] * y_len for _ in range(x_len)]
x_range = [0] * x_len
y_range = [0] * y_len
x_range[0] = x_range[-1] = y_range[0] = y_range[-1] = INF
for i in range(len(x_co) - 1):
    x_range[i * 2 + 1] = x_co[i + 1] - x_co[i]
    if x_co[i] <= 0 <= x_co[i + 1]:
        sx = i * 2 + 1

for j in range(len(y_co) - 1):
    y_range[j * 2 + 1] = x_co[j + 1] - y_co[j]
    if y_co[j] <= 0 <= y_co[j + 1]:
        sy = j * 2 + 1

for a, b, c in ver:
    yi = YD[c] * 2
    for xi in range(XD[a] * 2,  XD[b] * 2 + 1):
        grid[xi][yi] = 1


for d, e, f in hor:
    xi = XD[d] * 2
    for yi in range(YD[e] * 2,  YD[f] * 2 + 1):
        grid[xi][yi] = 1


ans = x_range[sx] * y_range[sy]
grid[sx][sy] = 1


ans = 0
dq = deque([(sx, sy)])
grid[sx][sy] = 1
diff = ((0, 1), (1, 0), (-1, 0), (0, -1))
ans += x_range[sx] * y_range[sy]
while dq:
    x, y = dq.popleft()
    for dx, dy in diff:
        nx, ny = dx + x, dy + y
        if grid[nx][ny]:
            continue
        nx += dx;
        ny += dy
        if grid[nx][ny]:
            continue
        grid[nx][ny] = 1
        dq.append((nx, ny))
        ans += x_range[nx] * y_range[ny]
        if ans >= INF:
            print("INF")
            exit()

print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, m = LI()
hor = []
ver = []
x_co = {-INF, INF}
y_co = {-INF, INF}
for _ in range(n):
    a, b, c = LI()
    x_co.add(a)
    x_co.add(b)
    y_co.add(c)
    ver += [(a, b, c)]

for _ in range(m):
    d, e, f = LI()
    x_co.add(d)
    y_co.add(e)
    y_co.add(f)
    hor += [(d, e, f)]


x_co = sorted(x_co)
y_co = sorted(y_co)
XD = {val:i for i,val in enumerate(x_co)}
YD = {val:i for i,val in enumerate(y_co)}
x_len = len(x_co) * 2 - 1
y_len = len(y_co) * 2 - 1
grid = [[0] * y_len for _ in range(x_len)]
x_range = [0] * x_len
y_range = [0] * y_len
x_range[0] = x_range[-1] = y_range[0] = y_range[-1] = INF
for i in range(len(x_co) - 1):
    x_range[i * 2 + 1] = x_co[i + 1] - x_co[i]
    if x_co[i] <= 0 <= x_co[i + 1]:
        sx = i * 2 + 1

for j in range(len(y_co) - 1):
    y_range[j * 2 + 1] = x_co[j + 1] - y_co[j]
    if y_co[j] <= 0 <= y_co[j + 1]:
        sy = j * 2 + 1

for a, b, c in ver:
    yi = YD[c] * 2
    for xi in range(XD[a] * 2,  XD[b] * 2 + 1):
        grid[xi][yi] = 1


for d, e, f in hor:
    xi = XD[d] * 2
    for yi in range(YD[e] * 2,  YD[f] * 2 + 1):
        grid[xi][yi] = 1


ans = x_range[sx] * y_range[sy]
grid[sx][sy] = 1

q = deque([(sx, sy)])
while q:
    cx, cy = q.popleft()
    for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        nx, ny = cx + dx, cy + dy
        if grid[nx][ny]:
            continue
        nx += dx
        ny += dy
        if grid[nx][ny]:
            continue
        grid[nx][ny] = 1
        q += [(nx, ny)]
        ans += x_range[nx] * y_range[ny]
        if ans >= INF:
            print("INF")
            exit()



print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, acos, asin, atan, sqrt, tan, cos, pi
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n, m = LI()
hor = []
ver = []
x_co = {-INF, INF}
y_co = {-INF, INF}
for _ in range(n):
    a, b, c = LI()
    x_co.add(a)
    x_co.add(b)
    y_co.add(c)
    ver += [(a, b, c)]

for _ in range(m):
    d, e, f = LI()
    x_co.add(d)
    y_co.add(e)
    y_co.add(f)
    hor += [(d, e, f)]


x_co = sorted(x_co)
y_co = sorted(y_co)
XD = {val:i for i,val in enumerate(x_co)}
YD = {val:i for i,val in enumerate(y_co)}
x_len = len(x_co) * 2 - 1
y_len = len(y_co) * 2 - 1
grid = [[0] * y_len for _ in range(x_len)]
x_range = [0] * x_len
y_range = [0] * y_len
x_range[0] = x_range[-1] = y_range[0] = y_range[-1] = INF
for i in range(len(x_co) - 1):
    x_range[i * 2 + 1] = x_co[i + 1] - x_co[i]
    if x_co[i] <= 0 <= x_co[i + 1]:
        sx = i * 2 + 1

for j in range(len(y_co) - 1):
    y_range[j * 2 + 1] = x_co[j + 1] - y_co[j]
    if y_co[j] <= 0 <= y_co[j + 1]:
        sy = j * 2 + 1

for a, b, c in ver:
    yi = YD[c] * 2
    for xi in range(XD[a] * 2,  XD[b] * 2 + 1):
        grid[xi][yi] = 1


for d, e, f in hor:
    xi = XD[d] * 2
    for yi in range(YD[e] * 2,  YD[f] * 2 + 1):
        grid[xi][yi] = 1

ans = 0
ans += x_range[sx] * y_range[sy]
q = deque([(sx, sy)])
while q:
    cx, cy = q.popleft()
    for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        nx, ny = cx + dx, cy + dy
        if grid[nx][ny]:
            continue
        nx += dx
        ny += dy
        if grid[nx][ny]:
            continue
        grid[nx][ny] = 1
        q += [(nx, ny)]
        ans += x_range[nx] * y_range[ny]
        if ans >= INF:
            print("INF")
            exit()



print(ans)

Submission status: Runtime Error

Solution code:
#写経
#https://atcoder.jp/contests/abc168/submissions/14421546
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
from bisect import bisect_left, bisect_right
INF = 10 **18

def resolve():
    n,m = map(int,input().split())
    a = [list(map(int,input().split())) for i in range(n)]
    b = [list(map(int,input().split())) for i in range(m)]

    X = {-INF, INF}
    Y = {-INF, INF}
    for i in a:
        Y.add(i[2])
    for i in b:
        X.add(i[0])
    X = list(sorted(X))
    Y = list(sorted(Y))
    n = len(X) - 1
    m = len(Y) - 1
    wallx = [[False] * m for i in range(n)]
    wally = [[False] * m for i in range(m)]

    for x1, x2, y1 in a:
        x1 = bisect_left(X, x1)
        y1 = bisect_left(Y, y1)
        x2 = bisect_right(X, x2) - 1
        for i in range(x1, x2):
            wally[i][y1] = True

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

    cow = [[False] * m for i in range(n)]
    cx = bisect_right(X, 0) - 1
    cy = bisect_right(Y, 0) - 1
    cow[cx][cy] = True
    q = [(cx, cy)]
    ans = 0

    while q:
        x, y = q.pop()
        if not x or not y:
            print("INF")
            sys.exit()
        ans += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        if x and not wallx[x][y] and not cow[x - 1][y]:
            cow[x - 1][y] = True
            q.append((x - 1, y))
        if y and not wally[x][y] and not cow[x][y - 1]:
            cow[x][y - 1] = True
            q.append((x, y - 1))
        if x + 1 < n and not wallx[x + 1][y] and not cow[x + 1][y]:
            cow[x + 1][y] = True
            q.append((x + 1, y))
        if y + 1 < m and not wally[x][y + 1] and not cow[x][y + 1]:
            cow[x][y + 1] = True
            q.append((x, y + 1))
 
    print(ans)
resolve()

Submission status: Runtime Error

Solution code:
#写経
#https://atcoder.jp/contests/abc168/submissions/14421546

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
from bisect import bisect_left, bisect_right
INF = 10 **18

def resolve():
    n,m = map(int,input().split())
    a = [list(map(int,input().split())) for i in range(n)]
    b = [list(map(int,input().split())) for i in range(m)]

    X = {-INF, INF}
    Y = {-INF, INF}
    for i in a:
        Y.add(i[2])
    for i in b:
        X.add(i[0])
    X = list(sorted(X))
    Y = list(sorted(Y))
    n = len(X) - 1
    m = len(Y) - 1
    wallx = [[False] * m for i in range(n)]
    wally = [[False] * m for i in range(m)]

    for x1, x2, y1 in a:
        x1 = bisect_left(X, x1)
        y1 = bisect_left(Y, y1)
        x2 = bisect_right(X, x2) - 1
        for i in range(x1, x2):
            wally[i][y1] = True

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

    cow = [[False] * m for i in range(n)]
    cx = bisect_right(X, 0) - 1
    cy = bisect_right(Y, 0) - 1
    cow[cx][cy] = True
    q = [(cx, cy)]
    ans = 0

    while q:
        x, y = q.pop()
        if not x or not y:
            print("INF")
            sys.exit()
        ans += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        if x and not wallx[x][y] and not cow[x - 1][y]:
            cow[x - 1][y] = True
            q.append((x - 1, y))
        if y and not wally[x][y] and not cow[x][y - 1]:
            cow[x][y - 1] = True
            q.append((x, y - 1))
        if x + 1 < n and not wallx[x + 1][y] and not cow[x + 1][y]:
            cow[x + 1][y] = True
            q.append((x + 1, y))
        if y + 1 < m and not wally[x][y + 1] and not cow[x][y + 1]:
            cow[x][y + 1] = True
            q.append((x, y + 1))
 
    print(ans)
resolve()