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;
using ll = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    // int ti = clock();
    int N, M;
    cin >> N >> M;
    vector<int> A(N), B(N), C(N);
    for(int i=0; i<N; i++)
    {
        cin >> A[i] >> B[i] >> C[i];
    }
    vector<int> D(M), E(M), F(M);
    for(int i=0; i<M; i++)
    {
        cin >> D[i] >> E[i] >> F[i];
    }
    // int N=1000, M=1000;
    // vector<int> A(N, -1000000000);
    // vector<int> B(N, 1000000000);
    // vector<int> C(N, 999999999);
    // for(int i=0; i<N; i++)
    // {
    //     A[i] += i*10;
    //     B[i] -= i*10;
    //     C[i] -= i*10;
    //     if(i%2) C[i] *= -1;
    // }
    // vector<int> D(N, -999999999);
    // vector<int> E(N, -1000000000);
    // vector<int> F(N, 1000000000);
    // for(int i=0; i<N; i++)
    // {
    //     D[i] += i*10;
    //     if(i%2) D[i] *= -1;
    //     E[i] += i*10;
    //     F[i] -= i*10;
    // }
    // cout << "input: ";
    // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;


    vector<int> X, Y;
    X.push_back(0); Y.push_back(0);
    for(int i=0; i<N; i++)
    {
        X.push_back(A[i]);
        X.push_back(B[i]);
        Y.push_back(C[i]);
    }
    for(int i=0; i<M; i++)
    {
        X.push_back(D[i]);
        Y.push_back(E[i]);
        Y.push_back(F[i]);
    }

    sort(X.begin(), X.end());
    X.erase(unique(X.begin(), X.end()), X.end());
    sort(Y.begin(), Y.end());
    Y.erase(unique(Y.begin(), Y.end()), Y.end());
    // cout << "prepare X Y: ";
    // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;

    // インデックスが偶数の行・列に壁(-1)を入れる。奇数マスは面積を入れる。
    vector<vector<int>> mp(2*X.size()-1, vector<int>(2*Y.size()-1, 0));
    // 面積埋め
    for(int i=1; i<2*X.size()-1; i+=2)
    {
        int height = X[i/2+1] - X[i/2];
        for(int j=1; j<2*Y.size()-1; j+=2)
        {
            int width = Y[i/2+1] - Y[i/2];
            mp[i][j] = height * width;
        }
    }
    // 壁埋め
    for(int i=0; i<N; i++)
    {
        int aidx = lower_bound(X.begin(), X.end(), A[i]) - X.begin();
        int bidx = lower_bound(X.begin(), X.end(), B[i]) - X.begin();
        int cidx = lower_bound(Y.begin(), Y.end(), C[i]) - Y.begin();
        for(int j=2*aidx; j<=2*bidx; j++)
        {
            mp[j][2*cidx] = -1;
        }
    }
    for(int i=0; i<M; i++)
    {
        int didx = lower_bound(X.begin(), X.end(), D[i]) - X.begin();
        int eidx = lower_bound(Y.begin(), Y.end(), E[i]) - Y.begin();
        int fidx = lower_bound(Y.begin(), Y.end(), F[i]) - Y.begin();
        for(int j=2*eidx; j<=2*fidx; j++)
        {
            mp[2*didx][j] = -1;
        }
    }
    // cout << "prepare mp: ";
    // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
    // for(auto vec : mp)
    // {
    //     for(auto num : vec)
    //     {
    //         if(num == -1) cout << "  * ";
    //         else cout << setw(3) << num << ' ';
    //     }
    //     cout << '\n';
    // }

    // BFS
    int sx = 2*(lower_bound(X.begin(), X.end(), 0) - X.begin());
    int sy = 2*(lower_bound(Y.begin(), Y.end(), 0) - Y.begin());
    queue<pair<int, int>> q;
    q.emplace(sx, sy);
    ll ans = 0;
    vector<vector<bool>> is_reached(2*X.size()-1, vector<bool>(2*Y.size()-1, false));
    is_reached[sx][sy] = true;
    vector<pair<int, int>> motion;
    motion.emplace_back(1, 0);
    motion.emplace_back(-1, 0);
    motion.emplace_back(0, 1);
    motion.emplace_back(0, -1);
    bool reached_edge = false;
    int sttime = clock();
    // cout << "start bfs: ";
    // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
    while(!q.empty() && !reached_edge)
    {
        pair<int, int> cp = q.front();
        q.pop();
        for(auto m : motion)
        {
            int nx = cp.first + m.first;
            int ny = cp.second + m.second;
            if(nx < 0 || 2*X.size()-1 <= nx || ny < 0 || 2*Y.size()-1 <= ny)
            {
                reached_edge = true;
                break;
            }
            if(mp[nx][ny] == -1 || is_reached[nx][ny]) continue;
            is_reached[nx][ny] = true;
            ans += mp[nx][ny];
            q.emplace(nx, ny);
        }
    }
    // cout << "end bfs: ";
    // cout << fixed << setprecision(4) << 1.0 * (clock() - sttime) / CLOCKS_PER_SEC << endl;
    if(reached_edge) cout << "INF\n";
    else cout << ans << '\n';

    return 0;
}