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 Scala (2.13.1) (judge verdict: Runtime Error)

object Main extends App {
  val sc = new FastScanner(System.in)
  val n, m = sc.nextInt()
  var xs, ys = List[Int](0)
  var lx, ly = List[(Int, Int, Int)]()
  for (i <- 1 to n) {
    val a, b, c = sc.nextInt()
    lx ::= (a,b,c)
    xs ::= a
    xs ::= b
    ys ::= c
  }
  for (i <- 1 to m) {
    val d, e, f = sc.nextInt()
    ly ::= (d,e,f)
    xs ::= d
    ys ::= e
    ys ::= f
  }

  val xsSorted = xs.distinct.sorted.toVector
  val ysSorted = ys.distinct.sorted.toVector

  if (xsSorted.length == 1 || ysSorted.length == 1) {
    println("INF")
  } else {
    val h = xsSorted.length
    val w = ysSorted.length
    val xCmp = xsSorted.zipWithIndex.toMap
    val yCmp = ysSorted.zipWithIndex.toMap
    val lxCmp = lx.map{case (x1,x2,y) => (xCmp(x1),xCmp(x2),yCmp(y))}
    val lyCmp = ly.map{case (x,y1,y2) => (xCmp(x),yCmp(y1),yCmp(y2))}
    val xWidth = Array.fill(h-1)(0)
    val yWidth = Array.fill(w-1)(0)
    for (x <- 0 until h - 1) {
      xWidth(x) = xsSorted(x+1) - xsSorted(x)
    }
    for (y <- 0 until w - 1) {
      yWidth(y) = ysSorted(y+1) - xsSorted(y)
    }

    val visited = Array.fill(2 * h - 1, 2 * w - 1)(false)
    for {
      (x1,x2,y) <- lxCmp
      x <- 2 * x1 to 2 * x2
    } {
      visited(x)(y*2) = true
    }
    for {
      (x,y1,y2) <- lyCmp
      y <- 2 * y1 to 2 * y2
    } {
      visited(x*2)(y) = true
    }

    val xInit = xCmp(0) * 2
    val yInit = yCmp(0) * 2

    if (xInit == 0 || xInit == 2 * h - 2 || yInit == 0 || yInit == 2 * w - 2) {
      println("INF")
    } else {
      visited(xInit)(yInit) = true

      var ans = 0L
      var queue = List((xCmp(0) * 2, yCmp(0) * 2))
      val d = List((1,0),(-1,0),(0,1),(0,-1))
      while (queue.nonEmpty) {
        val (x,y) = queue.head
        queue = queue.tail
        for ((dx, dy) <- d) {
          val x1 = x + dx
          val y1 = y + dy
          if (!visited(x1)(y1)) {
            if (x1 == 0 || x1 == 2 * h - 2 || y1 == 0 || y1 == 2 * w - 2) {
              ans = -1
              queue = Nil
            } else {
              queue ::= (x1, y1)
              visited(x1)(y1) = true
              if (x1 % 2 == 1 && y1 % 2 == 1) {
                ans += xWidth(x1 / 2) * yWidth(y1 / 2)
              }
            }
          }
        }
      }

      println(if (ans == -1) "INF" else ans)
    }
  }
}

class FastScanner(source: java.io.InputStream) {
  val buf = new Array[Byte](1024)
  var now, end = 0

  def isPrintable(b: Byte): Boolean = 33 <= b && b <= 126
  def isNum(b: Byte): Boolean = 48 <= b && b <= 57

  def hasNextByte(): Boolean = {
    if (now < end) {
      true
    } else {
      now = 0
      end = source.read(buf)
      end > 0
    }
  }

  def nextByte(): Byte = {
    if (hasNextByte()) {
      val b = buf(now)
      now += 1
      b
    } else {
      -1
    }
  }

  def hasNext(): Boolean = {
    while (hasNextByte() && !isPrintable(buf(now))) {
      now += 1
    }
    hasNextByte()
  }

  def next(): String = {
    if (hasNext()) {
      val sb = new StringBuilder()
      var b = nextByte()
      while (isPrintable(b)) {
        sb += b.toChar
        b = nextByte()
      }
      sb.toString
    } else {
      throw new NoSuchElementException()
    }
  }

  def nextInt(): Int = {
    nextLong().toInt
  }

  def nextLong(): Long = {
    if (hasNext()) {
      var b = nextByte()
      var sign = 1
      var n = 0L
      if (b == '-') {
        sign = -1
        b = nextByte()
      }
      while (isNum(b)) {
        n = n * 10 + (b - 48)
        b = nextByte()
      }
      sign * n
    } else {
      throw new NoSuchElementException()
    }
  }

  def nextDouble(): Double = next().toDouble
}