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:
# ABC168F .(Single Dot)
g=lambda:map(int,input().split())

N,M=g()

sortx=[]
sorty=[]

y_line=[0]*N
x_line=[0]*M

for i in range(N):
    a,b,c=g()
    sortx.append(a)
    sortx.append(b)
    sorty.append(c)
    y_line[i]=(a,b,c)
    
for i in range(M):
    d,e,f=g()
    sortx.append(d)
    sorty.append(e)
    sorty.append(f)
    x_line[i]=(d,e,f)
    
from bisect import bisect_left as bl
    
sortx=sorted(list(set(sortx)))
sorty=sorted(list(set(sorty)))

for i in range(N):
    a,b,c=y_line[i]
    a=bl(sortx,a)
    b=bl(sortx,b)
    c=bl(sorty,c)
    y_line[i]=(a,b,c)
    
for i in range(M):
    d,e,f=x_line[i]
    d=bl(sortx,d)
    e=bl(sorty,e)
    f=bl(sorty,f)
    x_line[i]=(d,e,f)
    
W=len(sortx)*2-1
H=len(sorty)*2-1
    
G=[1]*W*H

def make_graph(i,j,s=0):
    G[i+j*W]=s
    
def isOK(x,y):
    if 0<=x+y*W<W*H:
        return G[x+y*W]==1
    else:
        return False
    
def isSoto(x,y):
    return (not 0<=x+y*W<W*H)

