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: Wrong Answer)

//---------- begin union_find ----------
#[allow(dead_code)]
mod union_find {
    pub struct UF {
        p: Vec<i32>,
    }
    impl UF {
        pub fn new(n: usize) -> UF {
            UF {p: vec![-1; n] }
        }
        pub fn init(&mut self) {
            for p in self.p.iter_mut() {
                *p = -1;
            }
        }
        pub fn root(&self, mut x: usize) -> usize {
            while self.p[x] >= 0 {
                x = self.p[x] as usize;
            }
            x
        }
        pub fn same(&self, x: usize, y: usize) -> bool {
            self.root(x) == self.root(y)
        }
        pub fn unite(&mut self, mut x: usize, mut y: usize) -> Option<(usize, usize)> {
            x = self.root(x);
            y = self.root(y);
            if x == y {
                return None;
            }
            if self.p[x] > self.p[y] {
                let s = x;
                x = y;
                y = s;
            }
            self.p[x] += self.p[y];
            self.p[y] = x as i32;
            Some((x, y))
        }
        pub fn parent(&self, x: usize) -> Option<usize> {
            let p = self.p[x];
            if p >= 0 {
                Some(p as usize)
            } else {
                None
            }
        }
        pub fn get_size(&self, x: usize) -> usize {
            let r = self.root(x);
            (-self.p[r]) as usize
        }
    }
}
//---------- end union_find ----------

fn run() {
    proconio::input! {
        n: usize,
        m: usize,
        mut col: [(i64, i64, i64); n],
        mut row: [(i64, i64, i64); m],
    }
    for p in row.iter_mut().chain(col.iter_mut()) {
        p.0 = 2 * p.0 + 1;
        p.1 = 2 * p.1 + 1;
        p.2 = 2 * p.2 + 1;
    }
    let inf = 2_000_000_000 + 3;
    let mut x = vec![inf, -inf];
    let mut y = vec![inf, -inf];
    for &(_, _, c) in col.iter() {
        y.push(c);
    }
    for &(d, _, _) in row.iter() {
        x.push(d);
    }
    x.sort();
    x.dedup();
    y.sort();
    y.dedup();
    let h = x.len() - 1;
    let w = y.len() - 1;
    let mut u = union_find::UF::new(h * w);
    for (i, x) in x.iter().enumerate().skip(1) {
        if *x == inf {
            break;
        }
        let mut imos = vec![0; w];
        for &(_, e, f) in row.iter().filter(|p| p.0 == *x) {
            let l = match y.binary_search(&e) {
                Ok(k) => k,
                Err(k) => k,
            };
            let r = match y.binary_search(&f) {
                Ok(k) => k,
                Err(k) => k - 1,
            };
            if l < r {
                imos[l] += 1;
                imos[r] -= 1;
            }
        }
        for j in 1..w {
            imos[j] += imos[j - 1];
        }
        for (j, _) in imos.iter().enumerate().filter(|c| *c.1 == 0) {
            u.unite((i - 1) * w + j, i * w + j);
        }
    }
    for (j, y) in y.iter().enumerate().skip(1) {
        if *y == inf {
            break;
        }
        let mut imos = vec![0; h];
        for &(a, b, _) in col.iter().filter(|p| p.2 == *y) {
            let l = match x.binary_search(&a) {
                Ok(k) => k,
                Err(k) => k,
            };
            let r = match x.binary_search(&b) {
                Ok(k) => k,
                Err(k) => k - 1,
            };
            if l < r {
                imos[l] += 1;
                imos[r] -= 1;
            }
        }
        for j in 1..h {
            imos[j] += imos[j - 1];
        }
        for (i, _) in imos.iter().enumerate().filter(|c| *c.1 == 0) {
            u.unite(i * w + j - 1, i * w + j);
        }
    }
    let mut p = 0;
    while x[p + 1] <= 0 {
        p += 1;
    }
    let mut q = 0;
    while y[q + 1] <= 0 {
        q += 1;
    }
    let root = u.root(p * w + q);
    let mut ans = 0;
    for (i, x) in x.windows(2).map(|x| (x[1] - x[0]) / 2).enumerate() {
        for (j, y) in y.windows(2).map(|y| (y[1] - y[0]) / 2).enumerate() {
            if u.root(i * w + j) == root {
                if i == 0 || i == h - 1 || j == 0 || j == w - 1 {
                    println!("INF");
                    return;
                }
                ans += x * y;
            }
        }
    }
    ans /= 4;
    println!("{}", ans);
}

fn main() {
    run();
}