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 <vector>
#include <deque>
#include <set>
#include <tuple>
#include <utility>
#include <algorithm>
#include <limits>
using i64 = long long;

int main() {
    constexpr i64 inf = std::numeric_limits<i64>::max() / 2;
    int n, m;
    std::cin >> n >> m;
    std::vector<i64> xs, ys;
    std::vector<std::tuple<i64, i64, i64>> evx, evy;
    for (int i = 0; i < n; i++) {
        i64 a, b, c;
        std::cin >> a >> b >> c;
        xs.push_back(a);
        xs.push_back(b);
        ys.push_back(c);
        evx.emplace_back(c, a, b);
    }
    for (int i = 0; i < m; i++) {
        i64 d, e, f;
        std::cin >> d >> e >> f;
        xs.push_back(d);
        ys.push_back(e);
        ys.push_back(f);
        evy.emplace_back(d, e, f);
    }

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

    const int cx = xs.size(), cy = ys.size();

    std::vector ex(cx, std::vector<int>(cy + 1)), ey(cy, std::vector<int>(cx + 1));
    for (const auto [c, a, b] : evx) {
        const int li = std::lower_bound(xs.begin(), xs.end(), a) - xs.begin(),
            ui = std::lower_bound(xs.begin(), xs.end(), b) - xs.begin(),
            j = std::lower_bound(ys.begin(), ys.end(), c) - ys.begin();
        ey[j][li]++;
        ey[j][ui]--;
    }

    for (const auto [d, e, f] : evy) {
        const int li = std::lower_bound(ys.begin(), ys.end(), e) - ys.begin(),
            ui = std::lower_bound(ys.begin(), ys.end(), f) - ys.begin(),
            j = std::lower_bound(xs.begin(), xs.end(), d) - xs.begin();
        ex[j][li]++;
        ex[j][ui]--;
    }

    for (auto &v : ex) for (int i = 1; i <= cy; i++) v[i] += v[i - 1];
    for (auto &v : ey) for (int i = 1; i <= cx; i++) v[i] += v[i - 1];

    int s = -1;
    auto get = [=](const int x, const int y) {
        return (cy + 1) * x + y;
    };
    std::vector<std::vector<int>> g((cx + 1) * (cy + 1));
    for (int i = 1; i < cx; i++) for (int j = 1; j < cy; j++) {
        const int v = get(i, j);
        if (!ex[i - 1][j - 1]) g[v].push_back(get(i - 1, j));
        if (!ex[i][j - 1]) g[v].push_back(get(i + 1, j));
        if (!ey[j - 1][i - 1]) g[v].push_back(get(i, j - 1));
        if (!ey[j][i - 1]) g[v].push_back(get(i, j + 1));
        const i64 lx = xs[i - 1], ux = xs[i], ly = ys[j - 1], uy = ys[j];
        // fprintf(stderr, "i = %d, j = %d, [%lld, %lld) * [%lld, %lld)\n", i, j, lx, ux, ly, uy);
        if (lx <= 0 && 0 <= ux && ly <= 0 && 0 <= uy) s = v;
    }

    i64 ret = 0;
    std::deque<int> q { s };
    std::vector<int> done(g.size());

    if (s < 0) {
        std::cout << "INF" << std::endl;
        return 0;
    }

    done[s] = 1;
    while (!q.empty()) {
        const int v = q.front();
        q.pop_front();
        const int xi = v / (cy + 1), yi = v % (cy + 1);
        if (xi == 0 || xi == cx || yi == 0 || yi == cy) {
            std::cout << "INF" << std::endl;
            return 0;
        }
        const i64 dx = xs[xi] - xs[xi - 1], dy = ys[yi] - ys[yi - 1];
        ret += dx * dy;
        for (const int to : g[v]) {
            if (done[to]) continue;
            done[to] = 1;
            q.push_back(to);
        }
    }

    if (ret >= inf) std::cout << "INF" << std::endl;
    else std::cout << ret << std::endl;

    return 0;
}