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

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
bool check[2010][2010] = {};
bool line_yoko[2010][2010] = {};
bool line_tate[2010][2010] = {};

void solve(){
    ll N,M;
    cin >> N >> M;
    ll A[1010], B[1010], C[1010];
    ll D[1010], E[1010], F[1010];

    // 座標圧縮
    vector<ll> x, y;
    x.push_back(-INF); x.push_back(INF);
    y.push_back(-INF); y.push_back(INF);
    x.push_back(0); y.push_back(0);
    rep(i,0,N){
        cin >> A[i] >> B[i] >> C[i];
        y.push_back(C[i]);
        x.push_back(A[i]); x.push_back(B[i]);
    }
    rep(i,0,M){
        cin >> D[i] >> E[i] >> F[i];
        x.push_back(D[i]);
        y.push_back(E[i]); y.push_back(F[i]);
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    y.erase(unique(y.begin(), y.end()), y.end());
    vector<ll> diff_x, diff_y;
    ll sz_x = x.size();
    rep(i,0,sz_x-1){
        diff_x.push_back(x[i+1] - x[i]);
    }
    ll sz_y = y.size();
    rep(i,0,sz_y-1){
        diff_y.push_back(y[i+1] - y[i]);
    }
    rep(i,0,N){
        A[i] = lower_bound(x.begin(), x.end(), A[i]) - x.begin();
        B[i] = lower_bound(x.begin(), x.end(), B[i]) - x.begin();
        C[i] = lower_bound(y.begin(), y.end(), C[i]) - y.begin();
    }
    rep(i,0,M){
        D[i] = lower_bound(x.begin(), x.end(), D[i]) - x.begin();
        E[i] = lower_bound(y.begin(), y.end(), E[i]) - y.begin();
        F[i] = lower_bound(y.begin(), y.end(), F[i]) - y.begin();
    }

    // printa(diff_x, sz_x-1);
    // printa(diff_y, sz_y-1);


    rep(i,0,N){
        rep(j,A[i],B[i]){
            line_tate[j][C[i]] = true;
        }
    }
 
    rep(i,0,M){
        rep(j,E[i],F[i]){
            line_yoko[D[i]][j] = true;
        }
    }

    queue<lpair> que;
    ll x0 = lower_bound(x.begin(), x.end(), 0) - x.begin();
    ll y0 = lower_bound(y.begin(), y.end(), 0) - y.begin();
    que.push({x0,y0});
    bool ok = true;

    while(not que.empty()){
        lpair l1 = que.front();
        que.pop();
        ll x = l1.first, y = l1.second;
        if(x > 2000 || y > 2000){
            ok = false;
            break;
        }

        if(x < 0 || y < 0) continue;
        if(check[x][y]) continue;
        check[x][y] = true;
        if(not line_yoko[x+1][y]){
            if(not check[x+1][y]) que.push({x+1, y});
        }
        if(not line_yoko[x][y]){
            if(not check[x-1][y]) que.push({x-1, y});
        }
        if(not line_tate[x][y+1]){
            if(not check[x][y+1]) que.push({x, y+1});
        }
        if(not line_tate[x][y]){
            if(not check[x][y-1]) que.push({x, y-1});
        }
    }
    if(not ok){
        print("INF");
        return;
    }
    ll ans = 0;
    rep(i,0,2001){
        rep(j,0,2001){
            if(check[i][j]){
                ans += diff_x[i] * diff_y[j];
            }
        }
    }
    print(ans);
    // rep(i,0,10){
    //     rep(j,0,10){
    //         cout << check[i][j];
    //     }
    //     cout << endl;
    // }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}