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)

macro_rules !read_value {($iter :expr ,($($t :tt ) ,*) ) =>{($(read_value !($iter ,$t ) ) ,*) } ;($iter :expr ,[$t :tt ;$len :expr ] ) =>{(0 ..$len ) .map (|_ |read_value !($iter ,$t ) ) .collect ::<Vec <_ >>() } ;($iter :expr ,{chars :$base :expr } ) =>{read_value !($iter ,String ) .chars () .map (|c |(c as u8 -$base as u8 ) as usize ) .collect ::<Vec <usize >>() } ;($iter :expr ,{char :$base :expr } ) =>{read_value !($iter ,{chars :$base } ) [0 ] } ;($iter :expr ,chars ) =>{read_value !($iter ,String ) .chars () .collect ::<Vec <char >>() } ;($iter :expr ,char ) =>{read_value !($iter ,chars ) [0 ] } ;($iter :expr ,usize1 ) =>{read_value !($iter ,usize ) -1 } ;($iter :expr ,$t :ty ) =>{$iter .next () .unwrap () .parse ::<$t >() .unwrap () } ;}
macro_rules !input_inner {($iter :expr ) =>{} ;($iter :expr ,) =>{} ;($iter :expr ,mut $var :ident :$t :tt $($r :tt ) *) =>{let mut $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,mut $var :ident $($r :tt ) *) =>{input_inner !{$iter ,mut $var :usize $($r ) *} } ;($iter :expr ,$var :ident :$t :tt $($r :tt ) *) =>{let $var =read_value !($iter ,$t ) ;input_inner !{$iter $($r ) *} } ;($iter :expr ,$var :ident $($r :tt ) *) =>{input_inner !{$iter ,$var :usize $($r ) *} } ;}
macro_rules !input {(source =$s :expr ,$($r :tt ) *) =>{let mut iter =$s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;(iter =$iter :ident ,$($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut $iter =s .split_whitespace () ;input_inner !{$iter ,$($r ) *} } ;($($r :tt ) *) =>{let s ={use std ::io ::Read ;let mut s =String ::new () ;std ::io ::stdin () .read_to_string (&mut s ) .unwrap () ;s } ;let mut iter =s .split_whitespace () ;input_inner !{iter ,$($r ) *} } ;}
pub trait Bisect: Copy {
    fn halve(self, other: Self) -> Self;
    fn section_end(self, other: Self) -> bool;
}
pub mod bisect_impl {
    use super::*;
    macro_rules! impl_bisect_unsigned {
        ($t :ty ) => {
            impl Bisect for $t {
                fn halve(self, other: Self) -> Self {
                    if self > other {
                        other + (self - other) / 2
                    } else {
                        self + (other - self) / 2
                    }
                }
                fn section_end(self, other: Self) -> bool {
                    (if self > other {
                        self - other
                    } else {
                        other - self
                    }) <= 1
                }
            }
        };
    }
    macro_rules! impl_bisect_signed {
        ($t :ty ) => {
            impl Bisect for $t {
                fn halve(self, other: Self) -> Self {
                    (self + other) / 2
                }
                fn section_end(self, other: Self) -> bool {
                    (self - other).abs() <= 1
                }
            }
        };
    }
    macro_rules! impl_bisect_float {
        ($t :ty ) => {
            impl Bisect for $t {
                fn halve(self, other: Self) -> Self {
                    (self + other) / 2.
                }
                fn section_end(self, other: Self) -> bool {
                    (self - other).abs() <= 1e-8
                }
            }
        };
    }
    impl_bisect_unsigned!(u8);
    impl_bisect_unsigned!(u16);
    impl_bisect_unsigned!(u32);
    impl_bisect_unsigned!(u64);
    impl_bisect_unsigned!(usize);
    impl_bisect_signed!(i8);
    impl_bisect_signed!(i16);
    impl_bisect_signed!(i32);
    impl_bisect_signed!(i64);
    impl_bisect_signed!(isize);
    impl_bisect_float!(f32);
    impl_bisect_float!(f64);
}
pub fn binary_search<T: Bisect, F: Fn(T) -> bool>(f: F, ok: T, err: T) -> T {
    let mut ok = ok;
    let mut err = err;
    while !ok.section_end(err) {
        let m = ok.halve(err);
        if f(m) {
            ok = m;
        } else {
            err = m;
        }
    }
    ok
}
pub fn lower_bound<T: Bisect + Ord>(v: &[T], x: T) -> usize {
    binary_search(|i| v[i as usize] >= x, v.len() as i64, -1) as usize
}
pub fn upper_bound<T: Bisect + Ord>(v: &[T], x: T) -> usize {
    binary_search(|i| v[i as usize] > x, v.len() as i64, -1) as usize
}
#[derive(Clone, Debug)]
pub struct Compress<T> {
    v: Vec<T>,
}
impl<T: Bisect + Ord> Compress<T> {
    pub fn get(&self, x: T) -> usize {
        lower_bound(&self.v, x)
    }
}
impl<T> Compress<T> {
    pub fn len(&self) -> usize {
        self.v.len()
    }
}
impl<T> std::ops::Index<usize> for Compress<T> {
    type Output = T;
    fn index(&self, index: usize) -> &Self::Output {
        &self.v[index]
    }
}
impl<T: Ord> std::iter::FromIterator<T> for Compress<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Compress<T> {
        use std::collections::BTreeSet;
        let v = iter
            .into_iter()
            .collect::<BTreeSet<T>>()
            .into_iter()
            .collect::<Vec<T>>();
        Compress { v }
    }
}
pub struct GridGraph {
    height: usize,
    width: usize,
}
impl GridGraph {
    pub fn new(height: usize, width: usize) -> Self {
        Self { height, width }
    }
    pub fn len_adjacency(&self, x: usize, y: usize) -> usize {
        (x + 1 < self.height) as usize
            + (y + 1 < self.width) as usize
            + (x > 0) as usize
            + (y > 0) as usize
    }
    pub fn for_each_adjacency<F: FnMut(usize, usize)>(&self, x: usize, y: usize, mut f: F) {
        if x + 1 < self.height {
            f(x + 1, y);
        }
        if y + 1 < self.width {
            f(x, y + 1);
        }
        if x > 0 {
            f(x - 1, y);
        }
        if y > 0 {
            f(x, y - 1);
        }
    }
}
fn main() {
    input!(n, m, abc: [(i64, i64, i64); n], def: [(i64, i64, i64); m]);
    let mut ans = 0i64;
    let mut xs = vec![-2_001_001_001, 0, 2_001_001_001];
    let mut ys = vec![-2_001_001_001, 0, 2_001_001_001];
    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);
    }
    let xs: Compress<_> = xs.into_iter().collect();
    let ys: Compress<_> = ys.into_iter().collect();
    let h = xs.len();
    let w = ys.len();
    let mut vis = vec![vec![false; w * 2 - 1]; h * 2 - 1];
    let mut on = vec![vec![false; w * 2 - 1]; h * 2 - 1];
    for &(a, b, c) in abc.iter() {
        let x1 = xs.get(a) * 2;
        let x2 = xs.get(b) * 2;
        let y = ys.get(c) * 2;
        for i in x1..=x2 {
            on[i][y] = true;
        }
    }
    for &(d, e, f) in def.iter() {
        let x = xs.get(d) * 2;
        let y1 = ys.get(e) * 2;
        let y2 = ys.get(f) * 2;
        for j in y1..=y2 {
            on[x][j] = true;
        }
    }
    let gg = GridGraph::new(h * 2 - 1, w * 2 - 1);
    let (s, t) = (xs.get(0) * 2, ys.get(0) * 2);
    vis[s][t] = true;
    let mut stack = vec![(s, t)];
    while let Some((s, t)) = stack.pop() {
        if gg.len_adjacency(s, t) < 4 {
            println!("INF");
            std::process::exit(0);
        }
        gg.for_each_adjacency(s, t, |x, y| {
            if !vis[x][y] && !on[x][y] {
                vis[x][y] = true;
                stack.push((x, y))
            }
        });
    }
    for i in 0..2 * h - 1 {
        for j in 0..2 * w - 1 {
            if i % 2 == 1 && j % 2 == 1 && vis[i][j] {
                ans += (xs[i / 2 + 1] - xs[i / 2]) * (ys[j / 2 + 1] - ys[j / 2]);
            }
        }
    }
    println!("{}", ans);
}