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)

#[allow(unused_imports)]
use itertools::Itertools;
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;

fn main() {
    input! {
        n: usize,
        m: usize,
        abc: [(i64, i64, i64); n],
        def: [(i64, i64, i64); m],
    }

    let mut mp_x = std::collections::BTreeMap::new();
    let mut mp_y = std::collections::BTreeMap::new();
    for &(a,b,c) in &abc {
        mp_x.insert(a, 0);
        mp_x.insert(b, 0);
        mp_y.insert(c, 0);
    }
    for &(d,e,f) in &def {
        mp_y.insert(e, 0);
        mp_y.insert(f, 0);
        mp_x.insert(d, 0);
    }
    mp_x.insert(0, 0);
    mp_y.insert(0, 0);
    
    let (mut xs, mut ys) = (vec![], vec![]);
    for (x, n) in &mut mp_x {
        *n = xs.len();
        xs.push(*x);
    }
    for (y, n) in &mut mp_y {
        *n = ys.len();
        ys.push(*y);
    }

    let w = xs.len() * 2;
    let h = ys.len() * 2;
    let mut map = vec![vec![true; w]; h];
    for (a,b,c) in abc {
        let a = mp_x.get(&a).unwrap() * 2;
        let b = mp_x.get(&b).unwrap() * 2;
        let c = mp_y.get(&c).unwrap() * 2;
        for x in a..=b {
            map[c][x] = false;
        }
    }
    for (d,e,f) in def {
        let e = mp_y.get(&e).unwrap() * 2;
        let f = mp_y.get(&f).unwrap() * 2;
        let d = mp_x.get(&d).unwrap() * 2;
        for y in e..=f {
            map[y][d] = false;
        }
    }
    
    let mut res = 0;
    let mut q = std::collections::VecDeque::new();
    let d = [(1, 0), (0, 1), (-1, 0), (0, -1)];
    q.push_back((*mp_x.get(&0).unwrap() * 2, *mp_y.get(&0).unwrap() * 2));
    while let Some(pos) = q.pop_front() {
        if pos.0 %2 != 0 && pos.1 %2 != 0 {
            let dx = xs[pos.0/2 + 1] - xs[pos.0/2];
            let dy = ys[pos.1/2 + 1] - ys[pos.1/2];
            res += dx*dy;
        }
        map[pos.1][pos.0] = false;
        for &d in &d {
            let (nx,ny) = (pos.0 as isize + d.0, pos.1 as isize + d.1);
            if nx < 0 || w as isize <= nx || ny < 0 || h as isize <= ny {
                continue;
            }
            let (nx, ny) = (nx as usize, ny as usize);
            if !map[ny][nx] {
                continue;
            }
            if nx == 0 || nx == w-1 || ny == 0 || ny == h-1 {
                println!("INF");
                return;
            }
            map[ny][nx] = false;
            q.push_back((nx, ny));
        }
    }
    println!("{}", res);
}