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 Rust (1.42.0) (judge verdict: Accepted)

fn run() {
    proconio::input! {
        n: usize,
        m: usize,
        col: [(i64, i64, i64); n],
        row: [(i64, i64, i64); m],
    }
    let inf = 1_000_000_000i64 + 1;
    let mut x = vec![-inf, inf];
    let mut y = vec![-inf, inf];
    for &(a, b, c) in col.iter() {
        x.push(a);
        x.push(b);
        y.push(c);
    }
    for &(a, b, c) in row.iter() {
        x.push(a);
        y.push(b);
        y.push(c);
    }
    x.sort();
    x.dedup();
    y.sort();
    y.dedup();
    let mut h: Vec<_> = x.iter().map(|x| (*x, 0)).collect();
    let mut w: Vec<_> = y.iter().map(|x| (*x, 0)).collect();
    for x in x.windows(2) {
        h.push((x[0], x[1] - x[0]));
    }
    for y in y.windows(2) {
        w.push((y[0], y[1] - y[0]));
    }
    h.sort();
    w.sort();
    let mut board = vec![vec![false; w.len()]; h.len()];
    for &(a, b, c) in col.iter() {
        let l = h.binary_search(&(a, 0)).unwrap();
        let r = h.binary_search(&(b, 0)).unwrap();
        let y = w.binary_search(&(c, 0)).unwrap();
        for i in l..=r {
            board[i][y] = true;
        }
    }
    for &(d, e, f) in row.iter() {
        let x = h.binary_search(&(d, 0)).unwrap();
        let l = w.binary_search(&(e, 0)).unwrap();
        let r = w.binary_search(&(f, 0)).unwrap();
        for i in l..=r {
            board[x][i] = true;
        }
    }
    let mut dfs = vec![];
    let cond = |x: (i64, i64)| x.1 > 0 && x.0 <= 0 && x.0 + x.1 >= 0;
    for (i, x) in h.iter().enumerate() {
        if !cond(*x) {
            continue;
        }
        for (j, y) in w.iter().enumerate() {
            if cond(*y) {
                board[i][j] = true;
                dfs.push((i, j));
            }
        }
    }
    let mut ans = 0i64;
    while let Some((a, b)) = dfs.pop() {
        ans += h[a].1 * w[b].1;
        for &(i, j) in [(a - 1, b), (a + 1, b), (a, b - 1), (a, b + 1)].iter() {
            if !(i < h.len() && j < w.len()) {
                println!("INF");
                return;
            }
            if !board[i][j] {
                board[i][j] = true;
                dfs.push((i, j));
            }
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}