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# (Mono-mcs 6.8.0.105) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ABC168F
{
    class Program
    {
        static void Main(string[] args)
        {
            var NM = ReadInt();
            int N = NM[0];
            int M = NM[1];

            var A = new List<int>();//x
            var B = new List<int>();//x
            var C = new List<int>();//y
            var D = new List<int>();//x
            var E = new List<int>();//y
            var F = new List<int>();//y
            for (int i = 0; i < N; i++)
            {
                var input = ReadInt();
                A.Add(input[0]);
                B.Add(input[1]);
                C.Add(input[2]);
            }
            for (int i = 0; i < M; i++)
            {
                var input = ReadInt();
                D.Add(input[0]);
                E.Add(input[1]);
                F.Add(input[2]);
            }

            //座標圧縮
            List<int> xList = new List<int>();
            List<int> yList = new List<int>();
            xList.AddRange(A);
            xList.AddRange(B);
            xList.AddRange(D);
            xList.Add(0);
            yList.AddRange(C);
            yList.AddRange(E);
            yList.AddRange(F);
            yList.Add(0);       //牛原点

            var toValueX = xList.Distinct().OrderBy(x => x).ToArray();
            var toValueY = yList.Distinct().OrderBy(x => x).ToArray();

            var indexFromValueTableX = toValueX.Select((value, index) => new { value, index }).ToDictionary(p => p.value, p => p.index);
            var indexFromValueTableY = toValueY.Select((value, index) => new { value, index }).ToDictionary(p => p.value, p => p.index);

            int countX = indexFromValueTableX.Count;
            int countY = indexFromValueTableY.Count;

            A = A.Select(v => indexFromValueTableX[v]).ToList();
            B = B.Select(v => indexFromValueTableX[v]).ToList();
            D = D.Select(v => indexFromValueTableX[v]).ToList();
            C = C.Select(v => indexFromValueTableY[v]).ToList();
            E = E.Select(v => indexFromValueTableY[v]).ToList();
            F = F.Select(v => indexFromValueTableY[v]).ToList();

            //
            int[,] map = new int[2 * countX - 1, 2 * countY - 1];
            for (int i = 0; i < N; i++)
            {
                for (int x = 2 * A[i]; x <= 2 * B[i]; x++)
                {
                    map[x, 2 * C[i]] = 1;
                }
            }
            for (int i = 0; i < M; i++)
            {
                for (int y = 2 * E[i]; y <= 2 * F[i]; y++)
                {
                    map[2 * D[i], y] = 1;
                }
            }
            map[2 * indexFromValueTableX[0], 2 * indexFromValueTableY[0]] = 2;

            //DrawMapforDebug(map);

            //dfs
            long ans = 0;
            int[] dx = new int[] { -1, 0, 1, 0 };
            int[] dy = new int[] { 0, 1, 0, -1 };

            //接続部をすべて塗る
            Queue<int> queX = new Queue<int>();
            Queue<int> queY = new Queue<int>();
            queX.Enqueue(2 * indexFromValueTableX[0]);
            queY.Enqueue(2 * indexFromValueTableY[0]);

            while (queX.Count > 0)
            {
                DrawMapforDebug(map);

                int px = queX.Dequeue();
                int py = queY.Dequeue();
                for (int k = 0; k < 4; k++)
                {
                    int tx = px + dx[k];
                    int ty = py + dy[k];
                    if (tx < 0 || 2 * countX - 1 <= tx || ty < 0 || 2 * countY - 1 <= ty)
                    {
                        ans = -1;
                        continue;
                    }
                    if (map[tx, ty] > 0) continue;

                    long cand = 0;
                    if (tx % 2 == 1 && ty % 2 == 1) cand = (long)(toValueX[tx / 2 + 1] - toValueX[tx / 2]) * (toValueY[ty / 2 + 1] - toValueY[ty / 2]);
                    ans += cand;
                    queX.Enqueue(tx);
                    queY.Enqueue(ty);
                    map[tx, ty] = 2;
                }
            }

            if (ans != -1) Console.WriteLine(ans);
            else Console.WriteLine("INF");
            Console.ReadKey();
        }

        /// <summary>
        /// 二次元配列の可視化メソッド
        /// </summary>
        /// <param name="map"></param>
        static void DrawMapforDebug(int[,] map)
        {
            int W = map.GetLength(0);
            int H = map.GetLength(1);

            int[,] map2 = new int[W + 1, H + 1];
            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    if (i == 0 && j == 0) map[i, j] = 0;
                    else if (i == 0) map2[i, j] = j - 1;
                    else if (j == 0) map2[i, j] = i - 1;
                    else map2[i, j] = map[i - 1, j - 1];
                }
            }

            for (int i = 0; i < W + 1; i++)
            {
                for (int j = 0; j < H + 1; j++)
                {
                    Console.Write(map2[i, j] % 10);
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}