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: Runtime Error)

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    constexpr double PI = acos(-1);
    cout << fixed << setprecision(16);
    cin.tie(0); ios::sync_with_stdio(false);

    ll n, m;
    cin >> n >> m;

    vector<tuple<ll,ll,ll>> def(m), cab(n);
    for(auto &[c, a, b]: cab) cin >> a >> b >> c;
    for(auto &[a, b, c]: def) cin >> a >> b >> c;

    sort(cab.begin(), cab.end());
    sort(def.begin(), def.end());

    vector<ll> xs(5*(n+m), 1e10), ys(5*(n+m), 1e10);
    {
        ll xi = 0, yi = 0;
        xs[xi++] = -1e10;
        ys[yi++] = -1e10;
        xs[xi++] =  1e10;
        ys[yi++] =  1e10;

        for(auto &[c, a, b]: cab) {
            xs[xi++] = a;
            xs[xi++] = b;
            ys[yi++] = c;
        }
        for(auto &[d, e, f]: def) {
            xs[xi++] = d;
            ys[yi++] = e;
            ys[yi++] = f;
        }

        sort(xs.begin(), xs.end());
        sort(ys.begin(), ys.end());
        xs.erase(unique(xs.begin(), xs.end()), xs.end());
        ys.erase(unique(ys.begin(), ys.end()), ys.end());
    }

    auto sur = [&](ll x, ll y) -> ll {
        if(xs[x+1] == 1e10 || ys[y+1] == 1e10 || xs[x] == -1e10 || ys[y] == -1e10)
            return 3e18;
        return (xs[x+1] - xs[x]) * (ys[y+1] - ys[y]);
    };

    {
        vector<tuple<ll,ll,ll>> ncab;
        for(ll i=0; i<n; ) {
            vector<pair<ll,ll>> v;
            ll cc = get<0>(cab[i]);

            while(i<n) {
                auto [c, a, b] = cab[i];
                if(c != cc) break;
                v.push_back({a, 0});
                v.push_back({b, 1});
                i++;
            }
            sort(v.begin(), v.end());

            ll s = 0, p = -1e9;
            for(auto [x, t]: v) {
                if(t == 0) {
                    if(s++ == 0) p = x;
                } else {
                    if(--s == 0) ncab.push_back({cc, p, x});
                }
            }
        }
        cab = move(ncab);
    }
    {
        vector<tuple<ll,ll,ll>> ndef;
        for(ll i=0; i<m; ) {
            vector<pair<ll,ll>> v;
            ll cc = get<0>(def[i]);

            while(i<m) {
                auto [c, a, b] = def[i];
                if(c != cc) break;
                v.push_back({a, 0});
                v.push_back({b, 1});
                i++;
            }
            sort(v.begin(), v.end());

            ll s = 0, p = -1e9;
            for(auto [x, t]: v) {
                if(t == 0) {
                    if(s++ == 0) p = x;
                } else {
                    if(--s == 0) ndef.push_back({cc, p, x});
                }
            }
        }
        def = move(ndef);
    }

    ll si = upper_bound(xs.begin(), xs.end(), 0ll) - xs.begin();
    ll sj = upper_bound(ys.begin(), ys.end(), 0ll) - ys.begin();
    si--, sj--;

    ll res = 0;
    set<pair<ll,ll>> vis;
    {
        auto dfs = [&](auto dfs, ll i, ll j) {
            if(vis.count({i, j})) return;
            vis.insert({i, j});
            // cout << '!' << xs[i] << ' ' << ys[j] << endl;

            res += sur(i, j);
            if(sur(i, j) == 3e18) {
                cout << "INF" << endl;
                exit(0);
            }

            {
                bool ng = false;
                auto it = upper_bound(def.begin(), def.end(), make_tuple(xs[i], ys[j], 1e10));
                if(it!=def.begin()) it--;
                if(it!=def.end() && get<0>(*it) == xs[i]) {
                    auto [d, e, f] = *it;
                    if(e <= ys[j] && ys[j+1] <= f) {
                        ng = true;
                    }
                }
                if(!ng) dfs(dfs, i-1, j);
            }
            {
                bool ng = false;
                auto it = upper_bound(def.begin(), def.end(), make_tuple(xs[i+1], ys[j], 1e10));
                if(it!=def.begin()) it--;
                if(it!=def.end() && get<0>(*it) == xs[i+1]) {
                    auto [d, e, f] = *it;
                    if(e <= ys[j] && ys[j+1] <= f) {
                        ng = true;
                    }
                }
                if(!ng) dfs(dfs, i+1, j);
            }
            {
                bool ng = false;
                auto it = upper_bound(cab.begin(), cab.end(), make_tuple(ys[j], xs[i], 1e10));
                if(it!=cab.begin()) it--;
                if(it!=cab.end() && get<0>(*it) == ys[j]) {
                    auto [c, a, b] = *it;
                    if(a <= xs[i] && xs[i+1] <= b) {
                        ng = true;
                    }
                }
                if(!ng) dfs(dfs, i, j-1);
            }
            {
                bool ng = false;
                auto it = upper_bound(cab.begin(), cab.end(), make_tuple(ys[j+1], xs[i], 1e10));
                if(it!=cab.begin()) it--;
                if(it!=cab.end() && get<0>(*it) == ys[j+1]) {
                    auto [c, a, b] = *it;
                    if(a <= xs[i] && xs[i+1] <= b) {
                        ng = true;
                    }
                }
                if(!ng) dfs(dfs, i, j+1);
            }
        };

        dfs(dfs, si, sj);
    }
    cout << res << endl;

}