#K276. Minimum Obstacles Removal

    ID: 24808 Type: Default 1000ms 256MiB

Minimum Obstacles Removal

Minimum Obstacles Removal

You are given a grid with very large dimensions (up to \(10^9 \times 10^9\)). There are two types of obstacles placed on the grid:

  • Vertical obstacles, whose positions are given as a list of integers.
  • Horizontal obstacles, each described by a tuple of three integers representing the starting column, ending column, and the row where the obstacle lies.

Your task is to determine the minimum number of obstacles that must be removed in order to create a path from the top-left cell \((1, 1)\) to the bottom-right cell \((10^9, 10^9)\).

The twist is that if one type of obstacles is absent (i.e. count is zero), you will need to remove all obstacles of the other type. Otherwise, if both types are present, you only need to remove the smaller number among them. In other words, if \(n\) is the number of vertical obstacles and \(m\) is the number of horizontal obstacles, the answer is:

[ \text{answer} = \begin{cases} \max(n, m), & \text{if } n = 0 \text{ or } m = 0, \ \min(n, m), & \text{otherwise.} \end{cases} ]

Note that the actual positions or spans of the obstacles do not affect the minimal count required.

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains two space-separated integers \(n\) and \(m\) representing the number of vertical and horizontal obstacles, respectively.
  2. If \(n > 0\), the second line contains \(n\) space-separated integers representing the positions of the vertical obstacles. If \(n = 0\), this line will be empty.
  3. If \(m > 0\), the next \(m\) lines each contain three space-separated integers \(a\), \(b\), and \(r\) describing a horizontal obstacle that spans from column \(a\) to column \(b\) at row \(r\). If \(m = 0\), these lines are omitted.

outputFormat

Output a single integer to stdout representing the minimum number of obstacles to remove so that a path from \((1, 1)\) to \((10^9, 10^9)\) is possible.

## sample
0 0

0