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)

use std::collections::VecDeque;

const INF: i64 = 1 << 50;
fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let m: usize = sc.read();

    let mut vx = vec![-INF, 0, INF];
    let mut vy = vec![-INF, 0, INF];

    let mut x_lines = vec![];
    for _ in 0..n {
        let x1: i64 = sc.read();
        let x2: i64 = sc.read();
        let y: i64 = sc.read();
        x_lines.push((x1, x2, y));
        vx.push(x1);
        vx.push(x2);
        vy.push(y);
    }

    let mut y_lines = vec![];
    for _ in 0..m {
        let x: i64 = sc.read();
        let y1: i64 = sc.read();
        let y2: i64 = sc.read();
        y_lines.push((y1, y2, x));
        vy.push(y1);
        vy.push(y2);
        vx.push(x);
    }

    vx.sort();
    vx.dedup();
    vy.sort();
    vy.dedup();

    let h = vx.len();
    let w = vy.len();

    let mut x_wall = vec![vec![]; w];
    for &(x1, x2, y) in x_lines.iter() {
        let x1 = vx.binary_search(&x1).unwrap();
        let x2 = vx.binary_search(&x2).unwrap();
        let y = vy.binary_search(&y).unwrap();
        x_wall[y].push((x1, x2));
    }

    for x_wall in x_wall.iter_mut() {
        x_wall.sort();
    }

    let mut y_wall = vec![vec![]; h];
    for &(y1, y2, x) in y_lines.iter() {
        let y1 = vy.binary_search(&y1).unwrap();
        let y2 = vy.binary_search(&y2).unwrap();
        let x = vx.binary_search(&x).unwrap();
        y_wall[x].push((y1, y2));
    }

    for y_wall in y_wall.iter_mut() {
        y_wall.sort();
    }

    let mut board = vec![vec![false; w - 1]; h - 1];

    let si = vx.binary_search(&0).unwrap();
    let sj = vy.binary_search(&0).unwrap();
    board[si][sj] = true;
    let mut queue = VecDeque::new();
    queue.push_back((si, sj));
    while let Some((x, y)) = queue.pop_front() {
        if x > 0 {
            let conflict = wall(&y_wall[x], y);
            if !board[x - 1][y] && !conflict {
                board[x - 1][y] = true;
                queue.push_back((x - 1, y));
            }
        }

        if x + 1 < board.len() {
            let conflict = wall(&y_wall[x + 1], y);
            if !board[x + 1][y] && !conflict {
                board[x + 1][y] = true;
                queue.push_back((x + 1, y));
            }
        }

        if y > 0 {
            let conflict = wall(&x_wall[y], x);
            if !board[x][y - 1] && !conflict {
                board[x][y - 1] = true;
                queue.push_back((x, y - 1));
            }
        }

        if y + 1 < board[x].len() {
            let conflict = wall(&x_wall[y + 1], x);
            if !board[x][y + 1] && !conflict {
                board[x][y + 1] = true;
                queue.push_back((x, y + 1));
            }
        }
    }

    let mut ans = 0;
    for x in 0..board.len() {
        for y in 0..board[x].len() {
            if !board[x][y] {
                continue;
            }

            if x == 0 || x == board.len() - 1 || y == 0 || y == board[x].len() - 1 {
                println!("INF");
                return;
            }

            let x1 = vx[x];
            let x2 = vx[x + 1];
            let y1 = vy[y];
            let y2 = vy[y + 1];
            ans += (x2 - x1) * (y2 - y1);
        }
    }

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

fn wall(walls: &Vec<(usize, usize)>, x: usize) -> bool {
    let mut conflict = false;
    let right = walls.binary_search(&(x, 0)).err().unwrap();
    if walls.len() > right {
        let (x1, x2) = walls[right];
        if x1 <= x && x + 1 <= x2 {
            conflict = true;
        }
    }

    if right > 0 && walls.len() > right - 1 {
        let (x1, x2) = walls[right - 1];
        if x1 <= x && x + 1 <= x2 {
            conflict = true;
        }
    }
    conflict
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}