def get_score(i,j):
    if i<=0 or j<=0  or i>=W or j>=H:
        return float('inf')
    elif i%2 and j%2:
        dx=sortx[(i+1)//2]-sortx[(i-1)//2]
        dy=sorty[(j+1)//2]-sorty[(j-1)//2]
        return dx*dy
    else:
        return 0

dic=[(-1,0),(1,0),(0,1),(0,-1)]
def search(i,j):
    res=[]
    for dx,dy in dic:
        if isOK(i+dx,j+dy) and isOK(i+2*dx,j+2*dy):
            res.append((i+2*dx,j+2*dy))
    return res

for a,b,c in y_line:
    for i in range(2*a,2*b+1):
        make_graph(i,2*c)
        
for d,e,f in x_line:
    for j in range(2*e,2*f+1):
        make_graph(2*d,j)
        
x0,y0=2*bl(sortx,0)-1,2*bl(sorty,0)-1

stack=[]
for dx,dy in [(-1,-1),(-1,1),(1,1),(1,-1)]:
    if isOK(x0+dx,y0) or isOK(x0,y0+dy):
        stack.append((x0+dx,y0+dy))

stack.append((x0,y0))
res=0
make_graph(x0,y0,1)

while stack:
    x,y=stack.pop()
    if isSoto(x,y):
        res+=get_score(x,y)
        break
    if not isOK(x,y):
        continue
    res+=get_score(x,y)
    stack+=search(x,y)
    make_graph(x,y)

print(res if res<float('inf') else 'INF')

Submission status: Wrong Answer

Solution code:
# ABC168F .(Single Dot)
g=lambda:map(int,input().split())

N,M=g()

sortx=[]
sorty=[]

y_line=[0]*N
x_line=[0]*M

for i in range(N):
    a,b,c=g()
    sortx.append(a)
    sortx.append(b)
    sorty.append(c)
    y_line[i]=(a,b,c)
    
for i in range(M):
    d,e,f=g()
    sortx.append(d)
    sorty.append(e)
    sorty.append(f)
    x_line[i]=(d,e,f)
    
from bisect import bisect_left as bl
    
sortx=sorted(list(set(sortx)))
sorty=sorted(list(set(sorty)))

for i in range(N):
    a,b,c=y_line[i]
    a=bl(sortx,a)
    b=bl(sortx,b)
    c=bl(sorty,c)
    y_line[i]=(a,b,c)
    
for i in range(M):
    d,e,f=x_line[i]
    d=bl(sortx,d)
    e=bl(sorty,e)
    f=bl(sorty,f)
    x_line[i]=(d,e,f)
    
W=len(sortx)*2-1
H=len(sorty)*2-1
    
G=[1]*W*H

def make_graph(i,j,s=0):
    G[i+j*W]=s
    
def isOK(x,y):
    if 0<=x+y*W<W*H:
        return G[x+y*W]==1
    else:
        False
        
def get_score(i,j):
    if i<=0 or j<=0  or i>=W or j>=H:
        return float('inf')
    elif i%2 and j%2:
        dx=sortx[(i+1)//2]-sortx[(i-1)//2]
        dy=sorty[(j+1)//2]-sorty[(j-1)//2]
        return dx*dy
    else:
        return 0

dic=[(-1,0),(1,0),(0,1),(0,-1)]

def search(i,j):
    res=[]
    for dx,dy in dic:
        if isOK(i+dx,j+dy):
            res.append((i+2*dx,j+2*dy))
    return res

for a,b,c in y_line:
    for i in range(2*a,2*b+1):
        make_graph(i,2*c)
        
for d,e,f in x_line:
    for j in range(2*e,2*f+1):
        make_graph(2*d,j)
        


x0,y0=2*bl(sortx,0)-1,2*bl(sorty,0)-1

stack=[(x0,y0)]

for dx,dy in [(-1,-1),(-1,1),(1,1),(1,-1)]:
    if isOK(x0+dx,y0) or isOK(x0,y0+dy):
        stack.append((x0+dx,y0+dy))

res=0

make_graph(x0,y0,1)

while stack:
    x,y=stack.pop()
    if not isOK(x,y):
        continue
    res+=get_score(x,y)
    stack+=search(x,y)
    make_graph(x,y)

print(res if res<float('inf') else 'INF')

Submission status: Wrong Answer

Solution code:
# ABC168F .(Single Dot)
g=lambda:map(int,input().split())

N,M=g()

sortx=[]
sorty=[]

y_line=[]
x_line=[]

for _ in [0]*N:
    a,b,c=g()
    sortx.append(a)
    sortx.append(b)
    sorty.append(c)
    y_line.append((a,b,c))
    
for _ in [0]*M:
    d,e,f=g()
    sortx.append(d)
    sorty.append(e)
    sorty.append(f)
    x_line.append((d,e,f))
    
from bisect import bisect_left as bl
    
sortx=sorted(list(set(sortx)))
sorty=sorted(list(set(sorty)))

zip_x_line=[]
zip_y_line=[]
for a,b,c in y_line:
    a=bl(sortx,a)
    b=bl(sortx,b)
    c=bl(sorty,c)
    zip_y_line.append((a,b,c))
    
for d,e,f in x_line:
    d=bl(sortx,d)
    e=bl(sorty,e)
    f=bl(sorty,f)
    zip_x_line.append((d,e,f))
    
W=len(sortx)*2-1
H=len(sorty)*2-1
    
G=[1]*W*H+[0]*(W+1)

def make_graph(i,j,s=0):
    G[i+j*W]=s

for a,b,c in zip_y_line:
    for i in range(2*a,2*b+1):
        make_graph(i,2*c)
        
for d,e,f in zip_x_line:
    for j in range(2*e,2*f+1):
        make_graph(2*d,j)
        
def get_score(i,j):
    if i<=0 or j<=0  or i>=W or j>=H:
        return float('inf')
    elif i%2 and j%2:
        dx=sortx[(i+1)//2]-sortx[(i-1)//2]
        dy=sorty[(j+1)//2]-sorty[(j-1)//2]
        return dx*dy
    else:
        return 0

dic=[(-1,0),(1,0),(0,1),(0,-1)]

def search(i,j):
    res=[]
    for dx,dy in dic:
        if G[(i+dx)+(j+dy)*W]==1 and G[(i+2*dx)+(j+2*dy)*W]==1:
            res.append((i+2*dx,j+2*dy))
    return res

x0,y0=2*bl(sortx,0)-1,2*bl(sorty,0)-1
stack=[(x0,y0)]
res=0

make_graph(x0,y0,1)

while stack:
    x,y=stack.pop()
    if G[x+y*W]!=1:
        continue
    res+=get_score(x,y)
    stack+=search(x,y)
    make_graph(x,y)

print(res if res<float('inf') else 'INF')

Submission status: Wrong Answer

Solution code:
# ABC168F .(Single Dot)
g=lambda:map(int,input().split())

N,M=g()

sortx=[]
sorty=[]

y_line=[]
x_line=[]

for _ in [0]*N:
    a,b,c=g()
    sortx.append(a)
    sortx.append(b)
    sorty.append(c)
    y_line.append((a,b,c))
    
for _ in [0]*M:
    d,e,f=g()
    sortx.append(d)
    sorty.append(e)
    sorty.append(f)
    x_line.append((d,e,f))
    
from bisect import bisect_left as bl
    
sortx=sorted(list(set(sortx)))
sorty=sorted(list(set(sorty)))

zip_x_line=[]
zip_y_line=[]
for a,b,c in y_line:
    a=bl(sortx,a)
    b=bl(sortx,b)
    c=bl(sorty,c)
    zip_y_line.append((a,b,c))
    
for d,e,f in x_line:
    d=bl(sortx,d)
    e=bl(sorty,e)
    f=bl(sorty,f)
    zip_x_line.append((d,e,f))
    
W=len(sortx)*2-1
H=len(sorty)*2-1
    
G=[1]*W*H+[0]*(W+1)

def make_graph(i,j,s=0):
    G[i+j*W]=s

for a,b,c in zip_y_line:
    for i in range(2*a,2*b+1):
        make_graph(i,2*c)
        
for d,e,f in zip_x_line:
    for j in range(2*e,2*f+1):
        make_graph(2*d,j)
        
def get_score(i,j):
    if i<=0 or j<=0  or i>=W or j>=H:
        return float('inf')
    elif i%2 and j%2:
        dx=sortx[(i+1)//2]-sortx[(i-1)//2]
        dy=sorty[(j+1)//2]-sorty[(j-1)//2]
        return dx*dy
    else:
        return 0
        


dic=[(-1,0),(1,0),(0,1),(0,-1)]    
    
def search(i,j):
    res=[]
    for dx,dy in dic:
        if G[(i+dx)+(j+dy)*W]==1:
            res.append((i+dx,j+dy))
    return res
    
stack=[(2*bl(sortx,0)-1,2*bl(sorty,0)-1)]
res=0

while stack:
    x,y=stack.pop()
    if G[x+y*W]!=1:
        continue
    res+=get_score(x,y)
    stack+=search(x,y)
    make_graph(x,y)
    
print(res if res<float('inf') else 'INF')

Submission status: Wrong Answer

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import deque
def resolve():
    m, n = map(int, input().split())
    horizontal, vertical = [], []
    X, Y = {-INF, 0, INF}, {-INF, 0, INF}

    for _ in range(m):
        x0, x1, y = map(int, input().split())
        horizontal.append((x0, x1, y))
        X.add(x0)
        X.add(x1)
        Y.add(y)
    for _ in range(n):
        x, y0, y1 = map(int, input().split())
        vertical.append((x, y0, y1))
        X.add(x)
        Y.add(y0)
        Y.add(y1)
    X = sorted(X)
    Y = sorted(Y)

    # compress
    m, n = len(X), len(Y)
    cX = {v : i for i, v in enumerate(X)}
    cY = {v : i for i, v in enumerate(Y)}

    # make grid
    grid_hor = [[True] * n for _ in range(m)]
    grid_ver = [[True] * n for _ in range(m)]
    for x0, x1, y in horizontal:
        y = cY[y]
        for x in range(cX[x0], cX[x1]):
            grid_hor[x][y] = False
    for x, y0, y1 in vertical:
        x = cX[x]
        for y in range(cY[y0], cY[y1]):
            grid_ver[x][y] = False

    # bfs
    ans = 0
    queue = [(cX[0], cY[0])]
    used = [[False] * n for _ in range(m)]
    used[cX[0]][cY[0]] = True
    for x, y in queue:
        ans += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        # up
        if y + 1 < n - 1 and grid_hor[x][y + 1] and not used[x][y + 1]:
            used[x][y + 1] = True
            queue.append((x, y + 1))
        # down
        if y - 1 >= 0 and grid_hor[x][y] and not used[x][y - 1]:
            used[x][y - 1] = True
            queue.append((x, y - 1))
        # right
        if x + 1 < m - 1 and grid_ver[x + 1][y] and not used[x + 1][y]:
            used[x + 1][y] = True
            queue.append((x + 1, y))
        # left
        if x - 1 >= 0 and grid_ver[x][y] and not used[x - 1][y]:
            used[x - 1][y] = True
            queue.append((x - 1, y))

    if ans >= INF:
        ans = "INF"
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A, B, C = [], [], []
for _ in range(N):
  a, b, c = map(int, input().split())
  A.append(a)
  B.append(b)
  C.append(c)
D, E, F = [], [], []
for _ in range(M):
  d, e, f = map(int, input().split())
  D.append(d)
  E.append(e)
  F.append(f)

orig_X = sorted(set([0] + A + B + D))
orig_Y = sorted(set([0] + C + E + F))
                
X = {x: i for i, x in enumerate(orig_X)}
Y = {y: i for i, y in enumerate(orig_Y)}
max_X = len(X) - 1
max_Y = len(Y) - 1
#print(X, Y)

movable = [(-1, 0), (1, 0), (0, -1), (0, 1)]
blocked = {}
# (x, y) -> stands at (x+0.5, y+0.5)
for a, b, c in zip(A, B, C):
  y = Y[c]
  for x in range(X[a], X[b]):
    if y < max_Y:
      if (x, y) not in blocked:
        blocked[(x, y)] = set()
      blocked[(x, y)].add((0, -1))
    if y > 0:
      if (x, y-1) not in blocked:
        blocked[(x, y-1)] = set()
      blocked[(x, y-1)].add((0, +1))

for d, e, f in zip(D, E, F):
  x = X[d]
  for y in range(Y[e], Y[f]):
    if (x, y) not in blocked:
      blocked[(x, y)] = set()
    blocked[(x, y)].add((-1, 0))
    if x > 0:
      if (x-1, y) not in blocked:
        blocked[(x-1, y)] = set()
      blocked[(x-1, y)].add((+1, 0))

#print(sorted(blocked))

visited = [0] * max_X * max_Y
def get_visited(x, y):
  return visited[x * max_Y + y]
def set_visited(x, y):
  #print(x,y)
  visited[x * max_Y + y] = True

to_visit = [(X[0], Y[0])]
while to_visit:
  #print(to_visit)
  x, y = to_visit.pop()
  if x < 0 or x >= max_X - 1 or y < 0 or y >= max_Y - 1:
    print('INF')
    exit()
  if get_visited(x, y):
    continue
  set_visited(x, y)
  for d in movable:
    if (x, y) in blocked and d in blocked[(x, y)]:
      continue
    to_visit.append((x + d[0], y + d[1]))

ans = 0
for x in range(0, max_X - 1):
  for y in range(0, max_Y - 1):
    if get_visited(x, y):
      ans += (orig_X[x+1] - orig_X[x]) * (
        orig_Y[y+1] - orig_Y[y])
print(ans)

Submission status: Wrong Answer

Solution code:
def numba_compile(numba_config):
    import os, sys
    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC
        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)

import sys
import numpy as np

def solve(N, M, ABCDEF):
    L, R, U, D = 0, 1, 2, 3
    Dy = [0, 0, -1, 1]
    Dx = [-1, 1, 0, 0]
    mapping_inv_x = [0]
    mapping_inv_y = [0]
    for i in range(N):
        y1, y2, x = ABCDEF[i]
        mapping_inv_x.append(x)
        mapping_inv_y.append(y1)
        mapping_inv_y.append(y2)
    for i in range(N, N+M):
        y, x1, x2 = ABCDEF[i]
        mapping_inv_x.append(x1)
        mapping_inv_x.append(x2)
        mapping_inv_y.append(y)
    mapping_inv_y = sorted(set(mapping_inv_y))
    mapping_inv_x = sorted(set(mapping_inv_x))
    H = len(mapping_inv_y) + 1
    W = len(mapping_inv_x) + 1
    E = np.ones((H, W, 4), dtype=np.int8)
    #mapping_y = {v: i for i, v in enumerate(mapping_inv_y)}
    #mapping_x = {v: i for i, v in enumerate(mapping_inv_x)}
    mapping_y = {}
    for i, v in enumerate(mapping_inv_y):
        mapping_y[v] = i
    mapping_x = {}
    for i, v in enumerate(mapping_inv_x):
        mapping_x[v] = i
    for i in range(N):
        y1, y2, x = ABCDEF[i]
        y1 = mapping_y[y1]
        y2 = mapping_y[y2]
        x = mapping_x[x]
        for y in range(y1, y2):
            E[y, x, L] = 0
            E[y, x-1, R] = 0
    for i in range(N, N+M):
        y, x1, x2 = ABCDEF[i]
        y = mapping_y[y]
        x1 = mapping_x[x1]
        x2 = mapping_x[x2]
        for x in range(x1, x2):
            E[y, x, U] = 0
            E[y-1, x, D] = 0
    Diff_x = np.diff(np.array(mapping_inv_x))
    Diff_y = np.diff(np.array(mapping_inv_y))
    Area = np.empty((H, W), dtype=np.int64)
    Area[1:-1, 1:-1] = Diff_y.reshape(-1, 1) * Diff_x
    Closed = np.zeros((H, W), dtype=np.int8)
    y, x = mapping_y[0], mapping_x[0]
    Closed[y, x] = 1
    ans = 0
    stack = [(y, x)]
    while stack:
        vy, vx = stack.pop()
        if vy==0 or vy==H-1 or vx==0 or vx==W-1:
            print("INF")
            return
        ans += Area[vy, vx]
        for direction in [L, R, U, D]:
            if E[vy, vx, direction]:
                uy, ux = vy + Dy[direction], vx + Dx[direction]
                if not Closed[uy, ux]:
                    stack.append((uy, ux))
                    Closed[uy, ux] = 1
    print(ans)

numba_compile([
    [solve, "(i8,i8,i8[:,:])"],
])

def main():
    N, M = map(int, sys.stdin.buffer.readline().split())
    ABCDEF = np.array(sys.stdin.buffer.read().split(), dtype=np.int64).reshape(-1, 3)
    solve(N, M, ABCDEF)

main()

Submission status: Wrong Answer

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*8012006001+y1*4004001+x2*2001+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)

Submission status: Wrong Answer

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*8084294343+y1*4028049+x2*2007+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)

Submission status: Wrong Answer

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]]-x[dicx[0]-1])*(y[dicy[0]]-y[dicy[0]-1])
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]-x[tx-1])*(y[ty]-y[ty-1])
        stack.append((tx,ty))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N, M = map(int, input().split())
