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 C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll INF = 1000000001;
set<ll> sth, stw;

int N, M;
int A[1000], B[1000], C[1000];
int D[1000], E[1000], F[1000];

map<ll, int> mph, mpw;
bool l[4000][4000], r[4000][4000], u[4000][4000], d[4000][4000];
bool used[4000][4000];

int w_max, h_max;
bool is_inf;
void bfs(int i, int j){
    used[i][j] = true;
    queue<P> que;
    if(i >= h_max-1 || j >= w_max-1 || i <= 0 || j <= 0) {
        is_inf = true;
        return;
    }
    if(r[i][j] && !used[i][j+1]) {
        que.push(P(i, j+1));
        used[i][j+1] = true;
    }
    if(l[i][j] && !used[i][j-1]) {
        que.push(P(i, j-1));
        used[i][j-1] = true;
    }
    if(d[i][j] && !used[i+1][j]) {
        que.push(P(i+1, j));
        used[i+1][j] = true;
    }
    if(u[i][j] && !used[i-1][j]) {
        que.push(P(i-1, j));
        used[i-1][j] = true;
    }
    while(!que.empty()){
        P p = que.front();
        que.pop();
        int i = p.first;
        int j = p.second;
        used[i][j] = true;
        if(i >= h_max-1 || j >= w_max-1 || i <= 0 || j <= 0) {
            is_inf = true;
            continue;
        }
        if(r[i][j] && !used[i][j+1]) {
            que.push(P(i, j+1));
            used[i][j+1] = true;
        }
        if(l[i][j] && !used[i][j-1]) {
            que.push(P(i, j-1));
            used[i][j-1] = true;
        }
        if(d[i][j] && !used[i+1][j]) {
            que.push(P(i+1, j));
            used[i+1][j] = true;
        }
        if(u[i][j] && !used[i-1][j]) {
            que.push(P(i-1, j));
            used[i-1][j] = true;
        }
    }
}

vector<int> val_h, val_w;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> M;
    sth.insert(-INF); sth.insert(INF);
    stw.insert(-INF); stw.insert(INF);
    for(int i = 0; i < N; i++){
        cin >> A[i] >> B[i] >> C[i];
        sth.insert(A[i]); sth.insert(B[i]); stw.insert(C[i]);
    }
    for(int i = 0; i < M; i++){
        cin >> D[i] >> E[i] >> F[i];
        stw.insert(E[i]); stw.insert(F[i]); sth.insert(D[i]);
    }
    int cur = 0;
    h_max = sth.size()-1;
    w_max = stw.size()-1;
    for(auto h : sth){
        mph[h] = cur;
        cur++;
        val_h.push_back(h);
    }
    cur = 0;
    for(auto w : stw){
        mpw[w] = cur;
        cur++;
        val_w.push_back(w);
    }
    for(int i = 0; i < 4000; i++){
        for(int j = 0; j < 4000; j++){
            l[i][j] = true;
            r[i][j] = true;
            u[i][j] = true;
            d[i][j] = true;
        }
    }

    // 縦線
    for(int i = 0; i < N; i++){
        int u_ = mph[A[i]];
        int d_ = mph[B[i]];
        int w = mpw[C[i]];
        for(int h = u_; h < d_; h++){
            r[h][w-1] = false;
            l[h][w] = false;
        }
    }

    // 横線
    for(int i = 0; i < M; i++){
        int l_ = mpw[E[i]];
        int r_ = mpw[F[i]];
        int h = mph[D[i]];
        for(int w = l_; w < r_; w++){
            d[h-1][w] = false;
            u[h][w] = false;
        }
    }
    int start_h;
    for(auto i : mph){
        int h = i.first;
        int idx = i.second;
        if(h > 0) break;
        start_h = idx;
    }
    int start_w;
    for(auto i : mpw){
        int w = i.first;
        int idx = i.second;
        if(w > 0) break;
        start_w = idx;
    }
    if(h_max >= 3200 || w_max >= 3200) return 0;
    bfs(start_h, start_w);
    if(is_inf){
        cout << "INF" << endl;
        return 0;
    }
    ll ans = 0;
    for(int i = 0; i < h_max-1; i++){
        for(int j = 0; j < w_max-1; j++){
            if(!used[i][j]) continue;
            ll h = val_h[i+1]-val_h[i];
            ll w = val_w[j+1]-val_w[j];
            ans += h*w;
        }
    }
    cout << ans << endl;
}