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: Runtime Error)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using nint=System.Int32;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var c=sc.Arr(s[0],()=>sc.Ia3(""," 0 0 0"));
		var f=new int[s[0]][];
		var d=sc.Arr(s[1],()=>sc.Ia3(""," 0 0 0"));
		var g=new int[s[1]][];
		c=c.OrderBy(x=>x[2]).ThenBy(x=>x[0]).ToArray();
		d=d.OrderBy(x=>x[0]).ThenBy(x=>x[1]).ToArray();
		int m1=s[0],m2=s[1];
		for(int i = 0,k=0;i<s[0];i++) {
			if(c[i][5]==-1){continue;}
			for(int j = i+1;j<s[0]&&c[i][2]==c[j][2];j++) {
				if(c[i][1]>=c[j][0]){
					c[i][1]=Max(c[i][1],c[j][1]);
					c[j][5]=-1;
					m1--;
				}
			}
			f[k]=c[i];
			k++;
			c[i][3]=c[i][1];
			c[i][4]=c[i][0];
		}
		for(int i = 0,k=0;i<s[1];i++) {
			if(d[i][5]==-1){continue;}
			for(int j = i+1;j<s[1]&&d[i][0]==d[j][0];j++) {
				if(d[i][2]>=d[j][1]){
					d[i][2]=Max(d[i][2],d[j][1]);
					d[j][5]=-1;
					m2--;
				}
			}
			g[k]=d[i];
			k++;
			d[i][3]=d[i][2];
			d[i][4]=d[i][1];
			for(int j = 0;j<m1;j++) {
				if(f[j][0]<=d[i][0]&&d[i][0]<=f[j][1]&&d[i][1]<=f[j][2]&&f[j][2]<=d[i][2]){
					f[j][5]=d[i][5]=1;
					f[j][3]=Min(f[j][3],d[i][0]);
					f[j][4]=Max(f[j][4],d[i][0]);
					d[i][3]=Min(d[i][3],f[j][2]);
					d[i][4]=Max(d[i][4],f[j][2]);
				}
			}
		}
		var li=new List<int[]>();
		for(int i = 0;i<m1;i++) {
			if(f[i][5]==1){li.Add(new int[]{f[i][3],f[i][2],f[i][4],f[i][2]});}
		}
		for(int i = 0;i<m2;i++) {
			if(g[i][5]==1){li.Add(new int[]{g[i][0],g[i][3],g[i][0],g[i][4]});}
		}
		var z=new Cc2(li);
		if(z.ya[0]>0||z.ya[z.ya.Length-1]<0||z.xa[0]>0||z.xa[z.xa.Length-1]<0){
			Console.WriteLine("INF");
			return;
		}
		var b=new int[z.my+1][];
		for(int i = 0;i<=z.my;i++) {b[i]=new int[z.mx+1];}
		for(int i = 0;i<li.Count;i++) {
			int y1=z.Qy(li[i][0]),x1=z.Qx(li[i][1]);
			int y2=z.Qy(li[i][2])+1,x2=z.Qx(li[i][3])+1;
			b[y1][x1]+=1;
			b[y1][x2]+=-1;
			b[y2][x1]+=-1;
			b[y2][x2]+=1;
		}
		for(int i = 0;i<z.my;i++) {
			for(int j = 0;j<z.mx;j++) {b[i][j+1]+=b[i][j];}
		}
		for(int j = 0;j<z.mx;j++) {
			for(int i = 0;i<z.my;i++) {b[i+1][j]+=b[i][j];}
		}
		var bo=true;
		long ans=0;
		Fu(z.Qy(0),z.Qx(0));
		void Fu(int y,int x) {
			b[y][x]=-1;
			if(y==0||x==0||y==z.my-1||x==z.mx-1){
				bo=false;
				return;
			}
			ans+=z.Ar(x,y);
			if(bo&&b[y-1][x]==0){Fu(y-1,x);}
			if(bo&&b[y][x-1]==0){Fu(y,x-1);}
			if(bo&&b[y+1][x]==0){Fu(y+1,x);}
			if(bo&&b[y][x+1]==0){Fu(y,x+1);}
		}
		Console.WriteLine(ans);
	}
}
public class Cc2{
	public int my,mx;
	public nint[] ya,xa,yc,xc;
	public int[] yb,xb;
	public Cc2(List<int[]> a){
		int n=a.Count<<1;
		ya=new nint[n];
		xa=new nint[n];
		for(int i = 0;i<a.Count;i++) {
			ya[i<<1]=a[i][0];
			xa[i<<1]=a[i][1];
			ya[(i<<1)+1]=a[i][2];
			xa[(i<<1)+1]=a[i][3];
		}
		Array.Sort(ya);
		Array.Sort(xa);
		yb=new int[n];
		xb=new int[n];
		yc=new nint[n<<1];
		xc=new nint[n<<1];
		for(int i = 1;i<n;i++) {
			yb[i]=yb[i-1];
			if(ya[i]!=ya[i-1]){
				yc[yb[i]+1]=ya[i]-ya[i-1];
				yb[i]+=2;
			}
			xb[i]=xb[i-1];
			if(xa[i]!=xa[i-1]){
				xc[xb[i]+1]=xa[i]-xa[i-1];
				xb[i]+=2;
			}
		}
		my=yb[n-1]+1;
		mx=xb[n-1]+1;
	}
	public int Qy(long y){
		int lb=-1,ub=ya.Length,mid=0;
		while(lb+1!=ub){
			mid=(ub+lb)/2;
			if(ya[mid]==y){return yb[mid];}
			if(ya[mid]>y){ub=mid;}
			else{lb=mid;}
		}
		return yb[ub]-1;
	}
	public int Qx(long x){
		int lb=-1,ub=xa.Length,mid=0;
		while(lb+1!=ub){
			mid=(ub+lb)/2;
			if(xa[mid]==x){return xb[mid];}
			if(xa[mid]>x){ub=mid;}
			else{lb=mid;}
		}
		return xb[ub]-1;
	}
	public long Ar(int y,int x){return (long)yc[y]*xc[x];}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}