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)

// Sky's the limit :)
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 4005;
const int INF = 1e10 + 5;

int n, m;
int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> vx{-INF, INF}, vy{-INF, INF};
bool bu[N][N], bd[N][N], bl[N][N], br[N][N], vis[N][N];

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

bool check(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < n && !vis[x][y];
}

void bfs(int x, int y) {
    queue<pair<int, int> > q;
    vis[x][y] = true;
    q.push({x, y});
    while(!q.empty()) {
        auto p = q.front();
        q.pop();
        int x = p.first;
        int y = p.second;

        // cout << x << ' ' << y << '\n';

        for(int i = 0; i < 4; i++) {
            if((i == 0 && bu[x][y]) || (i == 1 && bd[x][y]) || (i == 2 && br[x][y]) || (i == 3 && bl[x][y])) 
                continue;
            
            int xn = x + dx[i];
            int yn = y + dy[i];
            if(check(xn, yn)) {
                vis[xn][yn] = true;
                q.push({xn, yn});
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    int T = 1; 
    // cin >> T; 
    while (T--) {
        cin >> n >> m;
        for(int i = 0; i < n; i++) {
            cin >> a[i] >> b[i] >> c[i];
            vx.push_back(a[i]);
            vx.push_back(b[i]);
            vy.push_back(c[i]);
        }
        for(int i = 0; i < m; i++) {
            cin >> d[i] >> e[i] >> f[i];
            vx.push_back(d[i]);
            vy.push_back(e[i]);
            vy.push_back(f[i]);
        }

        sort(vx.begin(), vx.end());
        sort(vy.begin(), vy.end());
        vx.resize(unique(vx.begin(), vx.end()) - vx.begin());
        vy.resize(unique(vy.begin(), vy.end()) - vy.begin());

        for(int i = 0; i < n; i++) {
            int la = lower_bound(vx.begin(), vx.end(), a[i]) - vx.begin();
            int lb = lower_bound(vx.begin(), vx.end(), b[i]) - vx.begin();
            int lc = lower_bound(vy.begin(), vy.end(), c[i]) - vy.begin();
            for(int j = la; j < lb; j++) 
                bu[j][lc - 1] = bd[j][lc] = true;
        }
        for(int i = 0; i < m; i++) {
            int ld = lower_bound(vx.begin(), vx.end(), d[i]) - vx.begin();
            int le = lower_bound(vy.begin(), vy.end(), e[i]) - vy.begin();
            int lf = lower_bound(vy.begin(), vy.end(), f[i]) - vy.begin();
            for(int j = le; j < lf; j++) 
                br[ld - 1][j] = bd[ld][j] = true;
        }

        int x = --lower_bound(vx.begin(), vx.end(), 0) - vx.begin();
        int y = --lower_bound(vy.begin(), vy.end(), 0) - vy.begin();
        bfs(x, y);
        cerr << x << ' ' << y << '\n';

        int nx = vx.size();
        int ny = vy.size();
        cerr << nx << ' ' << ny << '\n';
        if(vis[0][0] || vis[nx - 1][ny - 1])
            return cout << "INF\n", 0;
            
        int ans = 0;
        for(int i = 0; i < nx - 1; i++) {
            for(int j = 0; j < ny - 1; j++) {
                if(vis[i][j])
                    ans += (vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
            }
        }
        cout << ans << '\n';

    }
    
    return 0;
}