#P5229. Surface Area of the Union of Cubes

    ID: 18465 Type: Default 1000ms 256MiB

Surface Area of the Union of Cubes

Surface Area of the Union of Cubes

In a three-dimensional space, there are \(N\) cubes. The \(i\)-th cube occupies the space from \(x_{i_1}, y_{i_1}, z_{i_1}\) to \(x_{i_2}, y_{i_2}, z_{i_2}\). These cubes may intersect or overlap, forming a composite geometric shape. Your task is to compute the total external surface area of the union of these cubes.

Note that if two cubes share a face, the common interior surface is not counted. The surface area should be computed in \(\text{unit}^2\) based on the given coordinates. In other words, you need to calculate the surface area of the union of all volumes.

Hint: A possible approach is to discretize the coordinate space by collecting all unique \(x\), \(y\), and \(z\) boundaries from the cubes, marking the cells that lie inside the union, and then summing up the areas of the faces that are adjacent to an empty cell (or lie on the boundary).

The formulas for a face area between two adjacent cells can be represented as:

[ \text{Area} = (y_{i+1} - y_i) \times (z_{j+1} - z_j) \quad \text{(for a face perpendicular to the x-axis)} ]

and similarly for the other two axes.

inputFormat

The first line contains an integer \(N\) \( (1 \leq N \leq 50)\) representing the number of cubes.

The following \(N\) lines each contain 6 integers: \(x_{i_1}\, y_{i_1}\, z_{i_1}\, x_{i_2}\, y_{i_2}\, z_{i_2}\), which describe the opposite corners of the \(i\)-th cube. It is guaranteed that \(x_{i_1} < x_{i_2}\), \(y_{i_1} < y_{i_2}\) and \(z_{i_1} < z_{i_2}\).

outputFormat

Output a single integer, which is the total external surface area of the composite figure formed by the union of the cubes.

sample

1
0 0 0 1 1 1
6