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 (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 1010
#define MAX2 (MAX*2)
#define MAX3 (MAX*3)
#define INF 1e9

#define DOWN  1
#define UP    2
#define RIGHT 4
#define LEFT  8

int n,m;
int a[MAX][2], b[MAX][2], c[MAX][2];
int d[MAX][2], e[MAX][2], f[MAX][2];
long long x[MAX3],y[MAX3];
int xcnt,ycnt;
int result[MAX2][MAX2];

void printDir( int x, int y, int d )
{
    printf( "(%d %d) => %d", x, y, d );
    if( d & UP ) printf( " UP" );
    if( d & DOWN ) printf( " DOWN" );
    if( d & LEFT ) printf( " LEFT" );
    if( d & RIGHT ) printf( " RIGHT" );
    printf( "\n" );
}

// ソート用比較関数
int cmpfunc( const void *a, const void *b )
{
    return( *((long long *) a) - *((long long *) b) );
}

// ソートして重複を削除
int sort( long long d[],int n )
{
    int c=0;

    qsort( d, n, sizeof( long long ), cmpfunc );

    for( int i=1; i<n; i++ ) {
        if( d[i] != d[c]) {
            d[++c] = d[i];
        }
    }

    return( c+1 );
}

// 検索
int srch( int a, long long d[], int n )
{
    int l = 0;
    int r = n-1;

    while( r - l > 1 ) {
        int c = (l + r) / 2;
        if( a < d[c] ) r = c;
        else           l = c;
    }

    return( l );
}

// 四方向の壁の有無
int checkWall( int cx, int cy )
{
    int result = 0;

    for( int i=0; i<n; i++ ) {
        if( cx <  a[i][1] ) continue;
        if( cx >= b[i][1] ) continue;
        if( cy == c[i][1] ) result |= DOWN;
        if( cy == c[i][1] -1 ) result |= UP;
    }

    for( int i=0; i<m; i++ ) {
        if( cy <  e[i][1] ) continue;
        if( cy >= f[i][1] ) continue;
        if( cx == d[i][1] ) result |= LEFT;
        if( cx == d[i][1] -1 ) result |= RIGHT;
    }

//printDir( cx, cy, result );

    return( result );
}

// 
int main()
{
    long long tot = 0;

    clock_t start = clock();

    scanf( "%d %d", &n, &m );

    for( int i=0; i<n; i++ ) {
        scanf( "%d %d %d", &a[i][0], &b[i][0], &c[i][0] );
        x[xcnt++]=a[i][0];
        x[xcnt++]=b[i][0];
        y[ycnt++]=c[i][0];
    }

    for( int i=0; i<m; i++ ) {
        scanf( "%d %d %d", &d[i][0], &e[i][0], &f[i][0] );
        x[xcnt++]=d[i][0];
        y[ycnt++]=e[i][0];
        y[ycnt++]=f[i][0];
    }

    x[xcnt++] = y[ycnt++] = -INF;
    x[xcnt++] = y[ycnt++] = INF;
    x[xcnt++] = y[ycnt++] = 0;

    xcnt = sort( x, xcnt );
    ycnt = sort( y, ycnt );

//for( int i=0; i<xcnt; i++ ) printf( "x[%d] = %lld\n", i, x[i] );
//for( int i=0; i<ycnt; i++ ) printf( "y[%d] = %lld\n", i, y[i] );

    for( int i=0; i<n; i++ ) {
        a[i][1] = srch( a[i][0], x, xcnt );
        b[i][1] = srch( b[i][0], x, xcnt );
        c[i][1] = srch( c[i][0], y, ycnt );
//printf( "HORI %d: (%d %d %d)->(%d %d %d)\n", i,
//a[i][0],b[i][0],c[i][0], a[i][1],b[i][1],c[i][1] );
    }

    for( int i=0; i<m; i++ ) {
        d[i][1] = srch( d[i][0], x, xcnt );
        e[i][1] = srch( e[i][0], y, ycnt );
        f[i][1] = srch( f[i][0], y, ycnt );
//printf( "VERT %d: (%d %d %d)->(%d %d %d)\n", i,
//d[i][0],e[i][0],f[i][0], d[i][1],e[i][1],f[i][1] );
    }

    int cx = srch( 0, x, xcnt );
    int cy = srch( 0, y, ycnt );
    int cuex[2][MAX];
    int cuey[2][MAX];
    int cuenum[2] = { 1, 0 };
    int cuesel = 0;
    int cuenext;
    cuex[cuesel][0] = cx;
    cuey[cuesel][0] = cy;
    result[cx][cy] = 1;
    tot += (x[cx+1] - x[cx]) * (y[cy+1] - y[cy]);

    for( int r=1; r<100; r++ ) {

//printf( "*** %d (sel=%d,tot=%lld) ***\n", r, cuesel, tot );
        cuenext = 1 - cuesel;
        cuenum[cuenext] = 0;

        if( cuenum[cuesel] == 0 ) break;
        for( int i=0; i<cuenum[cuesel]; i++ ) {

            cx = cuex[cuesel][i];
            cy = cuey[cuesel][i];
            int wall = checkWall( cx, cy );

            // 左
            if( ! (wall & LEFT) && cx > 0 ) {
                cuex[cuenext][cuenum[cuenext]] = cx - 1;
                cuey[cuenext][cuenum[cuenext]] = cy;
                if( result[cx - 1][cy] == 0 ) {
                    cuenum[cuenext] ++;
                    result[cx - 1][cy] = r + 1;
                    tot += (x[cx] - x[cx-1]) * (y[cy+1] - y[cy]);
//printf( "left (%d %d) => %d\n", cx-1, cy, (x[cx] - x[cx-1]) * (y[cy+1] - y[cy]) );
                }
            }

            // 右
            if( ! (wall & RIGHT) && cx < xcnt ) {
                cuex[cuenext][cuenum[cuenext]] = cx + 1;
                cuey[cuenext][cuenum[cuenext]] = cy;
                if( result[cx + 1][cy] == 0 ) {
                    cuenum[cuenext] ++;
                    result[cx + 1][cy] = r + 1;
                    tot += (x[cx+2] - x[cx+1]) * (y[cy+1] - y[cy]);
//printf( "right (%d %d) => %d\n", cx+1, cy, (x[cx+1] - x[cx]) * (y[cy+1] - y[cy]) );
                }
            }

            // 下
            if( ! (wall & DOWN) && cy > 0 ) {
                cuex[cuenext][cuenum[cuenext]] = cx;
                cuey[cuenext][cuenum[cuenext]] = cy - 1;
                if( result[cx][cy - 1] == 0 ) {
                    cuenum[cuenext] ++;
                    result[cx][cy - 1] = r + 1;
                    tot += (x[cx+1] - x[cx]) * (y[cy] - y[cy-1]);
//printf( "down (%d %d) => %d\n", cx, cy-1, (x[cx+1] - x[cx]) * (y[cy] - y[cy-1]) );
                }
            }

            // 上
            if( ! (wall & UP) && cy < ycnt ) {
                cuex[cuenext][cuenum[cuenext]] = cx;
                cuey[cuenext][cuenum[cuenext]] = cy + 1;
                if( result[cx][cy + 1] == 0 ) {
                    cuenum[cuenext] ++;
                    result[cx][cy + 1] = r + 1;
                    tot += (x[cx+1] - x[cx]) * (y[cy+2] - y[cy+1]);
//printf( "up (%d %d) => %d\n", cx, cy+1, (x[cx+1] - x[cx]) * (y[cy+2] - y[cy+1]) );
                }
            }
        }

        cuesel = 1 - cuesel;
        if( cuenum[cuesel] == 0 ) break;
    }
/*
for( int j=ycnt-1; j>=0; j-- ) {
    for( int i=0; i<xcnt; i++ ) {
        printf( " %d", result[i][j] );
    }
    printf( "\n" );
}
*/

    if( result[0][0] > 0 ) puts( "INF" );
    else                   printf( "%lld\n", tot );
}