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:
#168 - F
import sys
import numpy as np
#from numba import njit


N, M = map(int, sys.stdin.buffer.readline().split())
LineData = np.int64(sys.stdin.buffer.read().split())

INF = 10**9 + 1

LineData = LineData.reshape(-1, 3)
A, B, C = LineData[:N].T
D, E, F = LineData[N:].T
#X = np.unique(np.concatenate([D, [-INF, 0, INF]]))
#Y = np.unique(np.concatenate([C, [-INF, 0, INF]]))
X = np.unique(np.concatenate([D, [-INF, INF]]))
Y = np.unique(np.concatenate([C, [-INF, INF]]))
A = np.searchsorted(X, A)
B = np.searchsorted(X, B, 'right') - 1
C = np.searchsorted(Y, C)
D = np.searchsorted(X, D)
E = np.searchsorted(Y, E)
F = np.searchsorted(Y, F, 'right') - 1

x = np.searchsorted(X, 0, 'right') - 1
y = np.searchsorted(Y, 0, 'right') - 1

DX = X[1:] - X[:-1]
DY = Y[1:] - Y[:-1]

A = A.tolist()
B = B.tolist()
C = C.tolist()
D = D.tolist()
E = E.tolist()
F = F.tolist()
X = X.tolist()
Y = Y.tolist()
DX = DX.tolist()
DY = DY.tolist()

LenX = len(X)
LenY = len(Y)

visit = [[False] * LenY for _ in range(LenX)]
visit[x][y] = True
area = 0
queue = [(x, y)]

LineX = [[False] * LenY for _ in range(LenX)]
LineY = [[False] * LenY for _ in range(LenX)]

for x1, x2, y in zip(A, B, C):
    for x in range(x1, x2):
        LineY[x][y] = True
#for i in range(len(A)):
#    y = C[i]
#    for x in range(A[i], B[i]):
#        LineY[x][y] = True

for x, y1, y2 in zip(D, E, F):
    for y in range(y1, y2):
        LineX[x][y] = True
#for i in range(len(D)):
#    x = D[i]
#    for y in range(E[i], F[i]):
#        LineX[x][y] = True

LenX -= 1
LenY -= 1

while queue:
    x, y = queue.pop()
    
    if x == 0 or x == LenX or y == 0 or y == LenY:
        area = 0
        break
    
    area += DX[x] * DY[y]
    
    x1 = x - 1
    if not LineX[x][y] and not visit[x1][y]:
        visit[x1][y] = True
        queue.append((x1, y))
    y1 = y - 1
    if not LineY[x][y] and not visit[x][y1]:
        visit[x][y1] = True
        queue.append((x, y1))
    x1 = x + 1
    if not LineX[x1][y] and not visit[x1][y]:
        visit[x1][y] = True
        queue.append((x1, y))
    y1 = y + 1
    if not LineY[x][y1] and not visit[x][y1]:
        visit[x][y1] = True
        queue.append((x, y1))

if area == 0:
    print("INF")
else:
    print(area)

Submission status: Time Limit Exceeded

Solution code:
import sys

max_int = 1000000001  # 10^9+1
min_int = -max_int

n, m = map(int, sys.stdin.readline().split())

x_coords = set()
y_coords = set()

hor = {}  # [x => [[a,b], [a,b]], ...]
ver = {}

for _n in range(n):
    a, b, c = map(int, sys.stdin.readline().split())
    x_coords.add(a)
    x_coords.add(b)
    y_coords.add(c)
    if c not in ver:
        ver[c] = [[a, b]]
    else:
        ver[c].append([a, b])

for _m in range(m):
    d, e, f = map(int, sys.stdin.readline().split())
    x_coords.add(d)
    y_coords.add(e)
    y_coords.add(f)
    if d not in hor:
        hor[d] = [[e, f]]
    else:
        hor[d].append([e, f])

x_coords = list(x_coords)
x_coords.sort()

y_coords = list(y_coords)
y_coords.sort()

