#P8470. Side Surface Area Calculation

    ID: 21645 Type: Default 1000ms 256MiB

Side Surface Area Calculation

Side Surface Area Calculation

We are given an infinite 2D grid (the ground) divided into unit squares. In each of n operations, you are provided with a grid cell coordinate \((x_i,y_i)\) and a number \(z_i\). At each operation, \(z_i\) unit cubes (each of side length 1) are stacked vertically on cell \((x_i,y_i)\). The stacked cubes form a 3D shape.

The side surface area of the shape is defined as follows: For every cube, each of its four lateral faces (front, back, left, right) is considered. A face is exposed if there is no cube immediately adjacent in that face's direction at the same level. In other words, for a cell \((x,y)\) with height \(h(x,y)\), the contribution from the face in a given direction (e.g. right) is \(\max(0, h(x,y)-h(x+1,y))\). Thus, the total side area \(S\) can be computed as:

$$ S = \sum_{(x,y)\in \text{grid}} \Bigl(\max(0, h(x,y)-h(x+1,y)) + \max(0, h(x,y)-h(x-1,y)) + \max(0, h(x,y)-h(x,y+1)) + \max(0, h(x,y)-h(x,y-1))\Bigr). $$

After each operation, output the current total side surface area of the entire shape.

inputFormat

The first line contains an integer \(n\) (\(1 \le n \le 10^5\)), the number of operations.

Each of the following \(n\) lines contains three integers \(x_i\), \(y_i\), and \(z_i\) (\(-10^9 \le x_i,y_i \le 10^9\), \(1 \le z_i \le 10^4\)), representing an operation where \(z_i\) cubes are stacked on cell \((x_i,y_i)\).

outputFormat

After each operation, output a single line containing one integer --- the side surface area of the entire structure.

sample

3
0 0 2
1 0 3
0 1 1
10

6 10

</p>