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 <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1005;
const int M = 7005;

int a[N], b[N], c[N], d[N], e[N], f[N];
vector <int> vx, vy;
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
bool vis[M][M], ma[M][M];

int get_x(int val){
    return lower_bound(vx.begin(), vx.end(), val) - vx.begin();
}

int get_y(int val){
    return lower_bound(vy.begin(), vy.end(), val) - vy.begin();
}

int main(){
    int n, m; scanf("%d%d", &n, &m);
    vx.push_back(-1.1e9), vx.push_back(0);
    vy.push_back(-1.1e9), vy.push_back(0);
    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];
        vy.push_back(e[i]), vy.push_back(f[i]);
        vx.push_back(d[i]);
    }

    sort(vx.begin(), vx.end()), vx.erase(unique(vx.begin(), vx.end()), vx.end());

    sort(vy.begin(), vy.end()), vy.erase(unique(vy.begin(), vy.end()), vy.end());


    for(int i = 0; i < n; i++){
        a[i] = get_x(a[i]), b[i] = get_x(b[i]), c[i] = get_y(c[i]);
        for(int j = a[i]*2 - 1; j < 2 * b[i]; j++)
            ma[j][2*c[i] - 1] = true;
    }

    for(int i = 0; i < m; i++){
        d[i] = get_x(d[i]), e[i] = get_y(e[i]), f[i] = get_y(f[i]);
        for(int j = e[i]*2-1; j < f[i]*2; j++)
            ma[2*d[i]-1][j] = true;
    }

    int sx = get_x(0)*2 - 1, sy = get_y(0)*2 - 1;

    queue<pair<int, int>> qu;
    qu.push({sx, sy}), vis[sx][sy] = true;
    ll ans = 0;
    bool flag = false;
    while(!qu.empty()){
        int ux = qu.front().first, uy = qu.front().second; qu.pop();
        if(ux == M-1 || uy == M-1 || ux == 0 || uy == 0){
            flag = true;
            break;
        }
        if(ux % 2 == 0 && uy % 2 == 0){
            int x = ux / 2, y = uy / 2;
            ans += 1ll * (vx[x + 1] - vx[x]) * (vy[y + 1] - vy[y]);
        }
        for(int i = 0; i < 4; i++){
            int xx = ux + dx[i], yy = uy + dy[i];
            if(xx >= M || xx < 0 || yy >= M || yy < 0 || ma[xx][yy] || vis[xx][yy]) continue;
            qu.push({xx, yy});
            vis[xx][yy] = true;
        }
    }
    if(flag) printf("INF\n");
    else printf("%lld\n", ans);
    return 0;
}