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 <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <map>
#include <queue>
using namespace std;

int N, M;
long long int A[1050], B[1050], C[1050], D[1050], E[1050], F[1050];

set<long long int> SX, SY;
long long int INF = 9999999999;

vector<long long int> X;
vector<long long int > Y;
map<long long int, int> NX, NY;

int num_X, num_Y;

set<pair<int, int> > Xp, Xm, Yp, Ym;

vector<pair<int,int> > Adj[1050][1050];

queue<pair<int, int>> Q;
int used[1050][1050];

int main(void) {

	cin >> N >> M;
	for (int i = 1; i <= N; i++) {
		cin >> A[i] >> B[i] >> C[i];
		SX.insert(A[i]); SX.insert(B[i]); SY.insert(C[i]);
	}
	for (int i = 1; i <= M; i++) {
		cin >> D[i] >> E[i] >> F[i];
		SX.insert(D[i]); SY.insert(E[i]); SY.insert(F[i]);
	}

	 SX.insert(0); 
	 SY.insert(0);

	 X.push_back(-INF);
	 num_X = 0;

	for (auto v : SX) {
		NX[v] = X.size();
		num_X++;
		X.push_back(v);
	}

	
	Y.push_back(-INF);
	num_Y = 0;

	for (auto v : SY) {
		NY[v] = Y.size();
		num_Y++;
	    Y.push_back(v);
	}

	


	for (int k = 1; k <= N; k++) {
		int j = NY[C[k]]-1;
		//cout << "k="<< k << endl;	
		for (int i = 1; i <= num_X - 1; i++) {
			long long int a = X[i];
			long long int b = X[i + 1];
			//cout << "a= " << a << " b= " << b;
			if (A[k] <= a && b <= B[k]) { Yp.insert(make_pair(i, j)); Ym.insert(make_pair(i, j + 1));  }
			
		}
	}

	for (int k = 1; k <= M; k++) {
		int i = NX[D[k]]-1;
		for (int j = 1; j <= num_Y - 1; j++) {
			long long int e = Y[j];
			long long int f = Y[j + 1];
			if (E[k] <= e && f <= F[k]) { Xp.insert(make_pair(i, j)); Xm.insert(make_pair(i+1, j)); }
		}
	}

	for (int i = 0; i <= num_X; i++) {
		for (int j = 0; j <= num_Y; j++) {
			if (Xm.count(make_pair(i, j)) == 0 && i >= 1) { Adj[i][j].push_back(make_pair(i - 1, j)); }
			if (Xp.count(make_pair(i, j)) == 0) { Adj[i][j].push_back(make_pair(i + 1, j)); }
			if (Ym.count(make_pair(i, j)) == 0 && j >= 1) { Adj[i][j].push_back(make_pair(i, j - 1)); }
			if (Yp.count(make_pair(i, j)) == 0) { Adj[i][j].push_back(make_pair(i, j + 1)); }
		}
	}


	Q.push(make_pair(NX[0], NY[0])); used[NX[0]][NY[0]] = 1;

	while (Q.size() > 0) {
		pair<int, int> P = Q.front(); Q.pop();
		used[P.first][P.second] = 1;

		for (auto z : Adj[P.first][P.second]) {
			if (used[z.first][z.second] == 0) {
				Q.push(z);
			}
		}
	}

	int sign = 1;

	for (int i = 0; i <= num_X; i++) {
		if (used[i][0] == 1 || used[i][num_Y] == 1) { sign = -1; }
	}

	for (int j = 0; j <= num_Y; j++) {
		if (used[0][j] == 1 || used[num_X][j] == 1) { sign = -1; }
	}
	

	long long int ans = 0;

	for (int i = 1; i <= num_X-1; i++) {
		for (int j = 1; j <= num_Y; j++) {
			if (used[i][j] == 1) { ans += (X[i + 1] - X[i])*(Y[j + 1] - Y[j]); }
		}
	}
	if (sign == -1) { cout << "INF" << endl; }
	else { cout << ans << endl; }


	/*for (int i = 0; i <= num_X; i++) {
		for (int j = 0; j <= num_Y; j++) {
			cout << " ";
			if (Xm.count(make_pair(i, j)) > 0) { cout << "-"; }
			else { cout << " "; }
		}cout << endl;
		for (int j = 0; j <= num_Y; j++) {
			if (Ym.count(make_pair(i, j)) > 0) { cout << "l"; }
			else { cout << " "; }
			cout << "o";
			
			//if (Yp.count(make_pair(i, j)) > 0) { cout << "l"; }
			//else { cout << " "; }
		}cout << endl;
	}*/

	return 0;
}