X = []
for _ in range(N):
    a, b, c = map(int, input().split())
    X.append((a,b,c))
Y = []
for _ in range(M):
    a, b, c = map(int, input().split())
    Y.append((b, c, a))
X.sort(key=lambda x:x[2])
Y.sort(key=lambda x:x[2])

#print(X)
#print(Y)
dxs = []
bc = X[0][2]
for _, _, c in X[1:]:
    dxs.append(c-bc)
    bc = c
dys = []
bc = Y[0][2]
for _, _, c in Y[1:]:
    dys.append(c-bc)
    bc = c
#print(dxs, dys)
Ws = set()
for i, (a, b, c) in enumerate(X):
    yi = 0
    for (x1, y1, z1), (x2, y2, z2) in zip(Y, Y[1:]):
        if a<=z1 and z2<=b:
            Ws.add(((i-1, yi), (i, yi)))
            Ws.add(((i, yi), (i-1, yi)))
        yi += 1

for i, (a, b, c) in enumerate(Y):
    yi = 0
    for (x1, y1, z1), (x2, y2, z2) in zip(X, X[1:]):
        if a<=z1 and z2<=b:
            Ws.add(((yi, i-1), (yi, i)))
            Ws.add(((yi, i), (yi, i-1)))
        yi += 1
#print(Ws)

