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 <stdio.h>

typedef struct {
	int key, id;
} data;

void merge_sort(data x[], int n)
{
	static data y[3001] = {};
	if (n <= 1) return;
	merge_sort(&(x[0]), n/2);
	merge_sort(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

void merge_sort_int(int x[], int n)
{
	static int y[3001] = {};
	if (n <= 1) return;
	merge_sort_int(&(x[0]), n/2);
	merge_sort_int(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p] < x[q])? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

int main()
{
	int i, j, k, m, n, N, M, A[1001], B[1001], C[1001], D[1001], E[1001], F[1001], X[3001], Y[3001];
	scanf("%d %d", &N, &M);
	for (i = 1, j = 0, k = 0; i <= N; i++) {
		scanf("%d %d %d", &(A[i]), &(B[i]), &(C[i]));
		X[j++] = A[i];
		X[j++] = B[i];
		Y[k++] = C[i];
	}
	for (i = 1; i <= M; i++) {
		scanf("%d %d %d", &(D[i]), &(E[i]), &(F[i]));
		X[j++] = D[i];
		Y[k++] = E[i];
		Y[k++] = F[i];
	}
	m = N * 2 + M;
	n = N + M * 2;
	merge_sort_int(X, m);
	merge_sort_int(Y, n);
	for (i = 1, j = 1; i < m; i++) if (X[i] > X[j-1]) X[j++] = X[i];
	for (i = 1, k = 1; i < n; i++) if (Y[i] > Y[k-1]) Y[k++] = Y[i];
	m = j;
	n = k;
	
	/* long long area[3001][3001];
	for (j = 1; j < m; j++) {
		for (k = 1; k < n; k++) area[j][k] = (long long)(X[j] - X[j-1]) * (Y[k] - Y[k-1]);
	} */
	
	data hor[1001], vert[1001];
	for (i = 0; i < N; i++) {
		hor[i].key = C[i+1];
		hor[i].id = i + 1;
	}
	for (i = 0; i < M; i++) {
		vert[i].key = D[i+1];
		vert[i].id = i + 1;
	}
	merge_sort(hor, N);
	merge_sort(vert, M);
	for (i = 0; i < N - 1; i++) {
		if (hor[i].key < hor[i+1].key) continue;
		for (j = i; j < N && hor[j].key == C[hor[i].id]; j++) hor[j].key = A[hor[j].id];
		merge_sort(&(hor[i]), j - i);
		i = j - 1;
	}
	for (i = 0; i < M - 1; i++) {
		if (vert[i].key < vert[i+1].key) continue;
		for (k = i; k < M && vert[k].key == D[vert[i].id]; k++) vert[k].key = E[vert[k].id];
		merge_sort(&(vert[i]), k - i);
		i = k - 1;
	}
	
	char adj_hor[3001][3001] = {}, adj_vert[3001][3001] = {};
	for (j = 0, i = 0; j <= m; j++) {
		for (k = 1; k <= n; k++) {
			if (i >= M || D[vert[i].id] > X[j] || E[vert[i].id] > Y[k-1]) adj_hor[j][k] = 1;
			else {
				for (i++; i < M && D[vert[i].id] == X[j] && E[vert[i].id] <= F[vert[i-1].id]; i++);
				for (; k <= n && Y[k] <= F[vert[i-1].id]; k++);
				if (k <= n) adj_hor[j][k] = 1;
			}
		}
	}
	for (k = 0, i = 0; k <= n; k++) {
		for (j = 1; j <= m; j++) {
			if (i >= N || C[hor[i].id] > Y[k] || A[hor[i].id] > X[j-1]) adj_vert[j][k] = 1;
			else {
				for (i++; i < N && C[hor[i].id] == Y[k] && A[hor[i].id] <= B[hor[i-1].id]; i++);
				for (; j <= m && X[j] <= B[hor[i-1].id]; j++);
				if (j <= m) adj_vert[j][k] = 1;
			}
		}
	}
	
	char flag[3001][3001] = {};
	int q[1000001][2], head, tail;
	long long ans = 0;
	for (j = 0; j < m && X[j] < 0; j++);
	for (k = 0; k < n && Y[k] < 0; k++);
	flag[j][k] = 1;
	q[0][0] = j;
	q[0][1] = k;
	for (head = 0, tail = 1; head < tail; head++) {
		j = q[head][0];
		k = q[head][1];
		if (j == 0 || j == m || k == 0 || k == n) {
			printf("INF\n");
			fflush(stdout);
			return 0;
		} else ans += (long long)(X[j] - X[j-1]) * (Y[k] - Y[k-1]);
		if (flag[j-1][k] == 0 && adj_hor[j-1][k] == 1) {
			flag[j-1][k] = 1;
			q[tail][0] = j - 1;
			q[tail++][1] = k;
		}
		if (flag[j+1][k] == 0 && adj_hor[j][k] == 1) {
			flag[j+1][k] = 1;
			q[tail][0] = j + 1;
			q[tail++][1] = k;
		}
		if (flag[j][k-1] == 0 && adj_vert[j][k-1] == 1) {
			flag[j][k-1] = 1;
			q[tail][0] = j;
			q[tail++][1] = k - 1;
		}
		if (flag[j][k+1] == 0 && adj_vert[j][k] == 1) {
			flag[j][k+1] = 1;
			q[tail][0] = j;
			q[tail++][1] = k + 1;
		}
	}
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}