x_indices = {}
y_indices = {}

x0 = 0
index = 0
for coord in x_coords:
    x_indices[coord] = index
    if coord <= 0:
        x0 = index
    index += 1

y0 = 0
index = 0
for coord in y_coords:
    y_indices[coord] = index
    if coord <= 0:
        y0 = index
    index += 1

if x0 == len(x_coords) - 1 or y0 == len(y_coords) - 1:
    print('INF')
    exit()

hor_borders = set()
ver_borders = set()

for x_coord in hor:
    x = x_indices[x_coord]
    for y1_coord, y2_coord in hor[x_coord]:
        y1 = y_indices[y1_coord]
        y2 = y_indices[y2_coord]
        for i in range(y1, y2):
            hor_borders.add((x, i))

for y_coord in ver:
    y = y_indices[y_coord]
    for x1_coord, x2_coord in ver[y_coord]:
        x1 = x_indices[x1_coord]
        x2 = x_indices[x2_coord]
        for i in range(x1, x2):
            ver_borders.add((i, y))

# print(hor_borders, ver_borders)
# exit()

# exit()
contenders = [(x0, y0)]
visited = set()
while contenders:
    new_contenders = []
    for x, y in contenders:
        if (x, y) in visited:
            continue
        # print(x, y)
        visited.add((x, y))
        if x < 0 or y < 0 or x == len(x_coords) or y == len(y_coords):
            print('INF')
            exit()

        # вверх
        if (x, y) not in hor_borders:
            # print('up to ', x - 1, y)
            new_contenders.append((x - 1, y))

        # влево
        if (x, y) not in ver_borders:
            # print('left to ', x, y - 1)
            new_contenders.append((x, y - 1))

        # вниз
        if (x + 1, y) not in hor_borders:
            # print('down to ', x + 1, y)
            new_contenders.append((x + 1, y))

        # вправо
        if (x, y + 1) not in ver_borders:
            # print('right to ')
            new_contenders.append((x, y + 1))
    contenders = new_contenders

    # print(new_contenders, ans)
    # break

area = 0
for x, y in visited:
    area += (x_coords[x + 1] - x_coords[x]) * (y_coords[y + 1] - y_coords[y])

print(area)

Submission status: Time Limit Exceeded

Solution code:
import sys

max_int = 1000000001  # 10^9+1
min_int = -max_int

n, m = map(int, sys.stdin.readline().split())

xs = {0}
ys = {0}

hor = {}  # [x => [[a,b], [a,b]], ...]
ver = {}

for _n in range(n):
    a, b, c = map(int, sys.stdin.readline().split())
    xs.add(a)
    xs.add(b)
    ys.add(c)
    if c not in ver:
        ver[c] = [[a, b]]
    else:
        ver[c].append([a, b])

for _m in range(m):
    d, e, f = map(int, sys.stdin.readline().split())
    xs.add(d)
    ys.add(e)
    ys.add(f)
    if d not in hor:
        hor[d] = [[e, f]]
    else:
        hor[d].append([e, f])

xs = list(xs)
xs.sort()
x0 = 0
for i in range(len(xs)):
    if xs[i] == 0:
        x0 = i
        break

ys = list(ys)
ys.sort()
y0 = 0
for j in range(len(ys)):
    if ys[j] == 0:
        y0 = j
        break

if x0 == 0 or y0 == 0 or x0 == len(xs) - 1 or y0 == len(ys) - 1:
    print('INF')
    exit()