fci = -1
for _, _, c in X:
    if 0<c:
        break
    fci += 1
if fci < 0 or N-1<=fci:
    print("INF")
    sys.exit()
fcj = -1
for _, _, c in Y:
    if 0<c:
        break
    fcj += 1
if fcj < 0 or M-1<=fcj:
    print("INF")
    sys.exit()
#print(fci, fcj)
stack = [(fci, fcj)]
S = 0
vs = set([(fci, fcj)])
while stack:
    i,j = stack.pop()
    S += dxs[i]*dys[j]
    for k, l in [(i+1, j), (i-1, j), (i, j-1), (i, j+1)]:
        if ((i, j), (k, l)) in Ws:
            continue
#        print(k, l)
        if k<0 or N-2<k:
            print("INF")
            sys.exit()
        if l<0 or M-2<l:
            print("INF")
            sys.exit()
        if (k, l) in vs:
            continue
        vs.add((k, l))
        stack.append((k, l))
print(S)

Submission status: Wrong Answer

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

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]]
visited = [0] * (H * W)
visited[Cx[0] * W + Cy[0]] = 1
res = 0

while stack:
    z = stack.pop()
    nz = z + 1
    if G[nz] != -1 and not visited[nz]:
        nx, ny = divmod(nz, W)
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    nz = z - 1
    if G[nz] != -1 and not visited[nz]:
        nx, ny = divmod(nz, W)
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
    nz = z + W
    if G[nz] != -1 and not visited[nz]:
        nx, ny = divmod(nz, W)
        if nx == 0 or nx == H - 1 or ny == 0 or 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]:
        nx, ny = divmod(nz, W)
        if nx == 0 or nx == H - 1 or ny == 0 or ny == W - 1:
            break
        visited[nz] = 1
        res += G[nz]
        stack.append(nz)
