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;
using System.Runtime.CompilerServices;
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 h=new int[s[0]+s[1]][];
		for(int i = 0;i<s[0];i++) {
			var e=sc.Ia;
			h[i]=new int[]{e[0],e[2],e[1],e[2]};
		}
		for(int i = 0;i<s[1];i++) {
			var e=sc.Ia;
			h[i+s[0]]=new int[]{e[0],e[1],e[0],e[2]};
		}
		var z=new Cc2(h);
		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*2+1][];
		for(int i = 0;i<=z.my*2;i++) {
			b[i]=new int[z.mx*2+1];
		}
		var ly=new int[z.my*2];
		var lx=new int[z.mx*2];
		for(int i = 0;i<h.Length;i++) {
			int y1=z.Qy(h[i][0])*2,x1=z.Qx(h[i][1])*2;
			int y2=z.Qy(h[i][2])*2+1,x2=z.Qx(h[i][3])*2+1;
			b[y1][x1]+=1;
			b[y1][x2]+=-1;
			b[y2][x1]+=-1;
			b[y2][x2]+=1;
			ly[y1]=h[i][0];
			ly[y2-1]=h[i][2];
			lx[x1]=h[i][1];
			lx[x2-1]=h[i][3];
		}
		for(int i = 1;i<ly.Length-1;i+=2) {
			ly[i]=ly[i+1]-ly[i-1];
			ly[i-1]=0;
		}
		for(int i = 1;i<lx.Length-1;i+=2) {
			lx[i]=lx[i+1]-lx[i-1];
			lx[i-1]=0;
		}
		for(int i = 0;i<z.my*2;i++) {
			for(int j = 0;j<z.mx*2;j++) {b[i][j+1]+=b[i][j];}
		}
		for(int j = 0;j<z.mx*2;j++) {
			for(int i = 0;i<z.my*2;i++) {b[i+1][j]+=b[i][j];}
		}
		var bo=true;
		long ans=0;
		int ty=z.Qy(0)*2,tx=z.Qx(0)*2;
		if(b[ty][tx]!=0){ty++;tx++;}
		Fu(ty,tx);
		void Fu(int y,int x) {
			b[y][x]=-1;
			if(y==0||x==0||y==z.my*2-1||x==z.mx*2-1){
				bo=false;
				return;
			}
			ans+=(long)ly[x]*lx[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(bo?ans.ToString():"INF");
	}
}
public class Cc2{
	public int my,mx;
	public nint[] ya,xa,yc,xc;
	public int[] yb,xb;
	public Cc2(int[][] a){
		int n=a.Length<<1;
		ya=new nint[n];
		xa=new nint[n];
		for(int i = 0;i<a.Length;i++) {
			ya[i<<1]=a[i][0]<<1;
			xa[i<<1]=a[i][1]<<1;
			ya[(i<<1)+1]=(a[i][2]<<1);
			xa[(i<<1)+1]=(a[i][3]<<1);
		}
		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++) {
			nint pd=ya[i-1]>>1,nd=ya[i]>>1;
			int pb=(int)(ya[i-1]&1),nb=(int)(ya[i]&1);
			yb[i]=yb[i-1];
			if(nd!=pd&&(pb==1||nb==0)){
				if(nd==pd+1||pb==0||nb==1){
					yc[yb[i]+nb]=Max(yc[yb[i]+nb],nd-pd);
					yb[i]++;
				}
				else{
					yb[i]+=2;
					yc[yb[i]-1]=nd-pd-1;
				}
			}
			else if(nd!=pd){yc[yb[i]]=nd-pd+1;}
			else if(pb==0&&nb==1){yc[yb[i]]=1;}
			pd=xa[i-1]>>1;pb=(int)(xa[i-1]&1);nd=xa[i]>>1;nb=(int)(xa[i]&1);
			xb[i]=xb[i-1];
			if(nd!=pd&&(pb==1||nb==0)){
				if(nd==pd+1||pb==0||nb==1){
					xc[xb[i]+nb]=Max(xc[xb[i]+nb],nd-pd);
					xb[i]++;
				}
				else{
					xb[i]+=2;
					xc[xb[i]-1]=nd-pd-1;
				}
			}
			else if(nd!=pd){xc[xb[i]]=nd-pd+1;}
			else if(pb==0&&nb==1){xc[xb[i]]=1;}
		}
		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]>>1==y){return yb[mid];}
			if(ya[mid]>>1>y){ub=mid;}
			else{lb=mid;}
		}
		return yb[lb]+(int)(ya[lb]&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]>>1==x){return xb[mid];}
			if(xa[mid]>>1>x){ub=mid;}
			else{lb=mid;}
		}
		return xb[lb]+(int)(xa[lb]&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(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).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;}
}