ans = 0
pretends = [(x0 - 1, y0 - 1, x0, y0)]
visited = set()
hor_borders = set()
ver_borders = set()
while pretends:
    new_pretends = []
    for xl_, yl_, xr_, yr_ in pretends:
        if (xl_, yl_, xr_, yr_) in visited:
            continue
        visited.add((xl_, yl_, xr_, yr_))
        if xl_ < 0 or yl_ < 0 or xr_ >= len(xs) or yr_ >= len(ys):
            print('INF')
            exit()
        xl = xs[xl_]
        xr = xs[xr_]
        yl = ys[yl_]
        yr = ys[yr_]
        ans += (xr - xl) * (yr - yl)
        # вверх
        if (xl, yl) not in hor_borders:
            if xl in hor:
                for lb, rb in hor[xl]:
                    if lb <= yl and rb >= yr:
                        hor_borders.add((xl, yl))
                        break
                else:
                    new_pretends.append((xl_ - 1, yl_, xr_ - 1, yr_))
            else:
                new_pretends.append((xl_ - 1, yl_, xr_ - 1, yr_))

        # вниз
        if (xr, yl) not in hor_borders:
            if xr in hor:
                for lb, rb in hor[xr]:
                    if lb <= yl and rb >= yr:
                        hor_borders.add((xr, yl))
                        break
                else:
                    new_pretends.append((xl_ + 1, yl_, xr_ + 1, yr_))
            else:
                new_pretends.append((xl_ + 1, yl_, xr_ + 1, yr_))

        # влево
        if (xl, yl) not in ver_borders:
            if yl in ver:
                for ub, db in ver[yl]:
                    if ub <= xl and db >= xr:
                        ver_borders.add((xl, yl))
                        break
                else:
                    new_pretends.append((xl_, yl_ - 1, xr_, yr_ - 1))
            else:
                new_pretends.append((xl_, yl_ - 1, xr_, yr_ - 1))

        # вправо
        if (xl, yr) not in ver_borders:
            if yr in ver:
                for ub, db in ver[yr]:
                    if ub <= xl and db >= xr:
                        ver_borders.add((xl, yr))
                        break
                else:
                    new_pretends.append((xl_, yl_ + 1, xr_, yr_ + 1))
            else:
                new_pretends.append((xl_, yl_ + 1, xr_, yr_ + 1))
    pretends = new_pretends
    # print(new_pretends, ans)
    # break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#168 - F
import sys
import numpy as np
#from numba import njit


def main():
    N, M = map(int, sys.stdin.buffer.readline().split())
    LineData = np.int64(sys.stdin.buffer.read().split())

    INF = 10**9 + 1

    LineData = LineData.reshape(-1, 3)
    A, B, C = LineData[:N].T
    D, E, F = LineData[N:].T
    #X = np.unique(np.concatenate([D, [-INF, 0, INF]]))
    #Y = np.unique(np.concatenate([C, [-INF, 0, INF]]))
    X = np.unique(np.concatenate([D, [-INF, INF]]))
    Y = np.unique(np.concatenate([C, [-INF, INF]]))
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B, 'right') - 1
    C = np.searchsorted(Y, C)
    D = np.searchsorted(X, D)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F, 'right') - 1

    area = cal_area(A, B, C, D, E, F, X, Y)

    if area == 0:
        print("INF")
    else:
        print(area)


#@njit
def cal_area(A, B, C, D, E, F, X, Y):
    x = np.searchsorted(X, 0, 'right') - 1
    y = np.searchsorted(Y, 0, 'right') - 1

    A = A.tolist()
    B = B.tolist()
    C = C.tolist()
    D = D.tolist()
    E = E.tolist()
    F = F.tolist()
    X = X.tolist()
    Y = Y.tolist()

    LenX = len(X)
    LenY = len(Y)

    visit = [[False] * LenY for _ in range(LenX)]
    visit[x][y] = True
    area = 0
    queue = [(x, y)]

    LineX = [[False] * LenY for _ in range(LenX)]
    LineY = [[False] * LenY for _ in range(LenX)]

    for x1, x2, y in zip(A, B, C):
        for x in range(x1, x2):
            LineY[x][y] = True

    for x, y1, y2 in zip(D, E, F):
        for y in range(y1, y2):
            LineX[x][y] = True

    while queue:
        x, y = queue.pop()

        if x == 0 or x == LenX - 1 or y == 0 or y == LenY - 1:
            area = 0
            break
        
        area += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])

        if not LineX[x][y] and not visit[x - 1][y]:
            visit[x - 1][y] = True
            queue.append((x - 1, y))
        if not LineY[x][y] and not visit[x][y - 1]:
            visit[x][y - 1] = True
            queue.append((x, y - 1))
        if not LineX[x + 1][y] and not visit[x + 1][y]:
            visit[x + 1][y] = True
            queue.append((x + 1, y))
        if not LineY[x][y + 1] and not visit[x][y + 1]:
            visit[x][y + 1] = True
            queue.append((x, y + 1))

    return area


