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 Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		int[] a = new int[n];
		int[] b = new int[n];
		int[] c = new int[n];
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			a[i] = Integer.parseInt(sa[0]);
			b[i] = Integer.parseInt(sa[1]);
			c[i] = Integer.parseInt(sa[2]);
		}
		int[] d = new int[m];
		int[] e = new int[m];
		int[] f = new int[m];
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			d[i] = Integer.parseInt(sa[0]);
			e[i] = Integer.parseInt(sa[1]);
			f[i] = Integer.parseInt(sa[2]);
		}
		br.close();

		TreeMap<Integer, Integer> mapx = new TreeMap<>();
		mapx.put(0, null);
		for (int i = 0; i < n; i++) {
			mapx.put(a[i], null);
			mapx.put(b[i], null);
		}
		for (int i = 0; i < m; i++) {
			mapx.put(d[i], null);
		}
		Integer[] arr = mapx.keySet().toArray(new Integer[0]);
		int cnt = 0;
		for (Integer i : arr) {
			mapx.put(i, cnt);
			cnt++;
		}
		int[] tox = new int[mapx.size()];
		for (Integer key : mapx.keySet()) {
			tox[mapx.get(key)] = key;
		}

		TreeMap<Integer, Integer> mapy = new TreeMap<>();
		mapy.put(0, null);
		for (int i = 0; i < n; i++) {
			mapy.put(c[i], null);
		}
		for (int i = 0; i < m; i++) {
			mapy.put(e[i], null);
			mapy.put(f[i], null);
		}
		arr = mapy.keySet().toArray(new Integer[0]);
		cnt = 0;
		for (Integer i : arr) {
			mapy.put(i, cnt);
			cnt++;
		}
		int[] toy = new int[mapy.size()];
		for (Integer key : mapy.keySet()) {
			toy[mapy.get(key)] = key;
		}


		boolean[][] ngy = new boolean[tox.length + 1][toy.length + 1];
		for (int i = 0; i < n; i++) {
			int x1 = mapx.get(a[i]);
			int x2 = mapx.get(b[i]);
			int y = mapy.get(c[i]);
			for (int j = x1 + 1; j <= x2; j++) {
				ngy[j][y] = true;
			}
		}

		boolean[][] ngx = new boolean[tox.length + 1][toy.length + 1];
		for (int i = 0; i < m; i++) {
			int x = mapx.get(d[i]);
			int y1 = mapy.get(e[i]);
			int y2 = mapy.get(f[i]);
			for (int j = y1 + 1; j <= y2; j++) {
				ngx[x][j] = true;
			}
		}


		boolean[][] visit = new boolean[tox.length + 1][toy.length + 1];
		int fx = mapx.get(0);
		int fy = mapx.get(0);
		if (fx == 0 || fx == tox.length - 1 || fy == 0 || fy == toy.length - 1) {
			System.out.println("INF");
			return;
		}

		Queue<Integer> que = new ArrayDeque<>();
		que.add(fx * 10000 + fy);
		visit[fx][fy] = true;
		while (!que.isEmpty()) {
			int cur = que.poll();
			int cx = cur / 10000;
			int cy = cur % 10000;
			// 上
			int nx = cx - 1;
			int ny = cy;
			if (!ngx[nx][ny] && !visit[nx][ny]) {
				if (nx == 0) {
					System.out.println("INF");
					return;
				}
				que.add(nx * 10000 + ny);
				visit[nx][ny] = true;
			}
			// 下
			nx = cx + 1;
			ny = cy;
			if (!ngx[cx][ny] && !visit[nx][ny]) {
				if (nx == tox.length) {
					System.out.println("INF");
					return;
				}
				que.add(nx * 10000 + ny);
				visit[nx][ny] = true;
			}
			// 左
			nx = cx;
			ny = cy - 1;
			if (!ngy[nx][ny] && !visit[nx][ny]) {
				if (ny == 0) {
					System.out.println("INF");
					return;
				}
				que.add(nx * 10000 + ny);
				visit[nx][ny] = true;
			}
			// 右
			nx = cx;
			ny = cy + 1;
			if (!ngy[nx][cy] && !visit[nx][ny]) {
				if (ny == toy.length) {
					System.out.println("INF");
					return;
				}
				que.add(nx * 10000 + ny);
				visit[nx][ny] = true;
			}
		}

		long ans = 0;
		for (int i = 1; i < tox.length - 1; i++) {
			for (int j = 1; j < toy.length - 1; j++) {
				if (visit[i][j]) {
					long dx = tox[i + 1] - tox[i];
					long dy = toy[j + 1] - toy[j];
					ans += dx * dy;
				}
			}
		}
		System.out.println(ans);
	}
}