else:
    print(res)

Submission status: Wrong Answer

Solution code:
from numba import njit
import numpy as np
'''
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]
'''
import random
N = 1000
M = 1000

abcs = []
defs = []
for _ in range(N-2):
    a, b = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    c = random.randint(-10**9, 10**9)
    if a == b:
        abcs.append((a, b+1, c))
    else:
        abcs.append((a, b, c))

abcs += [(-10**9, 10**9, -10**9), (-10**9, 10**9, 10**9)]

for _ in range(M-2):
    e, f = sorted([random.randint(-10**9, 10**9), random.randint(-10**9, 10**9)])
    d = random.randint(-10**9, 10**9)
    if e == f:
        defs.append((d, e, f+1))
    else:
        defs.append((d, e, f))
    
defs += [(-10**9, -10**9, 10**9), (10**9, -10**9, 10**9)]

#'''

xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x) + 1
y_size = 2 * len(scatter_y) + 1

field = np.zeros((x_size, y_size), np.int32)

for a, b, c in abcs:
    field[2 * gather_x[a]][2 * gather_y[c]] += 1
    field[2 * gather_x[b] + 1][2 * gather_y[c]] -= 1
    field[2 * gather_x[a]][2 * gather_y[c] + 1] -= 1
    field[2 * gather_x[b] + 1][2 * gather_y[c] + 1] += 1