if __name__ == '__main__':
	main()

sys.exit()

Submission status: Time Limit Exceeded

Solution code:
#168 - F
import sys
import numpy as np
#from numba import njit


def main():
    N, M = map(int, sys.stdin.buffer.readline().split())
    LineData = np.int64(sys.stdin.buffer.read().split())

    INF = 10**9 + 1

    LineData = LineData.reshape(-1, 3)
    A, B, C = LineData[:N].T
    D, E, F = LineData[N:].T
    #X = np.unique(np.concatenate([D, [-INF, 0, INF]]))
    #Y = np.unique(np.concatenate([C, [-INF, 0, INF]]))
    X = np.unique(np.concatenate([D, [-INF, INF]]))
    Y = np.unique(np.concatenate([C, [-INF, INF]]))
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B, 'right') - 1
    C = np.searchsorted(Y, C)
    D = np.searchsorted(X, D)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F, 'right') - 1

    area = cal_area(A, B, C, D, E, F, X, Y)

    if area == 0:
        print("INF")
    else:
        print(area)


#@njit
def cal_area(A, B, C, D, E, F, X, Y):
    LenX = len(X)
    LenY = len(Y)

    LineX = [[False] * LenY for _ in range(LenX)]
    LineY = [[False] * LenY for _ in range(LenX)]

    for x1, x2, y in zip(A, B, C):
        for x in range(x1, x2):
            LineY[x][y] = True

    for x, y1, y2 in zip(D, E, F):
        for y in range(y1, y2):
            LineX[x][y] = True
    
    visit = [[False] * LenY for _ in range(LenX)]
    x = np.searchsorted(X, 0, 'right') - 1
    y = np.searchsorted(Y, 0, 'right') - 1
    visit[x][y] = True
    area = 0
    queue = [(x, y)]

    while queue:
        x, y = queue.pop()

        if x == 0 or x == LenX - 1 or y == 0 or y == LenY - 1:
            area = 0
            break
        
        area += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])

        if not LineX[x][y] and not visit[x - 1][y]:
            visit[x - 1][y] = True
            queue.append((x - 1, y))
        if not LineY[x][y] and not visit[x][y - 1]:
            visit[x][y - 1] = True
            queue.append((x, y - 1))
        if not LineX[x + 1][y] and not visit[x + 1][y]:
            visit[x + 1][y] = True
            queue.append((x + 1, y))
        if not LineY[x][y + 1] and not visit[x][y + 1]:
            visit[x][y + 1] = True
            queue.append((x, y + 1))

    return area


if __name__ == '__main__':
	main()

sys.exit()

Submission status: Time Limit Exceeded

Solution code:
import bisect
INF = 10**9 + 1
n, m = map(int, input().split())
X, Y = [-INF, 0, INF], [-INF, 0, INF]
ABC, DEF = [], []
for i in range(n):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
    X.append(a)
    X.append(b)
    Y.append(c)
    Y.append(c)
for j in range(m):
    d, e, f = map(int, input().split())
    DEF.append((d, e, f))
    X.append(d)
    X.append(d)
    Y.append(e)
    Y.append(f)
X.sort()
Y.sort()
V = [[False for y in Y] for x in X]
for a, b, c in ABC:
    j = bisect.bisect_left(Y, c)
    ia = bisect.bisect_left(X, a)
    ib = bisect.bisect_left(X, b)
    for i in range(ia, ib):
        V[i][j] = True
