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: Wrong Answer)

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

//using System.Numerics;
using CS=System.Runtime.CompilerServices;
//using System.Diagnostics;
using static System.Math;
using ll=System.Int64;
using static Contest_F.Lib_IO;
using static Contest_F.Lib_Minifunc;


namespace Contest_F {

public static class Entry
{

	public static void Main() {
		checked{
			long n, m;
			Lib_IO.rm(out n, out m);
			P<int,int,int>[] abc,def;
			rl(n,out abc);
			rl(m,out def);

			MatMD<bool> wall;
			int[] rxi,ryi;
			P<int,int> range, start;
			{
				int[] xi = new int[2*n+m];
				int[] yi = new int[2*m+n];
				var dlx = new Dictionary<int, List<P<int, int>>>();
				var dly = new Dictionary<int, List<P<int, int>>>();
				for (int i = 0; i < n; i++)
				{
					xi[2*i] = abc[i].A;
					xi[2*i+1] = abc[i].B;
					yi[2*m+i] = abc[i].C;
					if(!dlx.ContainsKey(abc[i].C)) dlx[abc[i].C]=new List<P<int, int>>();
					dlx[abc[i].C].Add(new P<int, int>(abc[i].A,abc[i].B));
				}
				for (int i = 0; i < m; i++)
				{
					yi[2*i] = def[i].B;
					yi[2*i+1] = def[i].C;
					xi[2*n+i] = def[i].A;
					if(!dly.ContainsKey(def[i].A)) dly[def[i].A]=new List<P<int, int>>();
					dly[def[i].A].Add(new P<int, int>(def[i].B,def[i].C));
				}
				xi.sort(1);
				yi.sort(1);
				var dxi = new Dictionary<int, int>();
				var dyi = new Dictionary<int, int>();
				int kx = 0;
				int sx = 0;
				for(int i =0;i<xi.Length;i++)
				{
					var e = xi[i];
					if(0<i&&xi[i-1]<=0&&0<e) sx = i;
					if(!dxi.ContainsKey(e)) dxi[e]=kx++;
				}
				rxi = new int[kx];
				foreach (var e in dxi) rxi[e.Value] = e.Key;
				int ky = 0;
				int sy = 0;
				for(int i =0;i<yi.Length;i++)
				{
					var e = yi[i];
					if(0<i&&yi[i-1]<=0&&0<e) sy = i;
					if(!dyi.ContainsKey(e)) dyi[e]=ky++;
				}
				ryi = new int[ky];
				foreach (var e in dyi) ryi[e.Value] = e.Key;
				range = new P<int, int>(kx,ky);
				start = new P<int, int>(sx,sy);

				wall = new MatMD<bool>(range.A,range.B,2,false);
				foreach (var e in dxi.Keys)
				{
					if(!dly.ContainsKey(e)) continue;
					foreach (var w in dly[e]) {
						for (int i = dyi[w.A]+1; i <= dyi[w.B]; i++) wall[dxi[e],i,1] = true;
					}
				}
				foreach (var e in dyi.Keys)
				{
					if(!dlx.ContainsKey(e)) continue;
					foreach (var w in dlx[e]) {
						for (int i = dxi[w.A]+1; i <= dxi[w.B]; i++) wall[i,dyi[e],0] = true;
					}
				}
			}

			Func<int,int,ll> bfs = (sx,sy)=>{
				var que = new Queue<P<int, int,int>>();
				que.Enqueue(new P<int, int,int>(sx,sy,0));
				ll sum = 0;
				bool[][] visit = inita(range.A,range.B,false);
				while(0<que.Count){
					var q = que.Dequeue();
					if(q.A<=0||q.A>=range.A||q.B<=0||q.B>=range.B) return -1;
					if(visit[q.A][q.B]) continue;
					visit[q.A][q.B] = true;
					sum += (ll)(rxi[q.A]-rxi[q.A-1])*(ll)(ryi[q.B]-ryi[q.B-1]);
					if(q.C!=2&&!wall[q.A-1,q.B,1]) que.Enqueue(new P<int, int,int>(q.A-1,q.B,4));
					if(q.C!=4&&!wall[q.A,q.B,1]) que.Enqueue(new P<int, int,int>(q.A+1,q.B,2));
					if(q.C!=1&&!wall[q.A,q.B-1,0]) que.Enqueue(new P<int, int,int>(q.A,q.B-1,3));
					if(q.C!=3&&!wall[q.A,q.B,0]) que.Enqueue(new P<int, int,int>(q.A,q.B+1,1));
				}
				return sum;
			};

			long ans = bfs(start.A,start.B);

			Lib_IO.wm(ans==-1?"INF":ans.ToString());
		}
	}

public class MatMD<T> where T : IComparable {
	public readonly int Dimension;
	public readonly int Length1,Length2,Length3,Length4,Length5,Length6,Length7,Length8;
	T[] data;
	public MatMD(long l1, long l2, T ival){
		Dimension = 2;
		Length1 = (int)l1; Length2 = (int)l2;
		data = Enumerable.Repeat(ival,(int)(l1*l2)).ToArray();
	}
	public MatMD(long l1, long l2, long l3, T ival){
		Dimension = 3;
		Length1 = (int)l1; Length2 = (int)l2; Length3 = (int)l3;
		data = Enumerable.Repeat(ival,(int)(l1*l2*l3)).ToArray();
	}
	public MatMD(long l1, long l2, long l3, long l4, T ival){
		Dimension = 4;
		Length1 = (int)l1; Length2 = (int)l2; Length3 = (int)l3; Length4 = (int)l4;
		data = Enumerable.Repeat(ival,(int)(l1*l2*l3*l4)).ToArray();
	}
	public T this[int i1, int i2]{
		get { return data[i1*Length2+i2]; }
		set { data[i1*Length2+i2] = value; }
	}
	public T this[int i1, int i2, int i3]{
		get { return data[(i1*Length2+i2)*Length3+i3]; }
		set { data[(i1*Length2+i2)*Length3+i3] = value; }
	}
	public T this[int i1, int i2, int i3, int i4]{
		get { return data[((i1*Length2+i2)*Length3+i3)*Length4+i4]; }
		set { data[((i1*Length2+i2)*Length3+i3)*Length4+i4] = value; }
	}
	[CS.MethodImpl(256)] public bool Chmin(int i1, int i2, T v){ bool r; if (r = (this[i1,i2].CompareTo(v)== 1)) this[i1,i2] = v; return r; }
	[CS.MethodImpl(256)] public bool Chmax(int i1, int i2, T v){ bool r; if (r = (this[i1,i2].CompareTo(v)==-1)) this[i1,i2] = v; return r; }
	[CS.MethodImpl(256)] public bool Chmin(int i1, int i2, int i3, T v){ bool r; if (r = (this[i1,i2,i3].CompareTo(v)== 1)) this[i1,i2,i3] = v; return r; }
	[CS.MethodImpl(256)] public bool Chmax(int i1, int i2, int i3, T v){ bool r; if (r = (this[i1,i2,i3].CompareTo(v)==-1)) this[i1,i2,i3] = v; return r; }
	[CS.MethodImpl(256)] public bool Chmin(int i1, int i2, int i3, int i4, T v){ bool r; if (r = (this[i1,i2,i3,i4].CompareTo(v)== 1)) this[i1,i2,i3,i4] = v; return r; }
	[CS.MethodImpl(256)] public bool Chmax(int i1, int i2, int i3, int i4, T v){ bool r; if (r = (this[i1,i2,i3,i4].CompareTo(v)==-1)) this[i1,i2,i3,i4] = v; return r; }
}










}



#region BaseModule


public static class Lib_Minifunc{
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;
	public const long INF1 = 1000000000000000;  // 10^15
	[CS.MethodImpl(256)] public static void swap1<T>(ref T a,ref T b) { T t = a; a = b; b = t; }
	[CS.MethodImpl(256)] public static void swap2<T>(ref T a,ref T b) where T : IComparable { if (a.CompareTo(b)==1) swap1(ref a, ref b); }  // b の方が小さければ交換

