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 superslice::Ext;
fn main() {
    proconio::input! {
        (n,m): (usize,usize),
        abc: [(i64,i64,i64);n],
        def: [(i64,i64,i64);m],
    }
    let mut xs = Vec::with_capacity(n*2+m);
    let mut ys = Vec::with_capacity(n+m*2);
    for &(a,b,c) in &abc {
        xs.push(a);
        xs.push(b);
        ys.push(c);
    }
    for &(d,e,f) in &def {
        xs.push(d);
        ys.push(e);
        ys.push(f);
    }
    xs.sort_unstable(); xs.dedup();
    let xs = xs;
    ys.sort_unstable(); ys.dedup();
    let ys = ys;
    let mut g = vec![vec![true;ys.len()*2];xs.len()*2];
    for (a,b,c) in abc {
        let a = xs.binary_search(&a).unwrap()*2;
        let b = xs.binary_search(&b).unwrap()*2;
        let c = ys.binary_search(&c).unwrap()*2;
        for i in a..=b {
            g[i][c] = false;
        }
    }
    for (d,e,f) in &def {
        let d = xs.binary_search(&d).unwrap()*2;
        let e = ys.binary_search(&e).unwrap()*2;
        let f = ys.binary_search(&f).unwrap()*2;
        for i in e..=f {
            g[d][i] = false;
        }
    }
    let xm = g.len();
    let ym = g[0].len();
    let mut q = Vec::with_capacity(xm*ym);
    let mut c = vec![vec![false;ys.len()];xs.len()];
    {
        let i = xs.lower_bound(&0);
        let j = ys.lower_bound(&0);
        if i<=0 || xs.len()<=i || j<=0 || ys.len()<=j {
            println!("INF");
            return;
        }
        let i = if xs[i]>0 {i-1} else {i};
        let j = if ys[j]>0 {j-1} else {j};
        q.push((i*2+1,j*2+1));
    }
    while let Some((i,j))=q.pop() {
        c[i/2][j/2]=true;
        let dxy = [0,-1,0,1,0];
        for k in 0..4 {
            let x=i as i64+dxy[k];
            let y=j as i64+dxy[k+1];
            if x<0 || xm as i64<=x || y<0 || ym as i64<=y {
                println!("INF");
                return;
            }
            let (x,y)=(x as usize, y as usize);
            if g[x][y] {
                g[x][y]=false;
                q.push((x,y));
            }
        }
    }
    let mut t=0;
    for i in 0..xs.len()-1 {
        for j in 0..ys.len()-1 {
            if c[i][j] {
                t += (xs[i+1]-xs[i])*(ys[j+1]-ys[j]);
            }
        }
    }
    println!("{}",t);
}