#B4149. Overlapping Cuboids

    ID: 11806 Type: Default 1000ms 256MiB

Overlapping Cuboids

Overlapping Cuboids

In a 3-dimensional grid, the space is partitioned into unit cubes of size \(1 \times 1 \times 1\). Each cube is identified by its coordinate \((i, j, k)\), where \(i\) is the column index (from left to right), \(j\) is the row index (from front to back) and \(k\) is the level (from bottom to top).

You are given two rectangular blocks (which can be cubes) in this grid. For each block, you are provided with six positive integers \(a_i, a_j, a_k, b_i, b_j, b_k\) that describe the block. The block consists of all the unit cubes whose \(i\)-coordinate ranges from \(a_i\) to \(b_i\), \(j\)-coordinate from \(a_j\) to \(b_j\), and \(k\)-coordinate from \(a_k\) to \(b_k\). It is guaranteed that \(a_i \le b_i\), \(a_j \le b_j\), and \(a_k \le b_k\).

Your task is to determine if the two blocks overlap. If they do, calculate the number of unit cubes that are common to both blocks. The overlapping volume in each dimension can be calculated using the formulas below:

[ L_x = \max\left(0, \min(b_{1}, d_{1}) - \max(a_{1}, c_{1}) + 1\right), ] [ L_y = \max\left(0, \min(b_{2}, d_{2}) - \max(a_{2}, c_{2}) + 1\right), ] [ L_z = \max\left(0, \min(b_{3}, d_{3}) - \max(a_{3}, c_{3}) + 1\right). ]

The total number of overlapping unit cubes is then computed as:

[ \text{Overlap} = L_x \times L_y \times L_z. ]

</p>

If the blocks do not overlap, the result will be 0.

inputFormat

The input consists of two lines. Each line contains six positive integers separated by spaces. The first line represents the first block with the integers: ai aj ak bi bj bk and the second line represents the second block with the integers: ci cj ck di dj dk.

outputFormat

Output a single integer denoting the number of unit cubes that the two blocks overlap.

sample

1 1 1 3 3 3
2 2 2 4 4 4
8