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.

Solution in Python (3.8.2) (judge verdict: Time Limit Exceeded)

import sys
import numpy as np
from numba import njit, types
from numba.typed import Dict

n, m, *abcdef = map(int, sys.stdin.buffer.read().split())

n3 = n * 3
aaa = abcdef[0:n3:3]
bbb = abcdef[1:n3:3]
ccc = abcdef[2:n3:3]
ddd = abcdef[n3 + 0::3]
eee = abcdef[n3 + 1::3]
fff = abcdef[n3 + 2::3]
ver_lines = list(zip(aaa, bbb, ccc))
hor_lines = list(zip(ddd, eee, fff))
x_list = {0}
x_list.update(ccc)
x_list.update(eee)
x_list.update(fff)
y_list = {0}
y_list.update(aaa)
y_list.update(bbb)
y_list.update(ddd)


@njit
def create_dict(x_list):
    d = Dict.empty(key_type=types.int64, value_type=types.int64)
    for i in range(len(x_list)):
        d[x_list[i]] = i + 1
    return d


x_list = np.array(sorted(x_list), dtype=np.int64)
y_list = np.array(sorted(y_list), dtype=np.int64)
x_dict = create_dict(x_list)
y_dict = create_dict(y_list)
row_real = len(x_list)
col_real = len(y_list)
row = row_real + 2
col = col_real + 2
total = row * col

banned_up = np.zeros(total, dtype=np.int16)
banned_down = np.zeros(total, dtype=np.int16)
banned_left = np.zeros(total, dtype=np.int16)
banned_right = np.zeros(total, dtype=np.int16)


@njit
def register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict):
    global row
    if a > b:
        a, b = b, a
    ai = y_dict[a] * row
    bi = y_dict[b] * row
    j = x_dict[c]
    banned_left[ai + j] += 1
    banned_left[bi + j] -= 1
    banned_right[ai + j - 1] += 1
    banned_right[bi + j - 1] -= 1


@njit
def register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict):
    global row
    if e > f:
        e, f = f, e
    ri = y_dict[d] * row
    ej = x_dict[e]
    fj = x_dict[f]
    banned_up[ri + ej] += 1
    banned_up[ri + fj] -= 1
    banned_down[ri - row + ej] += 1
    banned_down[ri - row + fj] -= 1


@njit
def accumulate_banned(banned_up, banned_down, banned_left, banned_right):
    global row, col
    for i in range(1, col):
        ri0 = row * (i - 1)
        ri1 = row * i
        for j in range(1, row):
            banned_up[ri1 + j] += banned_up[ri1 + j - 1]
            banned_down[ri1 + j] += banned_down[ri1 + j - 1]
            banned_left[ri1 + j] += banned_left[ri0 + j]
            banned_right[ri1 + j] += banned_right[ri0 + j]


@njit
def dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right):
    global row, col
    s = row * y_dict[0] + x_dict[0]
    enable = [-1] * row + ([-1] + [0] * (row - 2) + [-1]) * (col - 2) + [-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:
            return -1
        enable[c] = 1
        for dc, banned in moves:
            if banned[c]:
                continue
            nc = c + dc
            if enable[nc] == 1:
                continue
            q.append(nc)

    ans = 0
    for i in range(1, col - 1):
        ri = i * row
        for j in range(1, row - 1):
            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)
    return ans


for a, b, c in ver_lines:
    register_ver_lines(a, b, c, banned_left, banned_right, x_dict, y_dict)
for d, e, f in hor_lines:
    register_hor_lines(d, e, f, banned_up, banned_down, x_dict, y_dict)

accumulate_banned(banned_up, banned_down, banned_left, banned_right)

ans = dfs(x_list, y_list, x_dict, y_dict, banned_up, banned_down, banned_left, banned_right)
print(ans if ans != -1 else 'INF')