Stacking Pyramids
Time limit5sMemory limit1024 MB
Given N pyramids, each covering a Chebyshev-distance diamond of height h, take the elementwise maximum over all cells and output the total sum.
- Level
Medium7 of 10
- Topics
- Prefix sum, Math, Matrix, Implementation
- Solved
- No attempts yet
Problem
In the ancient kingdom of JOI, there was a custom of building pyramids in the desert as tombs for kings. In this country, when a king dies, a pyramid of a "certain height" decided by divination is built at a "certain location" decided by divination.
The desert of the kingdom of JOI is a rectangle W wide from east to west and H long from north to south. The desert is divided into 1 × 1 square cells, and each cell is denoted by (x, y), where x and y are integers satisfying 0 ≤ x < W and 0 ≤ y < H. Cell (0, 0) is the northwest corner cell, and cell (x, y) is located x cells east and y cells south of cell (0, 0).
A pyramid is built as follows. First, divination determines the center cell (X, Y) and the height h of the pyramid. Following that, the pyramid is built by stacking stones on each cell in the desert according to the following rule:
When building a pyramid of height h centered at cell (X, Y), cell (x, y) in the desert receives max{0, h − max{|X − x|, |Y − y|}} stones. No stones are placed outside the desert.
For example, when the desert has size W = 7, H = 6 and a pyramid of height 3 centered at cell (2, 1) is built, the number of stones on each cell is as follows.

However, the kingdom of JOI is not that large, so pyramids are sometimes built "overlapping" past pyramids. That is, when building a new pyramid would place n stones on some cell, if that cell already has n or more stones, nothing is done to that cell. On the other hand, if that cell has fewer than n stones, the number of stones on that cell is increased to n.
Therefore, the appearance after many pyramids are built becomes complex. For example, if a pyramid of height 4 centered at cell (4, 3) is built in the state of the figure above, the number of stones on each cell is as follows.

As an archaeologist, you want to know exactly how many stones were used to build the pyramids.
Given the center cell and height of every pyramid, write a program to find the number of stones needed to build them.
Input
The first line of input contains three integers W, H, N (1 ≤ W, H ≤ 3000, 1 ≤ N ≤ 10000). W and H are the width and height of the desert, respectively. N is the number of pyramids.
Lines 2 through N + 1 (1 ≤ i ≤ N) each contain three integers xi, yi, hi (0 ≤ xi < W, 0 ≤ yi < H, 1 ≤ hi ≤ 3000). These mean that the center cell of the i-th pyramid is (xi, yi) and its height is hi.
Output
Output to standard output. Print a single integer representing the number of stones needed to build all the pyramids.