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: Runtime Error)

use proconio::input;

fn main() {
    input! {
        n: usize,
        m: usize,
        abc: [(i64, i64, i64); n],
        def: [(i64, i64, i64); m]
    };
    let mut map_x = std::collections::BTreeMap::new();
    let mut map_y = std::collections::BTreeMap::new();
    for &(a, b, c) in abc.iter() {
        map_x.entry(a).or_insert(0);
        map_x.entry(b).or_insert(0);
        map_y.entry(c).or_insert(0);
    }
    for &(d, e, f) in def.iter() {
        map_x.entry(d).or_insert(0);
        map_y.entry(e).or_insert(0);
        map_y.entry(f).or_insert(0);
    }
    map_x.entry(0).or_insert(0);
    map_y.entry(0).or_insert(0);

    let mut xs = vec![0; map_x.len()];
    for (i, (&x, v)) in map_x.iter_mut().enumerate() {
        *v = i;
        xs[i] = x;
    }
    let mut ys = vec![0; map_y.len()];
    for (i, (&y, v)) in map_y.iter_mut().enumerate() {
        *v = i;
        ys[i] = y;
    }

    let h = map_x.len() * 2;
    let w = map_y.len() * 2;
    let mut tbl = vec![vec![Some(false); w + 1]; h + 1];
    for (a, b, c) in abc.iter() {
        let tbl_a = map_x.get(a).unwrap() * 2;
        let tbl_b = map_x.get(b).unwrap() * 2;
        let tbl_c = map_y.get(c).unwrap() * 2;
        (tbl_a..=tbl_b).for_each(|x| tbl[x][tbl_c] = None);
    }
    for (d, e, f) in def.iter() {
        let tbl_d = map_x.get(d).unwrap() * 2;
        let tbl_e = map_y.get(e).unwrap() * 2;
        let tbl_f = map_y.get(f).unwrap() * 2;
        (tbl_e..=tbl_f).for_each(|y| tbl[tbl_d][y] = None);
    }

    let d = vec![(-1, 0), (0, -1), (0, 1), (1, 0)];
    let mut pq = std::collections::BinaryHeap::new();
    let (sx, sy) = (map_x.get(&0).unwrap() * 2, map_y.get(&0).unwrap() * 2);
    tbl[sx][sy] = Some(true);
    pq.push(std::cmp::Reverse((sx, sy)));
    while let Some(std::cmp::Reverse((x, y))) = pq.pop() {
        for &(d_y, d_x) in d.iter() {
            let (nx, ny) = (x as i64 + d_x, y as i64 + d_y);
            if (0..w as i64).contains(&ny) && (0..h as i64).contains(&nx) {
                let (nx, ny) = (nx as usize, ny as usize);
                if tbl[nx][ny] != Some(false) {
                    continue;
                }
                tbl[nx][ny] = Some(true);
                pq.push(std::cmp::Reverse((nx, ny)));
            }
        }
    }

    let mut ans = 0;
    for x in 0..=h {
        for y in 0..=w {
            if tbl[x][y] != Some(true) {
                continue;
            }
            if x == 0 || x == h || y == 0 || y == w {
                println!("INF");
                return;
            }
            if x % 2 == 0 || y % 2 == 0 {
                continue;
            }
            let ex = xs[x / 2 + 1] - xs[x / 2];
            let ey = xs[y / 2 + 1] - ys[y / 2];
            ans += ex * ey;
        }
    }

    println!("{}", ans);
}