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

Solution code:
n,m,*l = map(int, open(0).read().split())
l=iter(l)
l=list(zip(l,l,l))
l1=l[:n]
l2=l[-m:]

V=set([float('inf'),-float('inf')])
H=set([float('inf'),-float('inf')])

for a,b,c in l1:
    V.add(c)
    H.add(a)
    H.add(b)
for c,a,b in l2:
    H.add(c)
    V.add(a)
    V.add(b)

V=sorted(list(V))
H=sorted(list(H))
Vdic={}
Hdic={}
for i,v in enumerate(V):
    Vdic[v]=i
for i,h in enumerate(H):
    Hdic[h]=i

checkv=[[0]*3001 for i in range(3001)]
checkh=[[0]*3001 for i in range(3001)]
check=[[0]*3001 for i in range(3001)]

for a,b,c in l1:
    c=Vdic[c]
    for i in range(Hdic[a],Hdic[b]):
        checkh[c][i]=1
for c,a,b in l2:
    c=Hdic[c]
    for i in range(Vdic[a],Vdic[b]):
        checkv[i][c]=1

from bisect import bisect_left
pv=bisect_left(V,0)-1
ph=bisect_left(V,0)-1

ans=0

stack=[(pv,ph)]
check[pv][ph]=1

while stack:
    v,h=stack.pop()
    ans+=(V[v+1]-V[v])*(H[h+1]-H[h])
    if h!=0 and checkv[v][h]==0 and check[v][h-1]==0:
        stack.append((v,h-1))
        check[v][h-1]=1
    if h!=len(H)-2 and checkv[v][h+1]==0 and check[v][h+1]==0:
        stack.append((v,h+1))
        check[v][h+1]=1
    if v!=0 and checkh[v][h]==0 and check[v-1][h]==0:
        stack.append((v-1,h))
        check[v-1][h]=1
    if v!=len(V)-2 and checkh[v+1][h]==0 and check[v+1][h]==0:
        stack.append((v+1,h))
        check[v+1][h]=1

print("INF" if ans==float('inf') else ans)

Submission status: Wrong Answer

Solution code:
n,m,*l = map(int, open(0).read().split())
l=iter(l)
l=list(zip(l,l,l))
l1=l[:n]
l2=l[-m:]

V=set([float('inf'),-float('inf')])
H=set([float('inf'),-float('inf')])

for a,b,c in l1:
    V.add(c)
    H.add(a)
    H.add(b)
for c,a,b in l2:
    H.add(c)
    V.add(a)
    V.add(b)

V=sorted(list(V))
H=sorted(list(H))
Vdic={}
Hdic={}
for i,v in enumerate(V):
    Vdic[v]=i
for i,h in enumerate(H):
    Hdic[h]=i

checkv=[[0]*3001 for i in range(3001)]
checkh=[[0]*3001 for i in range(3001)]
check=[[0]*3001 for i in range(3001)]

for a,b,c in l1:
    c=Vdic[c]
    for i in range(Hdic[a],Hdic[b]):
        checkh[c][i]=1
for c,a,b in l2:
    c=Hdic[c]
    for i in range(Vdic[a],Vdic[b]):
        checkv[i][c]=1

from bisect import bisect_left
pv=bisect_left(V,0)-1
ph=bisect_left(V,0)-1

ans=0

stack=[(pv,ph)]
check[pv][ph]=1

while stack:
    v,h=stack.pop()
    ans+=(V[v+1]-V[v])*(H[h+1]-H[h])
    if h!=0 and checkv[v][h]==0 and check[v][h-1]==0:
        stack.append((v,h-1))
        check[v][h-1]=1
    if h!=len(H)-2 and checkv[v][h+1]==0 and check[v][h+1]==0:
        stack.append((v,h+1))
        check[v][h+1]=1
    if v!=0 and checkh[v][h]==0 and check[v-1][h]==0:
        stack.append((v-1,h))
        check[v-1][h]=1
    if v!=len(V)-2 and checkh[v+1][h]==0 and check[v+1][h]==0:
        stack.append((v+1,h))
        check[v+1][h]=1

print(ans)

Submission status: Wrong Answer

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

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

@njit
def set_ng(A, B, C, D, E, F):
    ng = np.zeros((N, 4), np.bool_)

    for i in range(len(A)):
        for x in range(A[i], B[i]):
            v = x * W + C[i]
            ng[v][1] = 1
            ng[v - 1][0] = 1

    for i in range(len(D)):
        for y in range(E[i], F[i]):
            v = D[i] * W + y
            ng[v][3] = 1
            ng[v - W][2] = 1
    return ng

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

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

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

Submission status: Wrong Answer

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

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

@njit
def set_ng(A, B, C, D, E, F):
    p = 0
    ng = np.zeros((N, 4), np.bool_)

    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
            ng[v][1] = 1
            ng[v - 1][0] = 1

    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
            ng[v][3] = 1
            ng[v - W][2] = 1
    return ng

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

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

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

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] = [(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[W] = INF
gyaku_ydic[H] = INF

connection = [[[] for i in range(W)] for j in range(H)]
for y in range(H):
  for x in range(W):
    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):
          connection[y][x].append((nx,ny))

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: 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] = [(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[W] = INF
gyaku_ydic[H] = 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: 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] = [(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)
print("xtic",xtic)
print("ytic",ytic)
print("xdic",xdic)
print("ydic",ydic)
print("gyaku_xdic",gyaku_xdic)
print("gyaku_ydic",gyaku_ydic)

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] = [(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)
print("xtic",xtic)
print("ytic",ytic)
print("xdic",xdic)
print("ydic",ydic)
print("gyaku_xdic",gyaku_xdic)
print("gyaku_ydic",gyaku_ydic)
"""

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

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

M = 2*10**9+1
M2 = M**2
def solve():
    n,m = LI()
    p = LIR(n)
    q = LIR(m)
    fx = set([-M,0,M])
    fy = set([-M,0,M])
    for i in range(n):
        p[i][0] <<= 1
        p[i][1] <<= 1
        p[i][2] <<= 1
        a,b,c = p[i]
        fy.add(a)
        fy.add(b)
        fy.add(b+1)
        fx.add(c)
        fx.add(c+1)

    for i in range(m):
        q[i][0] <<= 1
        q[i][1] <<= 1
        q[i][2] <<= 1
        d,e,f = q[i]
        fy.add(d)
        fy.add(d+1)
        fx.add(e)
        fx.add(f)
        fx.add(f+1)
    fy = list(fy)
    fx = list(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]
    sx = bisect.bisect_left(fx,0)
    sy = bisect.bisect_left(fy,0)
    nh,nw = len(fy)-1,len(fx)-1
    for i in range(nw-1):
        fx[i+1] >>= 1
        fx[i] = fx[i+1]-fx[i]
    for i in range(nh-1):
        fy[i+1] >>= 1
        fy[i] = fy[i+1]-fy[i]
    ans = fx[sx]*fy[sy]
    q = deque()
    q.append((sy,sx))
    f = [1]*nh*nw
    f[sy*nw+sx] = 0
    d = [(1,0),(-1,0),(0,1),(0,-1)]
    while q:
        y,x = q.popleft()
        i = y*nw+x
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < nh and 0 <= nx < nw:
                ind = i+dy*nw+dx
                if f[ind] and not s[ind+(ny<<1)]:
                    k = fx[nx]*fy[ny]
                    if k >= M2:
                        print("INF")
                        return
                    ans += k
                    f[ind] = 0
                    q.append((ny,nx))
    print(ans)
    return

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

Submission status: Wrong Answer

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

# debug
exit()

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)