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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;

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

    let mut xs = BTreeSet::new();
    let mut ys = BTreeSet::new();

    for &(a, b, c) in tate.iter() {
        xs.insert(a);
        xs.insert(b);
        ys.insert(c);
    }

    for &(d, e, f) in yoko.iter() {
        xs.insert(d);
        ys.insert(e);
        ys.insert(f);
    }

    xs.insert(0);
    ys.insert(0);

    let xs = xs.into_iter().collect_vec();
    let ys = ys.into_iter().collect_vec();

    // let mut walls = BTreeSet::new();

    let mut tates = BTreeMap::<i64, Vec<(i64, i64)>>::new();
    for &(a, b, c) in tate.iter() {
        tates.entry(c).or_default().push((a, b));
    }
    let mut yokos = BTreeMap::<i64, Vec<(i64, i64)>>::new();
    for &(d, e, f) in yoko.iter() {
        yokos.entry(d).or_default().push((e, f));
    }

    let mut twalls = vec![vec![false; ys.len()]; xs.len()];
    let mut ywalls = vec![vec![false; xs.len()]; ys.len()];

    for (c, rs) in tates.into_iter() {
        let mut st = vec![];
        let mut ed = vec![];

        for (a, b) in rs {
            st.push(a);
            ed.push(b);
        }

        st.sort();
        ed.sort();

        let mut st = st.into_iter().collect::<VecDeque<_>>();
        let mut ed = ed.into_iter().collect::<VecDeque<_>>();

        let mut dep = 0;

        for x in 0..xs.len() - 1 {
            let xf = xs[x];
            let xt = xs[x + 1];

            while st.len() > 0 && st[0] == xf {
                st.pop_front();
                dep += 1;
            }

            while ed.len() > 0 && ed[0] == xf {
                ed.pop_front();
                dep -= 1;
            }

            if dep > 0 {
                twalls[x][lower_bound(&ys, &c)] = true;
                // walls.insert(((xf, c), (xt, c)));
            }
        }
    }

    for (d, rs) in yokos.into_iter() {
        let mut st = vec![];
        let mut ed = vec![];

        for (a, b) in rs {
            st.push(a);
            ed.push(b);
        }

        st.sort();
        ed.sort();

        let mut st = st.into_iter().collect::<VecDeque<_>>();
        let mut ed = ed.into_iter().collect::<VecDeque<_>>();

        let mut dep = 0;

        for y in 0..ys.len() - 1 {
            let yf = ys[y];
            let yt = ys[y + 1];

            while st.len() > 0 && st[0] == yf {
                st.pop_front();
                dep += 1;
            }

            while ed.len() > 0 && ed[0] == yf {
                ed.pop_front();
                dep -= 1;
            }

            if dep > 0 {
                ywalls[y][lower_bound(&xs, &d)] = true;
                // walls.insert(((d, yf), (d, yt)));
            }
        }
    }

    // for &(a, b, c) in tates.iter() {
    //     let i = lower_bound(&xs, &a);
    //     let j = lower_bound(&xs, &b);

    //     for k in i..j {
    //         walls.insert(((xs[k], c), (xs[k + 1], c)));
    //     }
    // }

    // for &(d, e, f) in yoko.iter() {
    //     let i = lower_bound(&ys, &e);
    //     let j = lower_bound(&ys, &f);

    //     for k in i..j {
    //         walls.insert(((d, ys[k]), (d, ys[k + 1])));
    //     }
    // }

    let h = ys.len() - 1;
    let w = xs.len() - 1;
    let nn = h * w;

    let mut g = vec![vec![]; nn + 1];
    let mut area = vec![0; nn + 1];
    area[nn] = i64::MAX;

    let mut q = VecDeque::new();

    let nf = |x, y| x + y * w;

    for y in 0..ys.len() - 1 {
        for x in 0..xs.len() - 1 {
            let node = y * w + x;

            let yf = ys[y];
            let yt = ys[y + 1];
            let xf = xs[x];
            let xt = xs[x + 1];
            area[node] = (xt - xf) * (yt - yf);

            if (xf, yf) == (0, 0) || (xf, yt) == (0, 0) || (xt, yf) == (0, 0) || (xt, yt) == (0, 0)
            {
                q.push_back(node);
            }

            if !twalls[x][y] {
                // !walls.contains(&((xf, yf), (xt, yf))) {
                let to = if y > 0 { nf(x, y - 1) } else { nn };
                g[node].push(to);
            }
            if !ywalls[y][x] {
                // !walls.contains(&((xf, yf), (xf, yt))) {
                let to = if x > 0 { nf(x - 1, y) } else { nn };
                g[node].push(to)
            }
            if !twalls[x][y + 1] {
                // !walls.contains(&((xf, yt), (xt, yt))) {
                let to = if y + 1 < h { nf(x, y + 1) } else { nn };
                g[node].push(to)
            }
            if !ywalls[y][x + 1] {
                // !walls.contains(&((xt, yf), (xt, yt))) {
                let to = if x + 1 < w { nf(x + 1, y) } else { nn };
                g[node].push(to)
            }
        }
    }

    let mut ans = 0;
    let mut close = vec![false; nn + 1];

    while let Some(cur) = q.pop_front() {
        if close[cur] {
            continue;
        }

        close[cur] = true;
        if area[cur] == i64::MAX {
            echo!("INF");
            return;
        }

        ans += area[cur];

        for &next in g[cur].iter() {
            if close[next] {
                continue;
            }
            q.push_back(next);
        }
    }

    echo!(ans);
}
*/

fn main() {
    let exe = "/tmp/bin71026175";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";