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: Time Limit Exceeded)

#![allow(unused_imports)]

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::cmp::{min, max};
use std::collections::{HashMap, HashSet};

fn main() {
    input!{
        n: usize,
        m: usize,
        v: [(i64, i64, i64); n],
        w: [(i64, i64, i64); m]
    }
    let mut xs = vec![];
    let mut ys = vec![];
    for &(a, b, c) in &v {
        xs.push(a);
        xs.push(b);
        ys.push(c);
    }
    for &(d, e, f) in &w {
        xs.push(d);
        ys.push(e);
        ys.push(f);
    }

    xs.sort();
    xs.dedup();
    ys.sort();
    ys.dedup();
    let mut g = HashMap::new();
    let mut ar = vec![vec![-1; ys.len() + 1]; xs.len() + 1];
    for i in 0..=xs.len() {
        for j in 0..=ys.len() {
            g.insert((i, j), 0);
            if i != 0 && i != xs.len() && j != 0 && j != ys.len() {
                ar[i][j] = (xs[i] - xs[i - 1]) * (ys[j] - ys[j - 1]);
            }
        }
    }
    for &(a, b, c) in &v {
        let xl = xs.binary_search(&a).unwrap();
        let xr = xs.binary_search(&b).unwrap();
        let y = ys.binary_search(&c).unwrap();
        for i in xl + 1..=xr {
            *g.get_mut(&(i, y)).unwrap() |= 8;
            *g.get_mut(&(i, y + 1)).unwrap() |= 4;
        }
    }
    for &(d, e, f) in &w {
        let x = xs.binary_search(&d).unwrap();
        let yl = ys.binary_search(&e).unwrap();
        let yr = ys.binary_search(&f).unwrap();
        for i in yl + 1..=yr {
            *g.get_mut(&(x, i)).unwrap() |= 2;
            *g.get_mut(&(x + 1, i)).unwrap() |= 1;
        }
    }
    let x0 = match xs.binary_search(&0) {Ok(i) => i, Err(i) => i};
    let y0 = match ys.binary_search(&0) {Ok(i) => i, Err(i) => i};
    if ar[x0][y0] == -1 {
        println!("INF");
        return;
    }
    let mut st = vec![(x0, y0)];
    let mut done = HashSet::new();
    let mut ans = ar[x0][y0];
    done.insert((x0, y0));
    while let Some((i, j)) = st.pop() {
        let con = g.get(&(i, j)).unwrap();
        if con & 1 == 0 && done.insert((i - 1, j)) {
            if ar[i - 1][j] == -1 {
                println!("INF");
                return;
            }
            ans += ar[i - 1][j];
            st.push((i - 1, j));
        }
        if con & 2 == 0 && done.insert((i + 1, j)) {
            if ar[i + 1][j] == -1 {
                println!("INF");
                return;
            }
            ans += ar[i + 1][j];
            st.push((i + 1, j));
        }
        if con & 4 == 0 && done.insert((i, j - 1)) {
            if ar[i][j - 1] == -1 {
                println!("INF");
                return;
            }
            ans += ar[i][j - 1];
            st.push((i, j - 1));
        }
        if con & 8 == 0 && done.insert((i, j + 1)) {
            if ar[i][j + 1] == -1 {
                println!("INF");
                return;
            }
            ans += ar[i][j + 1];
            st.push((i, j + 1));
        }
    }
    println!("{}", ans);
}