for d, e, f in defs:
    field[2 * gather_x[d]][2 * gather_y[e]] += 1
    field[2 * gather_x[d]][2 * gather_y[f] + 1] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[e]] -= 1
    field[2 * gather_x[d] + 1][2 * gather_y[f] + 1] += 1

@njit
def imos(field):
    for x in range(x_size-1):
        for y in range(y_size-1):
            field[x][y+1] += field[x][y]

    for y in range(y_size-1):
        for x in range(x_size-1):
            field[x+1][y] += field[x][y]

    return field

field = imos(field)

area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = 1
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy] == 0:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = 1

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size or y == y_size:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y

print(area)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
abcs = [tuple(map(int, input().split())) for _ in range(N)]
defs = [tuple(map(int, input().split())) for _ in range(M)]


xs = set([0])
ys = set([0])
for a, b, c in abcs:
    xs.add(a)
    xs.add(b)
    ys.add(c)
for d, e, f in defs:
    xs.add(d)
    ys.add(e)
    ys.add(f)

scatter_x = sorted(xs)
scatter_y = sorted(ys)

gather_x = {e:i for i, e in enumerate(scatter_x)}
gather_y = {e:i for i, e in enumerate(scatter_y)}

x_size = 2 * len(scatter_x)
y_size = 2 * len(scatter_y)

field = [[True] * y_size for _ in range(x_size)]

for a, b, c in abcs:
    y = 2 * gather_y[c]
    for x in range(2 * gather_x[a], 2 * gather_x[b] + 1):
        field[x][y] = False

for d, e, f in defs:
    x = 2 * gather_x[d]
    for y in range(2 * gather_y[e], 2 * gather_y[f] + 1):
        field[x][y] = False


