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: Wrong Answer

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][H-1-x]:
        return False
      return True
    else:
      if hor_wall[y][H-1-x]:
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1][W-1-y]:
        return False
      return True
    else:
      if ver_wall[x][W-1-y]:
        return False
      return True
      
def calc_area(x,y):
  X = gyaku_xdic[x+1] - gyaku_xdic[x]
  Y = gyaku_ydic[y+1] - gyaku_ydic[y]
  return X*Y

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

hor_wall = [0 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]
  
for k in range(H):
  listb = list(map(int,list(bin(hor_wall[k])[2:])))
  hor_wall[k] = [0 for j in range(H-len(listb))] + listb

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

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

area = 0

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

print(area)

Submission status: Wrong Answer

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][H-1-x]:
        return False
      return True
    else:
      if hor_wall[y][H-1-x]:
        return False
      return True
  else:
    if dx > 0:
      if ver_wall[x+1][W-1-y]:
        return False
      return True
    else:
      if ver_wall[x][W-1-y]:
        return False
      return True
      
def calc_area(x,y):
  X = gyaku_xdic[x+1] - gyaku_xdic[x]
  Y = gyaku_ydic[y+1] - gyaku_ydic[y]
  return X*Y

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

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

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

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

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


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

area = 0

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

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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

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

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

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

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

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

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


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

area = 0

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

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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

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

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

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

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

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

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


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

area = 0

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

print(area)

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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

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

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

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

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

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

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


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

area = 0

while q:
  x,y = q.pop()
  area += calc_area(x,y)
  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 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: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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

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

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

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

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

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

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


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

area = 0

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

print(area)

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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

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

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

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

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

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

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


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

area = 0

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

print(area)

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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


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

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

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

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

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

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

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

area = 0

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

print(area)

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

INF = float("inf")

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

xtic = {0}
ytic = {0}

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

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

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

hor.sort()
ver.sort()

hor_dic = {}
ver_dic = {}

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

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

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

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

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

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

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

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

area = 0

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

print(area)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
# from collections import OrderedDict, deque
import bisect

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

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)]
ys = list(ys)
ys.sort()
y_diff = [y1 - y0 for y1, y0 in zip(ys[1:], ys)]

Q = [(bisect.bisect_left(xs, 0), bisect.bisect_left(ys, 0))]
xs = dict(zip(xs, range(len(xs))))
ys = dict(zip(ys, range(len(ys))))


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

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


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


if Q[0][0] < len(x_diff) and Q[0][1] < 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