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)

use proconio::input;
use std::collections::BTreeMap;

fn merge_lines(l : &Vec<(i64, (i64, i64))>) -> Vec<(i64, (i64, i64))> {
    let mut res = Vec::new();
    let mut s = None;
    for i in 0..l.len() {
        if s == None { s = Some(l[i]) }
        let mut c = s.unwrap();
        if i == l.len() - 1 || c.0 != l[i+1].0 || (c.1).1 < (l[i+1].1).0 {
            res.push(c);
            s = None
        } else {
            (c.1).1 = (c.1).1.max((l[i+1].1).1);
            s = Some(c);
        }
    }
    res
}

fn dir(prev : (i64, i64), cur : (i64, i64), next : (i64, i64)) -> i32 {
    let x1 = cur.0 - prev.0;
    let y1 = cur.1 - prev.1;
    let x2 = next.0 - cur.0;
    let y2 = next.1 - cur.1;
    let cr = x1 * y2 - x2 * y1;
    if cr > 0 { 0 }
    else if cr < 0 { 2 }
    else if x1 * x2 + y1 * y2 > 0 { 1 }
    else { 3 }
}

fn main() {
    input! {
        n : usize,
        m : usize,
        vin : [((i64, i64), i64); n],
        mut h : [(i64, (i64, i64)); m]
    }
    let mut v = vin.iter().map(|&t| (t.1, t.0)).collect::<Vec<_>>();
    v.sort();
    h.sort();
    let vl = merge_lines(&v);
    let hl = merge_lines(&h);
    let mut pts = Vec::new();
    let mut vtx_v = vec![Vec::new(); vl.len()];
    let mut vtx_h = vec![Vec::new(); hl.len()];
    for i in 0..vl.len() {
        pts.push(((vl[i].1).0, vl[i].0));
        pts.push(((vl[i].1).1, vl[i].0));
        vtx_v[i].push(((vl[i].1).0, vl[i].0));
        vtx_v[i].push(((vl[i].1).1, vl[i].0));
    }
    for j in 0..hl.len() {
        pts.push((hl[j].0, (hl[j].1).0));
        pts.push((hl[j].0, (hl[j].1).1));
        vtx_h[j].push((hl[j].0, (hl[j].1).0));
        vtx_h[j].push((hl[j].0, (hl[j].1).1));
        if (hl[j].1).0 <= 0 && 0 <= (hl[j].1).1 {
            pts.push((hl[j].0, 0));
            vtx_h[j].push((hl[j].0, 0));
        }
    }
    for i in 0..vl.len() {
        for j in 0..hl.len() {
            if (hl[j].1).0 <= vl[i].0 && vl[i].0 <= (hl[j].1).1 && (vl[i].1).0 <= hl[j].0 && hl[j].0 <= (vl[i].1).1 {
                pts.push((hl[j].0, vl[i].0));
                vtx_v[i].push((hl[j].0, vl[i].0));
                vtx_h[j].push((hl[j].0, vl[i].0));
            }
        }
    }
    pts.sort();
    pts.dedup();
    let mut idx = BTreeMap::<(i64, i64), usize>::new();
    for i in 0..pts.len() {
        idx.insert(pts[i], i);
    }
    let mut g = vec![Vec::new(); pts.len()];
    for t in &mut vtx_v {
        t.sort();
        t.dedup();
        for i in 0..t.len()-1 {
            let a = idx[&t[i]];
            let b = idx[&t[i+1]];
            g[a].push(b);
            g[b].push(a);
        }
    }
    for t in &mut vtx_h {
        t.sort();
        t.dedup();
        for i in 0..t.len()-1 {
            let a = idx[&t[i]];
            let b = idx[&t[i+1]];
            g[a].push(b);
            g[b].push(a);
        }
    }

    let mut res = i64::max_value();
    let mut checked = vec![0; pts.len()];
    for i in 0..pts.len() {
        if checked[i] == 1 { continue; }
        if pts[i].1 != 0 { continue; }
        if pts[i].0 < 0 { continue; }
        let mut pos = i;
        let mut prev = (pts[i].0-1, 0);
        let mut cur = pts[i];
        let mut area = 0;
        let mut first_dst = pts.len();
        loop {
            checked[pos] = 1;
            let mut next_idx = 0;
            let mut next_dir = 4;
            for &p in &g[pos] {
                let d = dir(prev, cur, pts[p]);
                if d < next_dir {
                    next_idx = p;
                    next_dir = d;
                }
            }
            if pos == i && next_idx == first_dst { break; }
            let next = pts[next_idx];
            area += cur.0 * next.1 - cur.1 * next.0;
            prev = cur;
            cur = next;
            pos = next_idx;
            if first_dst == pts.len() { first_dst = next_idx; }
        }
        if area > 0 { res = res.min(area); }
    }
    if res == i64::max_value() {
        println!("INF");
    } else {
        println!("{}", res/2);
    }
}