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(unused_imports)]
use proconio::*;

fn main() {
   input! {
      n: usize,
      m: usize,
      lh: [(i64, i64, i64); n],
      lv: [(i64, i64, i64); m],
   }
   let mut mpx = std::collections::BTreeMap::new();
   let mut mpy = std::collections::BTreeMap::new();
   mpx.insert(0, 0);
   mpy.insert(0, 0);
   for &(a,b,c) in lh.iter() {
      mpx.entry(a).or_insert(0);
      mpx.entry(b).or_insert(0);
      mpy.entry(c).or_insert(0);
   }
   for &(d,e,f) in lv.iter() {
      mpx.entry(d).or_insert(0);
      mpy.entry(e).or_insert(0);
      mpy.entry(f).or_insert(0);
   }
   let mut xs = vec![];
   let mut ys = vec![];
   for (x, c) in mpx.iter_mut() {
      *c = xs.len();
      xs.push(*x);
   }
   for (y, c) in mpy.iter_mut() {
      *c = ys.len();
      ys.push(*y);
   }

   let h = xs.len()*2;
   let w = ys.len()*2;
   let mut g = vec![vec![0; w]; h];
   for (a,b,c) in lh.iter() {
      let a = mpx.get(a).unwrap()*2;
      let b = mpx.get(b).unwrap()*2;
      let c = mpy.get(c).unwrap()*2;
      for x in a..=b {
         g[x][c] = -1;
      }
   }
   for (d,e,f) in lv.iter() {
      let d = mpx.get(d).unwrap()*2;
      let e = mpy.get(e).unwrap()*2;
      let f = mpy.get(f).unwrap()*2;
      for y in e..=f {
         g[d][y] = -1;
      }
   }
   let mut q = std::collections::VecDeque::new();
   let sx = mpx.get(&0).unwrap()*2;
   let sy = mpy.get(&0).unwrap()*2;
   q.push_back((sx, sy));
   while let Some((x, y)) = q.pop_front() {
      for i in 0..4 {
         let nx = x as i32+ dx[i];
         let ny = y as i32+ dy[i];
         if nx < 0 || h as i32 <= nx {
            continue;
         }
         if ny < 0 || w as i32 <= ny {
            continue;
         }
         let nx = nx as usize;
         let ny = ny as usize;
         if g[nx][ny] != 0 {
            continue;
         }
         g[nx][ny] = 1;
         q.push_back((nx, ny));
      }
   }

   let mut ans: i64 = 0;
   for x in 0..h {
      for y in 0..w {
         if g[x][y] != 1 {
            continue;
         }
         if x == 0 || x == h-1 || y == 0 || y == w-1 {
            println!("INF");
            return;
         }
         if x%2 == 0 || y%2==0 {
            continue;
         }
         let ex = xs[x/2+1] - xs[x/2];
         let ey = ys[y/2+1] - ys[y/2];
         ans += ex*ey;
      }
   }
   println!("{}", ans);
}

const dx: [i32; 4] = [1, 0, -1, 0];
const dy: [i32; 4] = [0, 1, 0, -1];