	[CS.MethodImpl(256)] public static bool chmin<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)== 1)) a = b; return r; }  // b の方が小さければ a を更新
	[CS.MethodImpl(256)] public static bool chmax<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)==-1)) a = b; return r; }  // b の方が大きければ a を更新
	[CS.MethodImpl(256)] public static bool inside(long i,long n) => (0<=i&&i<n);
	[CS.MethodImpl(256)] public static bool inside(long x,long y,long w,long h) => (inside(x,w)&&inside(y,h));
	[CS.MethodImpl(256)] public static T min<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmin(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static T max<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmax(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static long mod(long a,long m=MOD1) { var v = a%m; return (v<0?(m<0?v-m:v+m):v); }
	[CS.MethodImpl(256)] public static long ceil(long a,long b) => (a%b==0||(a<0&&0<b)||(0<a&&b<0)?a/b:a/b+1);	// 整数商の切り上げ
	[CS.MethodImpl(256)] public static long round(double a) => Convert.ToInt64(a);	// 四捨五入
	public static string YES(bool a) => a?"YES":"NO";
	public static string yes(bool a) => a?"Yes":"No";
	[CS.MethodImpl(256)] public static P<T,U> initp<T,U>(T a,U b) => new P<T,U>(a,b);
	[CS.MethodImpl(256)] public static P<T,U,V> initp<T,U,V>(T a,U b,V c) => new P<T,U,V>(a,b,c);
	[CS.MethodImpl(256)] public static P<T,U,V,W> initp<T,U,V,W>(T a,U b,V c,W d) => new P<T,U,V,W>(a,b,c,d);
	public static T[] initc<T>(long l1) where T : new() { var m = new T[l1]; for (int i=0;i<m.Length;i++) m[i] = new T(); return m; }	// new制約は遅い
	public static int[] range(long s, long c) => Enumerable.Range((int)s,(int)c).ToArray();
	[CS.MethodImpl(256)] public static T[] inita<T>(long l1,T v=default(T)) => Enumerable.Repeat(v,(int)l1).ToArray();
	public static T[][] inita<T>(long l1,long l2,T v=default(T)) { var m = new T[l1][]; for (int i=0;i<m.Length;i++) m[i] = inita(l2,v); return m; }
	public static void shift(this IList<int> l,int o) { for (int i = 0;i<l.Count;i++) l[i]+=o; }
	public static void shift(this IList<long> l,long o) { for (int i = 0;i<l.Count;i++) l[i]+=o; }
	[CS.MethodImpl(256)] public static bool initd<T,U>(this IDictionary<T,U> d, T k, U v=default(U)) { if(d.ContainsKey(k)) { return false; } else { d[k] = v; return true; } }
	public static void sort<T>(this T[] a,int o=1, Comparison<T> f=null) where T : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.CompareTo(y));break;
			case -1: Array.Sort(a,(x,y)=>y.CompareTo(x));break;
			default: break;
		}
	}
	public static void sort<T,U>(this P<T,U>[] a,int o, Comparison<P<T,U>> f=null) where T : IComparable where U : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			default: break;
		}
	}
	public static void sort<T,U,V>(this P<T,U,V>[] a,int o, Comparison<P<T,U,V>> f=null) where T : IComparable where U : IComparable where V : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			default: break;
		}
	}
	public static void sort<T,U,V,W>(this P<T,U,V,W>[] a,int o, Comparison<P<T,U,V,W>> f=null) where T : IComparable where U : IComparable where V : IComparable where W : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			case  4: Array.Sort(a,(x,y)=>x.D.CompareTo(y.D));break;
			case -4: Array.Sort(a,(x,y)=>y.D.CompareTo(x.D));break;
			default: break;
		}
	}
}