area = 0
sx = gather_x[0] * 2
sy = gather_y[0] * 2
field[sx][sy] = False
stack = [(sx, sy)]
while stack:
    x, y = stack.pop()
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        if not (0 <= x+dx < x_size and 0 <= y+dy < y_size):
            print("INF")
            exit()
        else:
            if field[x+dx][y+dy]:
                stack.append( (x+dx, y+dy) )
                field[x+dx][y+dy] = False

    if x % 2 == 1 and y % 2 == 1:
        if x == x_size - 1 and y == y_size - 1:
            print("INF")
            exit()
        else:
            delta_x = scatter_x[x//2 + 1] - scatter_x[x//2]
            delta_y = scatter_y[y//2 + 1] - scatter_y[y//2]       
            area += delta_x * delta_y
            print(x, y, area)  

print(area)

Submission status: Wrong Answer

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

N, M = map(int, input().split())
INF = 4 * 10**18 + 1

X = [0]
Y = [0]
X_line = []
Y_line = [] 

X_append = X.append
Y_append = Y.append
X_line_append = X_line.append
Y_line_append = Y_line.append

for i in range(N):
  A, B, C = map(int, input().split())
  if A > B:
    A, B = B, A
  X_append(A)
  X_append(B)
  Y_append(C)
  X_line_append((A,B,C)) 
  
for j in range(M):
  D, E, F = map(int, input().split())
  if E > F:
    E, F = F, E
  X_append(D)
  Y_append(E)
  Y_append(F)
  Y_line_append((D,E,F)) 

X.sort()
Y.sort()
X = list(set(X))
Y = list(set(Y))
X.sort()
Y.sort()

X_dict = {}
Y_dict = {}

i = 0
for x in X:
  X_dict[x] = 2 * i + 1
  i += 1
j = 0
for y in Y:
  Y_dict[y] = 2 * j + 1
  j += 1

G = [[0]*(2*len(Y)+1)]*(2*len(X)+1)
for i_2 in range(len(X)+1):
  for j_2 in range(len(Y)+1):
    #if i % 2 == 1 or j % 2 == 1:
    #  continue #G[i][j] = 0, 奇数の箇所が面積0の要素を表す
    #else:
    if 1 <= i_2 < len(X) and 1 <= j_2 < len(Y):
      G[i_2*2][j_2*2] = (X[i_2]-X[i_2-1])*(Y[j_2]-Y[j_2-1])
    else:
      G[i_2*2][j_2*2] = INF #各ノードが持つ面積を記録、最外殻は面積無限大 
        
for (A, B, C) in X_line:
  for xd in range(X_dict[A], X_dict[B]+1):
    #print(xd, yd, X_dict[B], len(X), len(Y))
    G[xd][Y_dict[C]] = -1 #壁は面積-1で区別
    xd += 1
    
for (D, E, F) in Y_line:
  for yd in range(Y_dict[E], Y_dict[F]+1):
    G[X_dict[D]][yd] = -1 #壁は面積-1で区別
    yd += 1
    
#探索により面積導出
ans = 0
que = []
visited = [[0]*(2*len(Y)+1)]*(2*len(X)+1)
if G[X_dict[0]][Y_dict[0]] >= 0:
  #print(X_dict[0], Y_dict[0])
  que.append((X_dict[0],Y_dict[0]))
  visited[X_dict[0]][Y_dict[0]] = 1
while que and ans < INF:
  xd, yd = que.pop(-1)
  #print(xd, yd, G[xd][yd])
  if G[xd][yd] >= 0: 
    ans += G[xd][yd]
    G[xd][yd] = -1
    if xd < 2*len(X) and visited[xd+1][yd] == 0:
      que.append((xd+1,yd))
      visited[xd+1][yd] = 1
    if xd > 0 and visited[xd-1][yd] == 0:
      que.append((xd-1,yd))
      visited[xd-1][yd] = 1
    if yd < 2*len(Y) and visited[xd][yd+1] == 0:
      que.append((xd,yd+1))
      visited[xd][yd+1] = 1
    if yd > 0 and visited[xd][yd-1] == 0:
      que.append((xd,yd-1))
      visited[xd][yd-1] = 1
    
if ans >= INF:
  print("INF")
else:
  print(ans)