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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = (int)3e3 + 9, INF = 0x3f3f3f3f;
int n, m, xtot, xque[maxn], ytot, yque[maxn];
struct Line {
	int L, R, pos;
	void read() {
		scanf("%d%d%d", &L, &R, &pos);
	}
} er[maxn], ec[maxn];
bool vis[maxn][maxn], banL[maxn][maxn], banR[maxn][maxn], banU[maxn][maxn], banD[maxn][maxn];
void dfs(int x, int y, LL &ans, bool &inf) {
	if(vis[x][y])
		return;
	vis[x][y] = 1;
	inf |= x == 0 || x == xtot - 2 || y == 0 || y == ytot - 2;
	ans += (LL)(xque[x + 1] - xque[x]) * (yque[y + 1] - yque[y]);
	if(!banL[x][y])
		dfs(x, y - 1, ans, inf);
	if(!banR[x][y])
		dfs(x, y + 1, ans, inf);
	if(!banU[x][y])
		dfs(x - 1, y, ans, inf);
	if(!banD[x][y])
		dfs(x + 1, y, ans, inf);
}
void show() {
		for(int j = 0; j < ytot - 1; ++j)
			printf(" %c", " -"[banU[0][j]]);
		puts("");
		for(int i = 0; i < xtot - 1; ++i) {
			printf("%c", " |"[banL[i][0]]);
			for(int j = 0; j < ytot - 1; ++j) {
				printf("%d", vis[i][j]);
				printf("%c", " |"[banR[i][j]]);
				assert(j == ytot - 2 || banR[i][j] == banL[i][j + 1]);
			}
			puts("");
			for(int j = 0; j < ytot - 1; ++j) {
				printf(" %c", " -"[banD[i][j]]);
				assert(i == xtot - 2 || banU[i + 1][j] == banD[i][j]);
			}
			puts("");
		}
}
int main() {
	int T = 1;
	// scanf("%d", &T);
	for(int Case = 1; Case <= T; ++Case) {
		scanf("%d%d", &n, &m);
		xtot = ytot = 0;
		xque[xtot++] = yque[ytot++] = -INF;
		xque[xtot++] = yque[ytot++] = INF;
		for(int i = 0; i < n; ++i) {
			Line &cur = er[i];
			cur.read();
			xque[xtot++] = cur.L;
			xque[xtot++] = cur.R;
			yque[ytot++] = cur.pos;
		}
		for(int i = 0; i < m; ++i) {
			Line &cur = ec[i];
			cur.read();
			swap(cur.pos, cur.L);
			swap(cur.L, cur.R);
			yque[ytot++] = cur.L;
			yque[ytot++] = cur.R;
			xque[xtot++] = cur.pos;
		}
		sort(xque, xque + xtot);
		xtot = unique(xque, xque + xtot) - xque;
		sort(yque, yque + ytot);
		ytot = unique(yque, yque + ytot) - yque;
		for(int i = 0; i < xtot - 1; ++i)
			for(int j = 0; j < ytot - 1; ++j) {
				vis[i][j] = 0;
				banL[i][j] = j == 0;
				banR[i][j] = j == ytot - 2;
				banU[i][j] = i == 0;
				banD[i][j] = i == xtot - 2;
			}
		for(int i = 0; i < n; ++i) {
			Line &cur = er[i];
			int xL = lower_bound(xque, xque + xtot, cur.L) - xque;
			int xR = lower_bound(xque, xque + xtot, cur.R) - xque;
			int y = lower_bound(yque, yque + ytot, cur.pos) - yque;
			for(int x = xL; x < xR; ++x) {
				banL[x][y] = 1;
				banR[x][y - 1] = 1;
			}
//			printf("ban [%d, %d) x %d\n", xL, xR, y);
//			show();
		}
		for(int i = 0; i < m; ++i) {
			Line &cur = ec[i];
			int yL = lower_bound(yque, yque + ytot, cur.L) - yque;
			int yR = lower_bound(yque, yque + ytot, cur.R) - yque;
			int x = lower_bound(xque, xque + xtot, cur.pos) - xque;
			for(int y = yL; y < yR; ++y) {
				banU[x][y] = 1;
				banD[x - 1][y] = 1;
			}
//			printf("ban %d x [%d, %d)\n", x, yL, yR);
//			show();
		}
		int sx = lower_bound(xque, xque + xtot, 0) - xque - 1;
		int sy = lower_bound(yque, yque + ytot, 0) - yque - 1;
		LL ans = 0;
		bool inf = 0;
		dfs(sx, sy, ans, inf);
		if(inf) {
			puts("INF");
		} else {
			printf("%lld\n", ans);
		}
//		printf("%d %d\n", sx, sy);
//		show();
	}
	return 0;
}