public struct P<T,U>
{
	public T A; public U B;
	public P(T a,U b) { A = a; B = b; }
	public static implicit operator KeyValuePair<T,U>(P<T,U> a) => new KeyValuePair<T,U>(a.A,a.B);
	public static implicit operator P<T,U>(KeyValuePair<T,U> a) => new P<T,U>(a.Key,a.Value);
}
public struct P<T,U,V>
{
	public T A; public U B; public V C;
	public P(T a,U b,V c) { A = a; B = b; C = c; }
}
public struct P<T,U,V,W>
{
	public T A; public U B; public V C; public W D;
	public P(T a,U b,V c,W d) { A = a; B = b; C = c; D = d; }
}

public static class Lib_IO
{
	class Prt : System.IO.StreamWriter
	{
		public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
		public Prt(System.IO.Stream s) : base(s,new UTF8Encoding(false,true)) {}
		public Prt(System.IO.Stream s,Encoding e) : base(s,e) {}
	}
	static Prt sw = new Prt(Console.OpenStandardOutput());
	static char[] sp = new char[] {' '};
	[CS.MethodImpl(256)] static bool eq<T,U>() => typeof(T).Equals(typeof(U));
	[CS.MethodImpl(256)] static T ct<T,U>(U a) => (T)Convert.ChangeType(a,typeof(T));
	[CS.MethodImpl(256)] static T cv<T>(string s) =>
		eq<T,int>()	? ct<T,int>(int.Parse(s))
	  : eq<T,long>()   ? ct<T,long>(long.Parse(s))
	  : eq<T,double>() ? ct<T,double>(double.Parse(s,System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T,char>()   ? ct<T,char>(s[0])
					   : ct<T,string>(s);
	public static string[] rm<T>(out T a) {
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(z[0]);
		return z;
	}
	public static string[] rm<T,U>(out T a,out U b) {
		var z = rm<T>(out a); 
		b = cv<U>(z[1]);
		return z;
	}
	public static string[] rm<T,U,V>(out T a,out U b,out V c) {
		var z = rm<T,U>(out a,out b); 
		c = cv<V>(z[2]);
		return z;
	}
	public static string[] rm<T,U,V,W>(out T a,out U b,out V c,out W d) {
		var z = rm<T,U,V>(out a,out b,out c); 
		d = cv<W>(z[3]);
		return z;
	}
	public static string[] rm<T,U,V,W,X>(out T a,out U b,out V c,out W d,out X e) {
		var z = rm<T,U,V,W>(out a,out b,out c,out d); 
		e = cv<X>(z[4]);
		return z;
	}
	public static string[] rm<T,U,V,W,X,Y>(out T a,out U b,out V c,out W d,out X e,out Y f) {
		var z = rm<T,U,V,W,X>(out a,out b,out c,out d,out e); 
		f = cv<Y>(z[5]);
		return z;
	}
	public static string[] ra<T>(out T[] a) {		
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries);
		a = new T[z.Length];
		for(int i=0;i<a.Length;i++) a[i] = cv<T>(z[i]);
		return z;
	}
	public static string[][] rl<T>(long n,out T[] a) {
		a = new T[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = rm(out a[i]);
		return y;
	}
	public static string[][] rl<T,U>(long n,out P<T,U>[] a) {
		a = new P<T,U>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p;
			y[i] = rm(out o,out p);
			a[i] = new P<T,U>(o,p);
		}
		return y;
	}
	public static string[][] rl<T,U,V>(long n,out P<T,U,V>[] a) {
		a = new P<T,U,V>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q;
			y[i] = rm(out o,out p,out q);
			a[i] = new P<T,U,V>(o,p,q);
		}
		return y;
	}
	public static string[][] rl<T,U,V,W>(long n,out P<T,U,V,W>[] a) {
		a = new P<T,U,V,W>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q; W r;
			y[i] = rm(out o,out p,out q,out r);
			a[i] = new P<T,U,V,W>(o,p,q,r);
		}
		return y;
	}
	public static string[][] rx<T>(long n,out T[][] a) {
		a = new T[n][];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = ra(out a[i]);
		return y;
	}

	static void wwp(Action act){
		sw.AutoFlush = false;
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	[CS.MethodImpl(256)] static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void wm(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length-1;i++) Console.Write(wfm(a[i].GetType())+" ",a[i]);
			Console.WriteLine(wfm(a[a.Length-1].GetType()),a[a.Length-1]);
		});
	}
	public static void wa<T>(IList<T> a) { wxa(new IList<T>[]{a}, " "); }
	public static void wl<T>(IList<T> a) {
		wwp(()=>{
			var f = wfm(typeof(T));
			if(a.Count==0){
				Console.WriteLine();
			}else{
				foreach(T x in a) Console.WriteLine(f,x);
			}
		});
	}
	static void wxa<T>(IList<IList<T>> a, string s) {
		wwp(()=>{
			var f = wfm(typeof(T));
			var g = f + s;
			foreach(var b in a) {
				if(b.Count==0){
					Console.WriteLine();
				}else{
					for(int i=0;i<b.Count-1;i++) Console.Write(g,b[i]);
					Console.WriteLine(f,b[b.Count-1]);
				}
			}
		});
	}
	public static void wx<T>(IList<IList<T>> a) { wxa(a, " "); }
	public static void wg<T>(IList<IList<T>> a) { wxa(a, ""); }
}

#endregion



}