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)

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>; using vpll = vector<pll>;
using vll = vector<ll>; using vvll = vector<vll>;

pii ch[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main() {
    int n, m; cin >> n >> m;
    vi a(n + 1), b(n + 1), c(n + 1), d(m + 1), e(m + 1), f(m + 1);
    vll x, y;

    const int MX = 1e9;
    x.pb(-(MX + 5)); x.pb(MX + 5);
    y.pb(-(MX + 5)); y.pb(MX + 5);

    FOR(i, 1, n) {
        cin >> a[i] >> b[i] >> c[i];
        x.pb(a[i]); x.pb(b[i]); y.pb(c[i]);
    }

    FOR(i, 1, m) {
        cin >> d[i] >> e[i] >> f[i];
        x.pb(d[i]); y.pb(e[i]); y.pb(f[i]);
    }

    sort(all(x)); sort(all(y));
    auto xt = unique(all(x));
    auto yt = unique(all(y));
    x.erase(xt, x.end());
    y.erase(yt, y.end());

    int sx = lower_bound(all(x), 0) - x.begin(), sy = lower_bound(all(y), 0) - y.begin();
    int xs = x.size() - 1, ys = y.size() - 1;

    // cout << xs << ' ' << ys << endl;
    // for (int i : x) cout << i << ' ';
    // cout << endl;
    // for (int i : y) cout << i << ' ';
    // cout << endl;
    // return 0;
     
    using vb = vector<bool>; using vvb = vector<vb>;
    vvb vis(xs + 1, vb(ys + 1));
    vector <vvb> can(4, vvb(xs + 1, vb(ys + 1, 1)));

    FOR(i, 1, n) {
        int al = lower_bound(all(x), a[i]) - x.begin();
        int bl = lower_bound(all(x), b[i]) - x.begin();
        int cl = lower_bound(all(y), c[i]) - y.begin();
        FOR(j, al, bl - 1)
            can[2][j][cl - 1] = can[3][j][cl] = 0;
    }
    FOR(i, 1, m) {
        int dl = lower_bound(all(x), d[i]) - x.begin();
        int el = lower_bound(all(y), e[i]) - y.begin();
        int fl = lower_bound(all(y), f[i]) - y.begin();
        // cout << dl << ' ' << el << ' ' << fl << endl;
        FOR(j, el, fl - 1)
            can[0][dl - 1][j] = can[1][dl][j] = 0;
    }

    // FOR(k, 0, 3) {
    //     FOR(i, 1, ys) FOR(j, 1, xs) {
    //         cout << can[k][i][j];
    //         if (j == xs) cout << '\n';
    //         else cout << ' ';
    //     }
    //     cout << endl;
    // }
    // cout << sx << ' ' << sy << endl;

    queue <pii> q;
    q.push({sx, sy});
    vis[sx][sy] = 1;
    while (!q.empty()) {
        pii u = q.front(); q.pop();
        FOR(i, 0, 3) {
            pii v = {u.fi + ch[i].fi, u.se + ch[i].se};
            if (!can[i][u.fi][u.se] || v.fi < 1 || v.fi > xs || v.se < 1 || v.se > ys) continue;
            if (!vis[v.fi][v.se]) {
                vis[v.fi][v.se] = 1;
                q.push(v);
            }
        }
    }

    // FOR(i, 1, ys) FOR(j, 1, xs) {
    //     cout << vis[i][j];
    //     if (j == xs) cout << endl;
    //     else cout << ' ';
    // }
    bool inf = 0;
    FOR(i, 1, xs)
        if (vis[i][1] || vis[i][ys]) inf = 1;
    FOR(i, 1, ys)
        if (vis[1][i] || vis[xs][i]) inf = 1;
    if (inf)
        return cout << "INF\n", 0;
    
    ll ans = 0;
    FOR(i, 1, xs) FOR(j, 1, ys) {
        if (vis[i][j])
            ans += (y[i] - y[i - 1]) * (x[j] - x[j - 1]);
    }
    cout << ans << endl;
}