for d, e, f in DEF:
    i = bisect.bisect_left(X, d)
    je = bisect.bisect_left(Y, e)
    jf = bisect.bisect_left(Y, f)
    for j in range(je, jf):
        V[i][j] = True
i = bisect.bisect_left(X, 0)
j = bisect.bisect_left(Y, 0)
Q = [(i, j)]
V[i][j] = True
ans = 0
while Q:
    i, j = Q.pop()
    ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j])
    for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
        if 0 < i + di < len(X) - 1 and 0 < j + dj < len(Y) - 1:
            if not V[i + di][j + dj]:
                V[i + di][j + dj] = True
                Q.append((i + di, j + dj))
        else:
            print('INF')
            exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#168 - F
import sys
import numpy as np
from collections import deque


def main():
    N, M = map(int, sys.stdin.buffer.readline().split())
    LineData = np.int64(sys.stdin.buffer.read().split())

    INF = 10**9 + 1

    LineData = LineData.reshape(-1, 3)
    A, B, C = LineData[:N].T
    D, E, F = LineData[N:].T
    X = np.unique(np.concatenate([D, [-INF, 0, INF]]))
    Y = np.unique(np.concatenate([C, [-INF, 0, INF]]))
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B, 'right') - 1
    C = np.searchsorted(Y, C)
    D = np.searchsorted(X, D)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F, 'right') - 1

    LenX = len(X)
    LenY = len(Y)

    #LineX = np.zeros((LenX, LenY), np.bool)
    #LineY = np.zeros((LenX, LenY), np.bool)
    LineX = [[False] * LenY for _ in range(LenX)]
    LineY = [[False] * LenY for _ in range(LenX)]

    for x1, x2, y in zip(A, B, C):
        for x in range(x1, x2):
            LineY[x][y] = True

    for x, y1, y2 in zip(D, E, F):
        for y in range(y1, y2):
            LineX[x][y] = True
    
    #visit = np.zeros((LenX, LenY), np.bool)
    visit = [[False] * LenY for _ in range(LenX)]
    x = np.searchsorted(X, 0, 'right') - 1
    y = np.searchsorted(Y, 0, 'right') - 1
    visit[x][y] = True
    area = 0
    queue = deque([(x, y)])

    while queue:
        x, y = queue.pop()

        if x == 0 or x == LenX - 1 or y == 0 or y == LenY - 1:
            area = 0
            break
        
        area += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])

        if not LineX[x][y] and not visit[x - 1][y]:
            visit[x - 1][y] = True
            queue.append((x - 1, y))
        if not LineY[x][y] and not visit[x][y - 1]:
            visit[x][y - 1] = True
            queue.append((x, y - 1))
        if not LineX[x + 1][y] and not visit[x + 1][y]:
            visit[x + 1][y] = True
            queue.append((x + 1, y))
        if not LineY[x][y + 1] and not visit[x][y + 1]:
            visit[x][y + 1] = True
            queue.append((x, y + 1))


    if area == 0:
        print("INF")
    else:
        print(area)


if __name__ == '__main__':
	main()

sys.exit()

Submission status: Time Limit Exceeded

Solution code:
#168 - F
import sys
import numpy as np


