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)

fn main() {
	let mut s = String::new();
	use std::io::Read;
	std::io::stdin().read_to_string(&mut s).unwrap();
	let mut s = s.split_whitespace();
	let n: usize = s.next().unwrap().parse().unwrap();
	let m: usize = s.next().unwrap().parse().unwrap();
	let mut xs = vec![];
	let mut ys = vec![];
	let mut vert = vec![];
	let mut horz = vec![];
	for _ in 0..n {
		let y0: i32 = s.next().unwrap().parse().unwrap();
		let y1: i32 = s.next().unwrap().parse().unwrap();
		let x: i32 = s.next().unwrap().parse().unwrap();
		vert.push((y0, y1, x));
		xs.push(x);
		ys.push(y0);
		ys.push(y1);
	}
	for _ in 0..m {
		let y: i32 = s.next().unwrap().parse().unwrap();
		let x0: i32 = s.next().unwrap().parse().unwrap();
		let x1: i32 = s.next().unwrap().parse().unwrap();
		horz.push((y, x0, x1));
		xs.push(x0);
		xs.push(x1);
		ys.push(y);
	}
	xs.sort();
	xs.dedup();
	ys.sort();
	ys.dedup();
	let mut field = vec![vec![false; xs.len() + 1]; ys.len() + 1];
	let mut fence = vec![vec![0u8; xs.len() + 1]; ys.len() + 1];
	for (y0, y1, x) in vert.iter() {
		let yl = ys.binary_search(y0).unwrap() + 1;
		let yr = ys.binary_search(y1).unwrap();
		let x = xs.binary_search(x).unwrap();
		for y in yl..=yr {
			fence[y][x] |= 2;
			fence[y][x + 1] |= 8;
		}
	}
	for (y, x0, x1) in horz.iter() {
		let xl = xs.binary_search(x0).unwrap() + 1;
		let xr = xs.binary_search(x1).unwrap();
		let y = ys.binary_search(y).unwrap();
		for x in xl..=xr {
			fence[y][x] |= 4;
			fence[y + 1][x] |= 1;
		}
	}

	let mut q = std::collections::VecDeque::new();
	let ystart = ys.binary_search(&0).unwrap_or_else(|i| i);
	let xstart = xs.binary_search(&0).unwrap_or_else(|i| i);

	q.push_back((ystart as i32, xstart as i32));
	field[ystart][xstart] = true;
	while let Some((y, x)) = q.pop_front() {
		for (i, (dy, dx)) in [(-1, 0), (0, 1), (1, 0), (0, -1)].iter().enumerate() {
			let ny = y + dy;
			let nx = x + dx;
			if ny >= 0
				&& ny <= ys.len() as i32
				&& nx >= 0 && nx <= xs.len() as i32
				&& fence[y as usize][x as usize] & (1 << i) == 0
				&& !field[ny as usize][nx as usize]
			{
				field[ny as usize][nx as usize] = true;
				q.push_back((ny, nx));
			}
		}
	}
	let mut ans = 0i64;
	for y in 0..=ys.len() {
		for x in 0..=xs.len() {
			if field[y][x] {
				ans = ans.saturating_add(if y == 0 || y == ys.len() || x == 0 || x == xs.len() {
					std::i64::MAX
				} else {
					let h = ys[y] as i64 - ys[y - 1] as i64;
					let w = xs[x] as i64 - xs[x - 1] as i64;
					h * w
				});
			}
		}
	}
	if ans == std::i64::MAX {
		println!("INF");
	} else {
		println!("{}", ans);
	}
}