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;
 
#define INF 1234567890
#define ll long long

struct Hline {
	int x, sy, ey;
};

struct Wline {
	int y, sx, ex;
};

int N, M, SY = -1, SX = -1, szY, szX;
vector<int> Y, X;
vector<Hline> H;
vector<Wline> W;
map<int, int> newY, newX;
ll area[7070][7070];
bool vis[7070][7070], cutY[7070][7070], cutX[7070][7070];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void DFS(int y, int x)
{
	vis[y][x] = true;
	for(int i=0; i<4; i++)
	{
		int ny = y + dir[i][0], nx = x + dir[i][1];
		if (nx < x && 0 <= y && y < 7070 && 0 <= x && x < 7070 && cutX[y][x]) continue;
		if (x < nx && 0 <= y && y < 7070 && 0 <= x+1 && x+1 < 7070 && cutX[y][x+1]) continue;
		if (ny < y && 0 <= y && y < 7070 && 0 <= x && x < 7070 && cutY[y][x]) continue;
		if (y < ny && 0 <= y+1 && y+1 < 7070 && 0 <= x && x < 7070 && cutY[y+1][x]) continue;
		if (ny < 0 || nx < 0 || ny >= szY-1 || nx >= szX-1)
		{
			printf("INF");
			exit(0);
		}
		if (vis[ny][nx]) continue;
		DFS(ny, nx);
	}
}

int main()
{
	scanf("%d %d", &N, &M);
	for(int i=1; i<=N; i++) // 세로선
	{
		int sy, ey, x;
		scanf("%d %d %d", &sy, &ey, &x);
		H.push_back({x, sy, ey});
	}
	for(int i=1; i<=M; i++) // 가로선
	{
		int y, sx, ex;
		scanf("%d %d %d", &y, &sx, &ex);
		W.push_back({y, sx, ex});
	}

	// 좌표 압축
	for(int i=0; i<H.size(); i++)
	{
		X.push_back(H[i].x);
		Y.push_back(H[i].sy);
		Y.push_back(H[i].ey);
	}
	for(int i=0; i<W.size(); i++)
	{
		X.push_back(W[i].sx);
		X.push_back(W[i].ex);
		Y.push_back(W[i].y);
	}
	sort(Y.begin(), Y.end());
	sort(X.begin(), X.end());
	Y.erase(unique(Y.begin(), Y.end()), Y.end());
	X.erase(unique(X.begin(), X.end()), X.end());

	for(int i=0; i<Y.size(); i++)
		newY[Y[i]] = i;
	for(int i=0; i<X.size(); i++)
		newX[X[i]] = i;

	// 영역 넓이 전처리
	for(int i=0; i<(int)Y.size()-1; i++)
	{
		for(int j=0; j<(int)X.size()-1; j++)
		{
			area[i][j] = (ll)(Y[i+1]-Y[i])*(X[j+1]-X[j]);
			if (Y[i] <= 0 && 0 < Y[i+1] && X[j] <= 0 && 0 < X[j+1]) //
				SY = i, SX = j;
		}
	}

	// 컷 구하기
	for(int i=0; i<H.size(); i++)
	{
		int x = H[i].x, sy = H[i].sy, ey = H[i].ey;
		x = newX[x]; sy = newY[sy]; ey = newY[ey];
		for(int y=sy; y<ey; y++)
			cutX[y][x] = true;
	}
	for(int i=0; i<W.size(); i++)
	{
		int y = W[i].y, sx = W[i].sx, ex = W[i].ex;
		y = newY[y]; sx = newX[sx]; ex = newX[ex];
		for(int x=sx; x<ex; x++)
			cutY[y][x] = true;
	}

	// 상하좌우 밖으로 나갔을 때 예외 처리
	if (SY == -1 && SX == -1) return !printf("INF");
	// 도달 가능한 넓이 구하기
	szY = Y.size(); szX = X.size();
	vector<int>().swap(Y);
	vector<int>().swap(X);
	vector<Hline>().swap(H);
	vector<Wline>().swap(W);
	map<int, int>().swap(newY);
	map<int, int>().swap(newX);

	DFS(SY, SX);

	ll res = 0;
	for(int i=0; i<7070; i++)
		for(int j=0; j<7070; j++)
			if (vis[i][j])
				res += area[i][j];
	printf("%lld", res);
	return 0;
}