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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using CompLib.Util;

public class Program
{
    int N, M;
    long[] A, B, C;
    long[] D, E, F;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        M = sc.NextInt();
        A = new long[N];
        B = new long[N];
        C = new long[N];
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.NextInt();
            B[i] = sc.NextInt();
            C[i] = sc.NextInt();
        }

        D = new long[M];
        E = new long[M];
        F = new long[M];
        for (int i = 0; i < M; i++)
        {
            D[i] = sc.NextInt();
            E[i] = sc.NextInt();
            F[i] = sc.NextInt();
        }

        // 座圧
        var xHs = new HashSet<long>();
        var yHs = new HashSet<long>();
        xHs.Add(-1500000001);
        xHs.Add(1500000001);
        yHs.Add(-1500000001);
        yHs.Add(1500000001);
        for (int i = 0; i < N; i++)
        {
            xHs.Add(A[i]);
            xHs.Add(B[i]);
            yHs.Add(C[i]);
        }
        for (int i = 0; i < M; i++)
        {
            xHs.Add(D[i]);
            yHs.Add(E[i]);
            yHs.Add(F[i]);
        }

        var xAr = xHs.ToArray();
        Array.Sort(xAr);
        var yAr = yHs.ToArray();
        Array.Sort(yAr);
        var xMap = new Dictionary<long, int>();
        var yMap = new Dictionary<long, int>();
        for (int i = 0; i < xAr.Length; i++)
        {
            xMap[xAr[i]] = i;
        }
        for (int i = 0; i < yAr.Length; i++)
        {
            yMap[yAr[i]] = i;
        }

        // グリッドの面積
        var grid = new long[xAr.Length * 2 - 1, yAr.Length * 2 - 1];
        // i, jが通れ無いか?
        var f = new bool[xAr.Length * 2 - 1, yAr.Length * 2 - 1];

        for (int i = 0; i < xAr.Length - 1; i++)
        {
            for (int j = 0; j < yAr.Length - 1; j++)
            {
                grid[i * 2 + 1, j * 2 + 1] = (xAr[i + 1] - xAr[i]) * (yAr[j + 1] - yAr[j]);
            }
        }

        for (int i = 0; i < N; i++)
        {
            int l = xMap[A[i]];
            int r = xMap[B[i]];

            int y = yMap[C[i]];
            for (int j = 2 * l; j <= 2 * r; j++)
            {
                f[j, y * 2] = true;
            }
        }
        for (int i = 0; i < N; i++)
        {
            int l = xMap[A[i]];
            int r = xMap[B[i]];

            int y = yMap[C[i]];
            for (int j = 2 * l; j <= 2 * r; j++)
            {
                f[j, y * 2] = true;
            }
        }
        for (int i = 0; i < M; i++)
        {
            int l = yMap[E[i]];
            int r = yMap[F[i]];

            int x = xMap[D[i]];
            for (int j = 2 * l; j <= 2 * r; j++)
            {
                f[x * 2, j] = true;
            }
        }
        int sx = -1;
        int sy = -1;
        for (int i = 0; i < xAr.Length - 1; i++)
        {
            for (int j = 0; j < yAr.Length - 1; j++)
            {
                if (xAr[i] <= 0 && 0 <= xAr[i + 1] && yAr[j] <= 0 && 0 <= yAr[j + 1])
                {
                    sx = i * 2 + 1;
                    sy = j * 2 + 1;
                    break;
                }
            }
            if (sx != -1) break;
        }
        // Console.WriteLine($"{sx} {sy}");
        var dx = new int[] { 1, 0, -1, 0 };
        var dy = new int[] { 0, 1, 0, -1 };

        var xq = new Queue<int>();
        var yq = new Queue<int>();
        var flag = new bool[xAr.Length * 2 - 1, yAr.Length * 2 - 1];
        xq.Enqueue(sx);
        yq.Enqueue(sy);
        flag[sx, sy] = true;

        while (xq.Count > 0)
        {
            var xx = xq.Dequeue();
            var yy = yq.Dequeue();
            for (int i = 0; i < 4; i++)
            {
                int nx = xx + dx[i];
                int ny = yy + dy[i];
                if (nx < 0 || nx >= xAr.Length * 2 - 1) continue;
                if (ny < 0 || ny >= yAr.Length * 2 - 1) continue;
                if (f[nx, ny]) continue;
                if (flag[nx, ny]) continue;
                flag[nx, ny] = true;
                xq.Enqueue(nx);
                yq.Enqueue(ny);
            }
        }

        long ans = 0;
        for (int i = 0; i < xAr.Length * 2 - 1; i++)
        {
            for (int j = 0; j < yAr.Length * 2 - 1; j++)
            {
                if ((i == 0 || j == 0 || i == xAr.Length * 2 - 2 || j == yAr.Length * 2 - 2) && flag[i, j])
                {
                    // Console.WriteLine($"{i} {j}");
                    Console.WriteLine("INF");
                    return;
                }
                if (flag[i, j])
                {
                    // Console.WriteLine($"{i} {j} {grid[i, j]}");
                    ans += grid[i, j];
                }
            }
        }
        Console.WriteLine(ans);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}