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(
    non_snake_case,
    unused_variables,
    unused_assignments,
    unused_mut,
    unused_imports,
    dead_code
)]
//use proconio::fastout;
use proconio::input;
use proconio::marker::*;
use std::cmp::*;
use std::collections::*;
use std::ops::Bound::Included;
//use std::collections::VecDeque;

const MOD: i64 = 1_000_000_007;
fn main() {
    input! {
       N:usize,M:usize,
       mut ABC:[(i64,i64,i64);N],
       mut DEF:[(i64,i64,i64);M]
    }
    let inf = std::i64::MAX / 2;
    //ABC.sort_by_key(|&(_, _, c)| c);
    //DEF.sort_by_key(|&(d, _, _)| d);
    // 壁の数は高々1000個なので、先に座標を圧縮する。
    let mut xmap = BTreeMap::new();
    let mut ymap = BTreeMap::new();
    xmap.insert(0, 0);
    ymap.insert(0, 0);
    for &(a, b, c) in ABC.iter() {
        xmap.insert(a, 0);
        xmap.insert(b, 0);
        ymap.insert(c, 0);
    }
    for &(d, e, f) in DEF.iter() {
        xmap.insert(d, 0);
        ymap.insert(e, 0);
        ymap.insert(f, 0);
    }
    let mut xwidth = vec![0; xmap.len() * 2 + 1];
    let mut ywidth = vec![0; ymap.len() * 2 + 1];
    let mut prev = 0;
    // xwidth[0],ywidth[0]の幅がおかしくなるけどどうせアクセスしないので気にしない。
    for (i, (&k, _)) in xmap.clone().iter().enumerate() {
        xmap.insert(k, i * 2 + 1);
        xwidth[i * 2] = k - prev;
        prev = k;
    }
    for (i, (&k, _)) in ymap.clone().iter().enumerate() {
        ymap.insert(k, i * 2 + 1);
        ywidth[i * 2] = k - prev;
        prev = k;
    }
    let mut p = vec![vec![0; ymap.len() * 2 + 1]; xmap.len() * 2 + 1];
    for &(a, b, c) in ABC.iter() {
        let x1 = *xmap.get(&a).unwrap();
        let x2 = *xmap.get(&b).unwrap();
        let y = *ymap.get(&c).unwrap();
        for x in x1..=x2 {
            p[x][y] = 1;
        }
    }
    for &(d, e, f) in DEF.iter() {
        let x = *xmap.get(&d).unwrap();
        let y1 = *ymap.get(&e).unwrap();
        let y2 = *ymap.get(&f).unwrap();
        for y in y1..=y2 {
            p[x][y] = 1;
        }
    }
    // start位置を設定。
    // 移動候補をheapに追加していく。
    // println!("{:?}", p);
    let x = *xmap.get(&0).unwrap();
    let y = *ymap.get(&0).unwrap();
    let mut h = BinaryHeap::new();
    h.push((x - 1, y - 1));
    h.push((x - 1, y + 1));
    h.push((x + 1, y - 1));
    h.push((x + 1, y + 1));
    let mut ans = 0;
    //println!("xmap={:?},ymap={:?}", xmap, ymap);
    while h.len() > 0 {
        let (hx, hy) = h.pop().unwrap();
        if hx == 0 || hy == 0 || hx == xmap.len() * 2 || hy == ymap.len() * 2 {
            println!("INF");
            return;
        }
        if p[hx][hy] == 2 {
            continue;
        }
        //移動済みフラグ。
        p[hx][hy] = 2;
        // 領域の面積を加算。
        //println!("{:?}", h);
        //println!("{},{}", hx, hy);
        ans += xwidth[hx] * ywidth[hy];
        //移動する方向に壁が無く、かつ移動先でもないなら、heapに積む。
        if p[hx - 1][hy] == 0 && p[hx - 2][hy] == 0 {
            h.push((hx - 2, hy));
        }
        if p[hx + 1][hy] == 0 && p[hx + 2][hy] == 0 {
            h.push((hx + 2, hy));
        }
        if p[hx][hy - 1] == 0 && p[hx][hy - 2] == 0 {
            h.push((hx, hy - 2));
        }
        if p[hx][hy + 1] == 0 && p[hx][hy + 2] == 0 {
            h.push((hx, hy + 2));
        }
    }
    //println!("{:?}", p);
    println!("{}", ans);
}