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: Time Limit Exceeded

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]>>x)&1:
        return False
      return True
    else:
      if (hor_wall[y]>>x)&1:
        return False
      return True
  else:
    if dx > 0:
      if (ver_wall[x+1]>>y)&1:
        return False
      return True
    else:
      if (ver_wall[x]>>y)&1:
        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]

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: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,M=map(int,input().split())
    X,Y=set([0]),set([0])
    A,B,C=[0]*N,[0]*N,[0]*N
    for i in range(N):
        A[i],B[i],C[i]=map(int,input().split())
        X.add(A[i])
        X.add(B[i])
        Y.add(C[i])
    D,E,F=[0]*M,[0]*M,[0]*M
    for i in range(M):
        D[i],E[i],F[i]=map(int,input().split())
        X.add(D[i])
        Y.add(E[i])
        Y.add(F[i])
    UX=list(X)
    UY=list(Y)
    UX.sort()
    UY.sort()
    dictx,dicty={n:i*2 for i,n in enumerate(UX)},{n:i*2 for i,n in enumerate(UY)}
    LX=[UX[i//2+1]-UX[i//2] if i%2 else 0 for i in range(len(UX)*2-1)]
    LY=[UY[i//2+1]-UY[i//2] if i%2 else 0 for i in range(len(UY)*2-1)]
    c=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    for i in range(N):
        y=dicty[C[i]]
        for x in range(dictx[A[i]],dictx[B[i]]+1):
            c[y][x]=1
    for i in range(M):
        x=dictx[D[i]]
        for y in range(dicty[E[i]],dicty[F[i]]+1):
            c[y][x]=1
    que=deque([(dicty[0],dictx[0])])
    used=[[0]*len(UX)*2 for _ in range(len(UY)*2)]
    used[dicty[0]][dictx[0]]=1
    ans=0
    dy=[1,0,-1,0]
    dx=[0,1,0,-1]
    while que:
        y,x=que.popleft()
        if y==0 or y==len(UY)*2-1 or x==0 or x==len(UX)*2-1:
            ans='INF'
            break
        if y%2 and x%2:
            ans+=LY[y]*LX[x]
        for k in range(4):
            ny,nx=y+dy[k],x+dx[k]
            if c[ny][nx]==0 and not used[ny][nx]:
                que.append((ny,nx))
                used[ny][nx]=1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
ABX = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
YEF = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

# X, Y を列挙して四角をいっぱい作る
X = []
Y = []

for a, b, x in ABX:
    X.append(x)
    Y.append(a)
    Y.append(b)
for y, e, f in YEF:
    X.append(e)
    X.append(f)
    Y.append(y)

X = [-INF] + list(sorted(set(X))) + [INF, INF]
Y = [-INF] + list(sorted(set(Y))) + [INF, INF]

v_lines = [[False] * len(Y) for _ in range(len(X))]
h_lines = [[False] * len(Y) for _ in range(len(X))]
for a, b, x in ABX:
    l = bisect.bisect_left(Y, min(a, b))
    r = bisect.bisect_left(Y, max(a, b))
    xi = bisect.bisect_left(X, x)
    for yi in range(l, r):
        v_lines[xi][yi] = True
for y, e, f in YEF:
    l = bisect.bisect_left(X, min(e, f))
    r = bisect.bisect_left(X, max(e, f))
    yi = bisect.bisect_left(Y, y)
    for xi in range(l, r):
        h_lines[xi][yi] = True

# (0, 0) を含む場所を適当に選ぶ
xi = bisect.bisect_right(X, 0) - 1
yi = bisect.bisect_right(Y, 0) - 1
s = 0
seen = [[False] * len(Y) for _ in range(len(X))]
seen[xi][yi] = True
stack = [(xi, yi)]
while stack and s < INF:
    xi, yi = stack.pop()
    s += (X[xi + 1] - X[xi]) * (Y[yi + 1] - Y[yi])
    for dx, dy, lines, xk, yk in (
            (0, 1, h_lines, xi, yi + 1),
            (1, 0, v_lines, xi + 1, yi),
            (0, -1, h_lines, xi, yi),
            (-1, 0, v_lines, xi, yi)):
        xj, yj = xi + dx, yi + dy
        if seen[xj][yj] or lines[xk][yk]:
            continue

        seen[xj][yj] = True
        stack.append((xj, yj))

if s < INF:
    print(s)
else:
    print('INF')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
ABX = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
YEF = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

# X, Y を列挙して四角をいっぱい作る
X = []
Y = []

for a, b, x in ABX:
    X.append(x)
    Y.append(a)
    Y.append(b)
for y, e, f in YEF:
    X.append(e)
    X.append(f)
    Y.append(y)

X = [-INF] + list(sorted(set(X))) + [INF, INF]
Y = [-INF] + list(sorted(set(Y))) + [INF, INF]

v_lines = [[False] * len(Y) for _ in range(len(X))]
h_lines = [[False] * len(Y) for _ in range(len(X))]
for a, b, x in ABX:
    l = bisect.bisect_left(Y, min(a, b))
    r = bisect.bisect_left(Y, max(a, b))
    xi = bisect.bisect_left(X, x)
    for yi in range(l, r):
        v_lines[xi][yi] = True
for y, e, f in YEF:
    l = bisect.bisect_left(X, min(e, f))
    r = bisect.bisect_left(X, max(e, f))
    yi = bisect.bisect_left(Y, y)
    for xi in range(l, r):
        h_lines[xi][yi] = True

# (0, 0) を含む場所を適当に選ぶ
xi = bisect.bisect_right(X, 0) - 1
yi = bisect.bisect_right(Y, 0) - 1
s = 0
seen = [[False] * len(Y) for _ in range(len(X))]
seen[xi][yi] = True
stack = [(xi, yi)]
while stack and s < INF:
    xi, yi = stack.pop()
    s += (X[xi + 1] - X[xi]) * (Y[yi + 1] - Y[yi])
    for dx, dy, blocked in (
            (0, 1, h_lines[xi][yi + 1]),
            (1, 0, v_lines[xi + 1][yi]),
            (0, -1, h_lines[xi][yi]),
            (-1, 0, v_lines[xi][yi])):
        xj, yj = xi + dx, yi + dy
        if seen[xj][yj] or blocked:
            continue
        seen[xj][yj] = True
        stack.append((xj, yj))

if s < INF:
    print(s)
else:
    print('INF')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
ABX = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
YEF = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

# X, Y を列挙して四角をいっぱい作る
X = []
Y = []

for a, b, x in ABX:
    X.append(x)
    Y.append(a)
    Y.append(b)
for y, e, f in YEF:
    X.append(e)
    X.append(f)
    Y.append(y)

X = [-INF] + list(sorted(set(X))) + [INF, INF]
Y = [-INF] + list(sorted(set(Y))) + [INF, INF]

lines = []
for a, b, x in ABX:
    l = bisect.bisect_left(Y, min(a, b))
    r = bisect.bisect_left(Y, max(a, b))
    xi = bisect.bisect_left(X, x)
    for yi in range(l, r):
        lines.append((xi, xi, yi, yi + 1))
for y, e, f in YEF:
    l = bisect.bisect_left(X, min(e, f))
    r = bisect.bisect_left(X, max(e, f))
    yi = bisect.bisect_left(Y, y)
    for xi in range(l, r):
        lines.append((xi, xi + 1, yi, yi))
lines = set(lines)

# (0, 0) を含む場所を適当に選ぶ
xi = bisect.bisect_right(X, 0) - 1
yi = bisect.bisect_right(Y, 0) - 1
s = 0
seen = {(xi, yi)}
stack = [(xi, yi)]
while stack and s < INF:
    xi, yi = stack.pop()
    s += (X[xi + 1] - X[xi]) * (Y[yi + 1] - Y[yi])
    # print(X[xi], X[xi + 1], Y[yi], Y[yi + 1], (X[xi + 1] - X[xi]) * (Y[yi + 1] - Y[yi]))
    for dx, dy, key in (
            (0, 1, (xi, xi + 1, yi + 1, yi + 1)),
            (1, 0, (xi + 1, xi + 1, yi, yi + 1)),
            (0, -1, (xi, xi + 1, yi, yi)),
            (-1, 0, (xi, xi, yi, yi + 1))):
        xj, yj = xi + dx, yi + dy
        if (xj, yj) in seen or key in lines:
            continue
        seen.add((xj, yj))
        stack.append((xj, yj))

if s < INF:
    print(s)
else:
    print('INF')

Submission status: Time Limit Exceeded

Solution code:
import bisect
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, M = list(map(int, sys.stdin.buffer.readline().split()))
ABX = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]
YEF = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

# X, Y を列挙して四角をいっぱい作る
X = []
Y = []

for a, b, x in ABX:
    X.append(x)
    Y.append(a)
    Y.append(b)
for y, e, f in YEF:
    X.append(e)
    X.append(f)
    Y.append(y)

X = [-INF] + list(sorted(set(X))) + [INF, INF]
Y = [-INF] + list(sorted(set(Y))) + [INF, INF]

lines = []
for a, b, x in ABX:
    l = bisect.bisect_left(Y, min(a, b))
    r = bisect.bisect_left(Y, max(a, b))
    xi = bisect.bisect_left(X, x)
    for yi in range(l, r):
        lines.append((xi, xi, yi, yi + 1))
for y, e, f in YEF:
    l = bisect.bisect_left(X, min(e, f))
    r = bisect.bisect_left(X, max(e, f))
    yi = bisect.bisect_left(Y, y)
    for xi in range(l, r):
        lines.append((xi, xi + 1, yi, yi))
lines = set(lines)

# (0, 0) を含む場所を適当に選ぶ
xi = bisect.bisect_right(X, 0) - 1
yi = bisect.bisect_right(Y, 0) - 1
s = 0
seen = {(xi, yi)}
stack = [(xi, yi)]
while stack and s < INF:
    xi, yi = stack.pop()
    s += (X[xi + 1] - X[xi]) * (Y[yi + 1] - Y[yi])
    # print(X[xi], X[xi + 1], Y[yi], Y[yi + 1], (X[xi + 1] - X[xi]) * (Y[yi + 1] - Y[yi]))
    for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
        xj, yj = xi + dx, yi + dy
        if (xj, yj) in seen:
            continue
        if dx == 1:
            key = (xi + 1, xi + 1, yi, yi + 1)
            blocked = key in lines
        elif dx == -1:
            key = (xi, xi, yi, yi + 1)
            blocked = key in lines
        elif dy == 1:
            key = (xi, xi + 1, yi + 1, yi + 1)
            blocked = key in lines
        elif dy == -1:
            key = (xi, xi + 1, yi, yi)
            blocked = key in lines
        if blocked:
            continue
        seen.add((xj, yj))
        stack.append((xj, yj))

if s < INF:
    print(s)
else:
    print('INF')

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3


(n, m) = map(int, input().split())

xti = {}
yti = {}
xti[0] = -1
yti[0] = -1

lvs = []
for i in range(n):
    (a, b, c) = map(int, input().split())
    yti[a] = -1
    yti[b] = -1   
    xti[c] = -1
    lvs.append((a, b, c))

lhs = []
for i in range(m):
    (d, e, f) = map(int, input().split())
    yti[d] = -1
    xti[e] = -1   
    xti[f] = -1
    lhs.append((d, e, f))

xs = sorted(xti.keys())
for (i, x) in enumerate(xs):
    xti[x] = i
    
ys = sorted(yti.keys())
for (i, y) in enumerate(ys):
    yti[y] = i

xsl = len(xs) * 2
ysl = len(ys) * 2

fi = [ [ 0 for x in range(xsl) ] for y in range(ysl) ]

for (a, b, c) in lvs:
    x = xti[c] * 2
    for y in range(yti[a] * 2, yti[b] * 2 + 1):
        fi[y][x] = -1

for (d, e, f) in lhs:
    y = yti[d] * 2
    for x in range(xti[e] * 2, xti[f] * 2 + 1):
        fi[y][x] = -1

(cx, cy) = (xti[0] * 2, yti[0] * 2)
q = []
q.append((cx, cy))
fi[cy][cx] = 1
s = 0
dxs = [0, 1, 0, -1]
dys = [1, 0, -1, 0]

xsd = [ xs[i + 1] - xs[i] for i in range(len(xs) - 1) ]
ysd = [ ys[i + 1] - ys[i] for i in range(len(ys) - 1) ]

while q:
    (x, y) = q.pop(-1)

    if x > 0:
        cx = x - 1
        cy = y
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += xsd[cx // 2] * ysd[cy // 2]
            q.append((cx, cy))

    if x < xsl - 1:
        cx = x + 1
        cy = y
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += xsd[cx // 2] * ysd[cy // 2]                
            q.append((cx, cy))
            
    if y > 0:
        cx = x
        cy = y - 1
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += xsd[cx // 2] * ysd[cy // 2]                                
            q.append((cx, cy))

    if y < ysl - 1:
        cx = x
        cy = y + 1
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += xsd[cx // 2] * ysd[cy // 2]                                                
            q.append((cx, cy))
            

print(s)
#for xl in fi:
#    print(xl)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3


(n, m) = map(int, input().split())

xti = {}
yti = {}
xti[0] = -1
yti[0] = -1

lvs = []
for i in range(n):
    (a, b, c) = map(int, input().split())
    yti[a] = -1
    yti[b] = -1   
    xti[c] = -1
    lvs.append((a, b, c))

lhs = []
for i in range(m):
    (d, e, f) = map(int, input().split())
    yti[d] = -1
    xti[e] = -1   
    xti[f] = -1
    lhs.append((d, e, f))

xs = sorted(xti.keys())
for (i, x) in enumerate(xs):
    xti[x] = i
    
ys = sorted(yti.keys())
for (i, y) in enumerate(ys):
    yti[y] = i

xsl = len(xs) * 2
ysl = len(ys) * 2

fi = [ [ 0 for x in range(xsl) ] for y in range(ysl) ]

for (a, b, c) in lvs:
    x = xti[c] * 2
    for y in range(yti[a] * 2, yti[b] * 2 + 1):
        fi[y][x] = -1

for (d, e, f) in lhs:
    y = yti[d] * 2
    for x in range(xti[e] * 2, xti[f] * 2 + 1):
        fi[y][x] = -1

(cx, cy) = (xti[0] * 2, yti[0] * 2)
q = []
q.append((cx, cy))
fi[cy][cx] = 1
s = 0
dxs = [0, 1, 0, -1]
dys = [1, 0, -1, 0]

while q:
    (x, y) = q.pop(-1)

    if x > 0:
        cx = x - 1
        cy = y
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += (xs[(cx + 1) // 2] - xs[cx // 2]) * (ys[(cy + 1) // 2] - ys[cy // 2])
            q.append((cx, cy))

    if x < xsl - 1:
        cx = x + 1
        cy = y
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += (xs[(cx + 1) // 2] - xs[cx // 2]) * (ys[(cy + 1) // 2] - ys[cy // 2])
            q.append((cx, cy))
            
    if y > 0:
        cx = x
        cy = y - 1
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += (xs[(cx + 1) // 2] - xs[cx // 2]) * (ys[(cy + 1) // 2] - ys[cy // 2])
            q.append((cx, cy))

    if y < ysl - 1:
        cx = x
        cy = y + 1
    
        if fi[cy][cx] == 0:
            if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
                print('INF')
                exit()

            fi[cy][cx] = 1
            if cx % 2 == 1 and cy % 2 == 1:
                s += (xs[(cx + 1) // 2] - xs[cx // 2]) * (ys[(cy + 1) // 2] - ys[cy // 2])
            q.append((cx, cy))
            
            

print(s)
#for xl in fi:
#    print(xl)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3


(n, m) = map(int, input().split())

xti = {}
yti = {}
xti[0] = -1
yti[0] = -1

lvs = []
for i in range(n):
    (a, b, c) = map(int, input().split())
    yti[a] = -1
    yti[b] = -1   
    xti[c] = -1
    lvs.append((a, b, c))

lhs = []
for i in range(m):
    (d, e, f) = map(int, input().split())
    yti[d] = -1
    xti[e] = -1   
    xti[f] = -1
    lhs.append((d, e, f))

xs = sorted(xti.keys())
for (i, x) in enumerate(xs):
    xti[x] = i
    
ys = sorted(yti.keys())
for (i, y) in enumerate(ys):
    yti[y] = i

xsl = len(xs) * 2
ysl = len(ys) * 2

fi = [ [ 0 for x in range(xsl) ] for y in range(ysl) ]

for (a, b, c) in lvs:
    x = xti[c] * 2
    for y in range(yti[a] * 2, yti[b] * 2 + 1):
        fi[y][x] = -1

for (d, e, f) in lhs:
    y = yti[d] * 2
    for x in range(xti[e] * 2, xti[f] * 2 + 1):
        fi[y][x] = -1

(cx, cy) = (xti[0] * 2, yti[0] * 2)
q = []
q.append((cx, cy))
fi[cy][cx] = 1
s = 0
dxs = [0, 1, 0, -1]
dys = [1, 0, -1, 0]

while q:
    (x, y) = q.pop(-1)

    for i in range(4):
        cx = x + dxs[i]
        cy = y + dys[i]
        if cx < 0 or cx >= xsl or cy < 0 or cy >= ysl:
            continue
    
        if fi[cy][cx] != 0:
            continue

        if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
            print('INF')
            exit()

        fi[cy][cx] = 1
        if cx % 2 == 1 and cy % 2 == 1:
            s += (xs[(cx + 1) // 2] - xs[cx // 2]) * (ys[(cy + 1) // 2] - ys[cy // 2])
        q.append((cx, cy))

print(s)
#for xl in fi:
#    print(xl)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
  n, m = nm()
  ver = [tuple(nm()) for _ in range(n)]
  hor = [tuple(nm()) for _ in range(m)]
  xli = [0]*(2*n+m+1)
  yli = [0]*(n+2*m+1)
  for i, (a, b, c) in enumerate(ver):
    xli[i*2] = a
    xli[i*2+1] = b
    yli[i] = c
  for i, (a, b, c) in enumerate(hor):
    xli[2*n + i] = a
    yli[n + 2*i] = b
    yli[n + 2*i + 1] = c
  xli = sorted(set(xli))
  yli = sorted(set(yli))
  xdic = {x:i for i, x in enumerate(xli)}
  ydic = {x:i for i, x in enumerate(yli)}
  H = len(yli)*2 + 1
  W = len(xli)*2 + 1
  G = [[0]*W for _ in range(H)]
  for a, b, c in ver:
    a = xdic[a]; b = xdic[b]
    c = ydic[c]
    if a > b:
      a, b = b, a
    for i in range(2*a+1, 2*b+2):
      G[2*c+1][i] = 1
  for a, b, c in hor:
    a = xdic[a]
    b = ydic[b]; c = ydic[c]
    if b > c:
      b, c = c, b
    for i in range(2*b+1, 2*c+2):
      G[i][2*a+1] = 1
  # print(xli)
  # print(yli)
  # prn(G)
  q = deque()
  sx = xdic[0]*2+1; sy = ydic[0]*2+1
  G[sy-1][sx-1] = 2
  q.append((sy-1, sx-1))
  ans = 0
  while q:
    y, x = q.popleft()
    for ny, nx in ((y+2, x), (y-2, x), (y, x+2), (y, x-2)):
      if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == 0 and G[(ny+y)//2][(nx+x)//2] != 1:
        G[ny][nx] = 2
        q.append((ny, nx))

  for ny in range(H):
    for nx in range(W):
      if G[ny][nx] > 1:
        if ny == 0 or ny == H-1 or nx == 0 or nx == W-1:
          print('INF')
          return
        if ny % 2 == nx % 2 == 0:
          ans += (yli[ny//2] - yli[ny//2-1]) * (xli[nx//2] - xli[nx//2-1])
  print(ans)
  return

solve()

Submission status: Time Limit Exceeded

Solution code:
import sys

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,m=MI()
    xx=set([0])
    yy=set([0])
    vl=[]
    hl=[]
    for _ in range(n):
        a,b,c=MI()
        xx.add(a)
        xx.add(b)
        yy.add(c)
        vl.append((a,b,c))
    for _ in range(m):
        a,b,c=MI()
        xx.add(a)
        yy.add(b)
        yy.add(c)
        hl.append((b,c,a))

    dex=list(sorted(xx))
    dey=list(sorted(yy))
    cox={x:i for i,x in enumerate(dex)}
    coy={x:i for i,x in enumerate(dey)}
    h=len(dex)*2
    w=len(dey)*2

    tt=[[9]*w for _ in range(h)]
    for x0,x1,y in vl:
        for i in range(cox[x0]*2,cox[x1]*2):
            tt[i][coy[y]*2]=0
    for y0,y1,x in hl:
        for j in range(coy[y0]*2,coy[y1]*2):
            tt[cox[x]*2][j]=0
    #p2D(tt)

    si,sj=cox[0]*2,coy[0]*2
    stack=[(si,sj)]
    tt[si][sj]=1
    dij=[(1,0),(0,1),(-1,0),(0,-1)]
    while stack:
        i,j=stack.pop()
        for di,dj in dij:
            ni,nj=i+di,j+dj
            if ni<0 or nj<0 or ni>=h or nj>=w:continue
            if tt[ni][nj]!=9:continue
            if ni==0 or nj==0 or ni==h-1 or nj==w-1:
                print("INF")
                #p2D(tt)
                exit()
            tt[ni][nj]=1
            stack.append((ni,nj))
    #p2D(tt)

    ans=0
    for i in range(1,h,2):
        for j in range(1,w,2):
            if tt[i][j]==1:
                ans+=(dex[i//2+1]-dex[i//2])*(dey[j//2+1]-dey[j//2])
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
from collections import defaultdict

(n, m) = map(int, input().split())

xti = {}
yti = {}
xti[0] = -1
yti[0] = -1

lvs = []
for i in range(n):
    (a, b, c) = map(int, input().split())
    yti[a] = -1
    yti[b] = -1   
    xti[c] = -1
    lvs.append((a, b, c))

lhs = []
for i in range(m):
    (d, e, f) = map(int, input().split())
    yti[d] = -1
    xti[e] = -1   
    xti[f] = -1
    lhs.append((d, e, f))

xs = sorted(xti.keys())
for (i, x) in enumerate(xs):
    xti[x] = i
    
ys = sorted(yti.keys())
for (i, y) in enumerate(ys):
    yti[y] = i

xsl = len(xs) * 2
ysl = len(ys) * 2

fi = [ [ 0 for x in range(xsl) ] for y in range(ysl) ]

for (a, b, c) in lvs:
    x = xti[c] * 2
    for y in range(yti[a] * 2, yti[b] * 2 + 1):
        fi[y][x] = -1

for (d, e, f) in lhs:
    y = yti[d] * 2
    for x in range(xti[e] * 2, xti[f] * 2 + 1):
        fi[y][x] = -1

q = [(xti[0] * 2, yti[0] * 2)]

s = 0
while q:
    (cx, cy) = q.pop(-1)
    if cx < 0 or cx >= xsl or cy < 0 or cy >= ysl:
        continue
    
    if fi[cy][cx] != 0:
        continue

    if cx == 0 or cx == xsl - 1 or cy == 0 or cy == ysl - 1:
        print('INF')
        exit()

    fi[cy][cx] = 1
    s += (xs[(cx + 1) // 2] - xs[cx // 2]) * (ys[(cy + 1) // 2] - ys[cy // 2])
    q.append((cx, cy - 1))
    q.append((cx + 1, cy))
    q.append((cx, cy + 1))
    q.append((cx - 1, cy))

print(s)
#for xl in fi:
#    print(xl)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
nm = lambda: map(int, readline().split())

def main():
  n, m = nm()
  ver = [tuple(nm()) for _ in range(n)]
  hor = [tuple(nm()) for _ in range(m)]
  xli = [0]*(2*n+m+1)
  yli = [0]*(n+2*m+1)
  for i, (a, b, c) in enumerate(ver):
    xli[i*2] = a
    xli[i*2+1] = b
    yli[i] = c
  for i, (a, b, c) in enumerate(hor):
    xli[2*n + i] = a
    yli[n + 2*i] = b
    yli[n + 2*i + 1] = c
  xli = sorted(set(xli))
  yli = sorted(set(yli))
  xdic = {x:i for i, x in enumerate(xli)}
  ydic = {x:i for i, x in enumerate(yli)}
  H = len(yli)*2 + 1
  W = len(xli)*2 + 1
  G = [[0]*W for _ in range(H)]
  for a, b, c in ver:
    a = xdic[a]; b = xdic[b]
    c = ydic[c]
    if a > b:
      a, b = b, a
    for i in range(2*a+1, 2*b+2):
      G[2*c+1][i] = 1
  for a, b, c in hor:
    a = xdic[a]
    b = ydic[b]; c = ydic[c]
    if b > c:
      b, c = c, b
    for i in range(2*b+1, 2*c+2):
      G[i][2*a+1] = 1
  # print(xli)
  # print(yli)
  # prn(G)
  q = deque()
  sx = xdic[0]*2+1; sy = ydic[0]*2+1
  G[sy][sx] = 1
  q.append((sy, sx))
  ans = 0
  while q:
    y, x = q.popleft()
    for ny, nx in ((y+1, x), (y-1, x), (y, x+1), (y, x-1)):
      if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == 0:
        G[ny][nx] = -1
        q.append((ny, nx))

  for ny in range(H):
    for nx in range(W):
      if G[ny][nx] < 0:
        if ny == 0 or ny == H-1 or nx == 0 or nx == W-1:
          print('INF')
          return
        if ny % 2 == nx % 2 == 0:
          ans += (yli[ny//2] - yli[ny//2-1]) * (xli[nx//2] - xli[nx//2-1])
  print(ans)
  return

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
  disti = ((1, 0), (-1, 0), (0, 1), (0, -1))
  n, m = nm()
  ver = [tuple(nm()) for _ in range(n)]
  hor = [tuple(nm()) for _ in range(m)]
  xli = [0]*(2*n+m+1)
  yli = [0]*(n+2*m+1)
  for i, (a, b, c) in enumerate(ver):
    xli[i*2] = a
    xli[i*2+1] = b
    yli[i] = c
  for i, (a, b, c) in enumerate(hor):
    xli[2*n + i] = a
    yli[n + 2*i] = b
    yli[n + 2*i + 1] = c
  xli = sorted(set(xli))
  yli = sorted(set(yli))
  xdic = {x:i for i, x in enumerate(xli)}
  ydic = {x:i for i, x in enumerate(yli)}
  H = len(yli)*2 + 1
  W = len(xli)*2 + 1
  G = [[0]*W for _ in range(H)]
  for a, b, c in ver:
    a = xdic[a]; b = xdic[b]
    c = ydic[c]
    if a > b:
      a, b = b, a
    for i in range(2*a+1, 2*b+2):
      G[2*c+1][i] = 1
  for a, b, c in hor:
    a = xdic[a]
    b = ydic[b]; c = ydic[c]
    if b > c:
      b, c = c, b
    for i in range(2*b+1, 2*c+2):
      G[i][2*a+1] = 1
  # print(xli)
  # print(yli)
  # prn(G)
  q = deque()
  sx = xdic[0]*2+1; sy = ydic[0]*2+1
  G[sy][sx] = 1
  q.append(sy*W+sx)
  ans = 0
  while q:
    v = q.popleft()
    y, x = divmod(v, W)
    for dy, dx in disti:
      ny, nx = y + dy, x + dx
      if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == 0:
        G[ny][nx] = -1
        q.append(ny*W+nx)

  for ny in range(H):
    for nx in range(W):
      if G[ny][nx] < 0:
        if ny == 0 or ny == H-1 or nx == 0 or nx == W-1:
          print('INF')
          return
        if ny % 2 == nx % 2 == 0:
          ans += (yli[ny//2] - yli[ny//2-1]) * (xli[nx//2] - xli[nx//2-1])
  print(ans)
  return

if __name__ == '__main__':
  solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
  n, m = nm()
  ver = [tuple(nm()) for _ in range(n)]
  hor = [tuple(nm()) for _ in range(m)]
  xli = [0]*(2*n+m+1)
  yli = [0]*(n+2*m+1)
  for i, (a, b, c) in enumerate(ver):
    xli[i*2] = a
    xli[i*2+1] = b
    yli[i] = c
  for i, (a, b, c) in enumerate(hor):
    xli[2*n + i] = a
    yli[n + 2*i] = b
    yli[n + 2*i + 1] = c
  xli = sorted(set(xli))
  yli = sorted(set(yli))
  xdic = {x:i for i, x in enumerate(xli)}
  ydic = {x:i for i, x in enumerate(yli)}
  H = len(yli)*2 + 1
  W = len(xli)*2 + 1
  G = [[0]*W for _ in range(H)]
  for a, b, c in ver:
    a = xdic[a]; b = xdic[b]
    c = ydic[c]
    if a > b:
      a, b = b, a
    for i in range(2*a+1, 2*b+2):
      G[2*c+1][i] = 1
  for a, b, c in hor:
    a = xdic[a]
    b = ydic[b]; c = ydic[c]
    if b > c:
      b, c = c, b
    for i in range(2*b+1, 2*c+2):
      G[i][2*a+1] = 1
  # print(xli)
  # print(yli)
  # prn(G)
  q = deque()
  sx = xdic[0]*2+1; sy = ydic[0]*2+1
  G[sy][sx] = 1
  q.append((sy, sx))
  ans = 0
  while q:
    y, x = q.popleft()
    for ny, nx in ((y+1, x), (y-1, x), (y, x+1), (y, x-1)):
      if 0 <= ny < H and 0 <= nx < W and G[ny][nx] == 0:
        G[ny][nx] = -1
        q.append((ny, nx))

  for ny in range(H):
    for nx in range(W):
      if G[ny][nx] < 0:
        if ny == 0 or ny == H-1 or nx == 0 or nx == W-1:
          print('INF')
          return
        if ny % 2 == nx % 2 == 0:
          ans += (yli[ny//2] - yli[ny//2-1]) * (xli[nx//2] - xli[nx//2-1])
  print(ans)
  return

solve()