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: Time Limit Exceeded)

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

public class Hello
{
    struct L {
        public int a, b, c;
        public L(int a = 0, int b = 0, int c = 0) 
        {
            this.a = a;
            this.b = b;
            this.c = c;
        }   
    }
    readonly static int[] dx = new []{ -1, 0, 1, 0 };
    readonly static int[] dy = new []{ 0, -1, 0, 1 };

    public static void Main()
    {
        var N_M = Console.ReadLine().Trim().Split().Select(e=>int.Parse(e)).ToArray();
        var N = N_M[0];
        var M = N_M[1];        

        var mpX = new SortedDictionary<int, int>();
        var mpY = new SortedDictionary<int, int>();
        mpX[0] = 0;
        mpY[0] = 0;

        var lh = new List<L>();
        var lv = new List<L>();

        for(var i = 0; i < N; i++) {
            var A_B_C = Console.ReadLine().Trim().Split().Select(e=>int.Parse(e)).ToArray();
            var a = A_B_C[0];
            var b = A_B_C[1];
            var c = A_B_C[2];
            mpX[a] = 0;
            mpX[b] = 0;
            mpY[c] = 0;
            lh.Add(new L(a, b, c));
        }
       for(var i = 0; i < M; i++) {
            var A_B_C = Console.ReadLine().Trim().Split().Select(e=>int.Parse(e)).ToArray();
            var c = A_B_C[0];
            var a = A_B_C[1];
            var b = A_B_C[2];
            mpY[a] = 0;
            mpY[b] = 0;
            mpX[c] = 0;
            lv.Add(new L(a, b, c));
       }

        var xs = new List<int>();
        var ys = new List<int>();
        
        foreach(var id in mpX.Keys.ToArray())
        {           
            mpX[id] = xs.Count();
            xs.Add(id);
        }

        foreach(var id in mpY.Keys.ToArray())
        {           
            mpY[id] = ys.Count();
            ys.Add(id);
        }

        int h = xs.Count() * 2;
        int w = ys.Count() * 2;

        var d = new int[h, w];
        for(var i = 0; i < N; i++) {
            int a = mpX[lh[i].a]*2;
            int b = mpX[lh[i].b]*2;
            int c = mpY[lh[i].c]*2;
            for (var x = a; x <= b; ++x) d[x,c] = -1;
        }
        for(var i = 0; i < M; i++) {
            int a = mpY[lv[i].a]*2;
            int b = mpY[lv[i].b]*2;
            int c = mpX[lv[i].c]*2;
            for (var y = a; y <= b; ++y) d[c,y] = -1;
        }

        var q = new Queue<Tuple<int, int>>();
        int sx = mpX[0] * 2;
        int sy = mpY[0] * 2;
        d[sx, sy] = 1;
        q.Enqueue(Tuple.Create(sx, sy));
        while(q.Count() > 0)
        {
            var f = q.Dequeue();
            int x = f.Item1;
            int y = f.Item2;
            for(var v=0 ; v<4; v++)
            {
                int nx = x + dx[v];
                int ny = y + dy[v];
                if (nx < 0 || nx >= h) continue; 
                if (ny < 0 || ny >= w) continue; 
                if (d[nx, ny] != 0) continue; 
                d[nx, ny] = 1;
                q.Enqueue(Tuple.Create(nx, ny));
            }
        }

        long ans  = 0;
        for(var x = 0; x < h; x++) {
            for(var y = 0; y < w; y++) {
                if (d[x, y] != 1) continue;
                if ((x == 0) || (x == h-1) || (y == 0) || (y == w-1)) {
                    Console.WriteLine("INF");
                    return;
                }
                if ((x%2 == 0) || (y%2 == 0)) continue;
                long ex = xs[x/2+1]-xs[x/2];
                long ey = ys[y/2+1]-ys[y/2];
                ans += ex * ey;         
            }
        }
        Console.WriteLine(ans);
   }
}