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: Runtime Error)

use proconio::input;
use itertools::Itertools;

const INF:i64 = 1e16 as i64;

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

    let xs:Vec<i64> = {
        let mut vec = Vec::<i64>::new();
        vec.push(0);
        vec.push(INF);
        vec.push(-INF);
        abc.iter().for_each(|&(a,b,_)|{
            vec.push(a);
            vec.push(b);
        });
        def.iter().for_each(|&(d,_,_)|{
            vec.push(d);
        });
        vec.sort();
        vec.into_iter().unique().collect()
    };
    let ys:Vec<i64> = {
        let mut vec = Vec::<i64>::new();
        vec.push(0);
        vec.push(INF);
        vec.push(-INF);
        abc.iter().for_each(|&(_,_,c)|{
            vec.push(c);
        });
        def.iter().for_each(|&(_,e,f)|{
            vec.push(e);
            vec.push(f);
        });
        vec.sort();
        vec.into_iter().unique().collect()
    };
    
    let gridx = {
        let mut gridx = vec![vec![false;ys.len()];xs.len()];
        for &(a,b,c) in &abc {
            let j = ys.iter().position(|&y|y==c).unwrap();
            for i in 0..xs.len() {
                if a<=xs[i] && xs[i]<b {
                    gridx[i][j] = true
                }
            }
        }
        gridx
    };

    let gridy = {
        let mut gridy = vec![vec![false;ys.len()];xs.len()];
        for &(d,e,f) in &def {
            let i = xs.iter().position(|&x|x==d).unwrap();
            for j in 0..ys.len() {
                if e<=ys[j] && ys[j]<f {
                    gridy[i][j] = true
                }
            }
        }
        gridy
    };

    let mut grid = vec![vec![false;ys.len()-1];xs.len()-1];
    let mut stack = Vec::<(usize,usize)>::new();
    {
        let (i,j) = {
            (
            xs.iter().position(|&x|x==0).unwrap(),
            ys.iter().position(|&y|y==0).unwrap(),
            )
        };
        grid[i][j] = true;
        stack.push((i,j));
    }

    let di = vec![1,0,-1,0];
    let dj = vec![0,1,0,-1];

    while let Some((i,j)) = stack.pop() {
        if i==0 || j==0 || i==xs.len()-1 || j==ys.len()-1 {
            println!("INF");
            return;
        }
        for k in 0..4 {
            let gi = i+(di[k]+1) as usize/2;
            let gj = j+(dj[k]+1) as usize/2;
            let xwall = dj[k]!=0 && gridx[gi][gj];
            let ywall = di[k]!=0 && gridy[gi][gj];
            let toi = (i as i32 +di[k]) as usize;
            let toj = (j as i32 +dj[k]) as usize;
            if !xwall && !ywall && !grid[toi][toj]{
                grid[toi][toj] = true;
                stack.push((toi,toj));
            }
        }
    }

    let mut ans = 0;
    for i in 0..xs.len()-1 {
        for j in 0..ys.len()-1 {
            if grid[i][j] {
                let xwid = xs[i+1]-xs[i];
                let ywid = ys[j+1]-ys[j];
                ans += xwid*ywid;
            }
        }
    }
    println!("{}",ans);

}