def main():
    N, M = map(int, sys.stdin.buffer.readline().split())
    LineData = np.int64(sys.stdin.buffer.read().split())

    INF = 10**9 + 1

    LineData = LineData.reshape(-1, 3)
    A, B, C = LineData[:N].T
    D, E, F = LineData[N:].T
    X = np.unique(np.concatenate([D, [-INF, 0, INF]]))
    Y = np.unique(np.concatenate([C, [-INF, 0, INF]]))
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B, 'right') - 1
    C = np.searchsorted(Y, C)
    D = np.searchsorted(X, D)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F, 'right') - 1

    LenX = len(X)
    LenY = len(Y)

    #LineX = np.zeros((LenX, LenY), np.bool)
    #LineY = np.zeros((LenX, LenY), np.bool)
    LineX = [[False] * LenY for _ in range(LenX)]
    LineY = [[False] * LenY for _ in range(LenX)]

    for x1, x2, y in zip(A, B, C):
        for x in range(x1, x2):
            LineY[x][y] = True

    for x, y1, y2 in zip(D, E, F):
        for y in range(y1, y2):
            LineX[x][y] = True
    
    #visit = np.zeros((LenX, LenY), np.bool)
    visit = [[False] * LenY for _ in range(LenX)]
    x = np.searchsorted(X, 0, 'right') - 1
    y = np.searchsorted(Y, 0, 'right') - 1
    visit[x][y] = True
    area = 0
    queue = [(x, y)]

    while queue:
        x, y = queue.pop()

        if x == 0 or x == LenX - 1 or y == 0 or y == LenY - 1:
            area = 0
            break
        
        area += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])

        if not LineX[x][y] and not visit[x - 1][y]:
            visit[x - 1][y] = True
            queue.append((x - 1, y))
        if not LineY[x][y] and not visit[x][y - 1]:
            visit[x][y - 1] = True
            queue.append((x, y - 1))
        if not LineX[x + 1][y] and not visit[x + 1][y]:
            visit[x + 1][y] = True
            queue.append((x + 1, y))
        if not LineY[x][y + 1] and not visit[x][y + 1]:
            visit[x][y + 1] = True
            queue.append((x, y + 1))


    if area == 0:
        print("INF")
    else:
        print(area)


if __name__ == '__main__':
	main()

sys.exit()

Submission status: Time Limit Exceeded

Solution code:
#168 - F
import sys
import numpy as np


def main():
    N, M = map(int, sys.stdin.buffer.readline().split())
    LineData = np.int64(sys.stdin.buffer.read().split())

    INF = 10**9 + 1

    LineData = LineData.reshape(-1, 3)
    A, B, C = LineData[:N].T
    D, E, F = LineData[N:].T
    X = np.unique(np.concatenate([D, [-INF, 0, INF]]))
    Y = np.unique(np.concatenate([C, [-INF, 0, INF]]))
    A = np.searchsorted(X, A)
    B = np.searchsorted(X, B, 'right') - 1
    C = np.searchsorted(Y, C)
    D = np.searchsorted(X, D)
    E = np.searchsorted(Y, E)
    F = np.searchsorted(Y, F, 'right') - 1

    LenX = len(X)
    LenY = len(Y)
    XY = LenX * LenY

    LineX = np.zeros((LenX, LenY), np.bool)
    LineY = np.zeros((LenX, LenY), np.bool)

    for x1, x2, y in zip(A, B, C):
        for x in range(x1, x2):
            LineY[x, y] = True

    for x, y1, y2 in zip(D, E, F):
        for y in range(y1, y2):
            LineX[x, y] = True
    
    visit = np.zeros((LenX, LenY), np.bool)
    x = np.searchsorted(X, 0, 'right') - 1
    y = np.searchsorted(Y, 0, 'right') - 1
    visit[x, y] = True
    area = 0
    queue = [(x, y)]

    for _ in range(XY):
        x, y = queue.pop()

        if x == 0 or x == LenX - 1 or y == 0 or y == LenY - 1:
            area = 0
            break
        
        area += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])

        if not LineX[x, y] and not visit[x - 1, y]:
            visit[x - 1, y] = True
            queue.append((x - 1, y))
        if not LineY[x, y] and not visit[x, y - 1]:
            visit[x, y - 1] = True
            queue.append((x, y - 1))
        if not LineX[x + 1, y] and not visit[x + 1, y]:
            visit[x + 1, y] = True
            queue.append((x + 1, y))
        if not LineY[x, y + 1] and not visit[x, y + 1]:
            visit[x, y + 1] = True
            queue.append((x, y + 1))

        if not queue:
            break
    
    if area == 0:
        print("INF")
    else:
        print(area)


if __name__ == '__main__':
	main()

sys.exit()