Lake Making
Time limit1sMemory limit128 MB
Apply each 3x3 stomp by flattening the block down to its maximum minus D, then sum water depth times 72*72 for squares below level E.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Array, Brute force
- Solved
- No attempts yet
Problem
Farmer John wants his cows to help him dig a lake. He has mapped the pasture as an () by () grid of six-foot-by-six-foot squares and recorded the average elevation of each square in inches ().
He has also trained the cows in "stomp digging". One instruction sends a herd that exactly covers a block of squares whose upper-left square is at row () and column (). The herd stomps the ground down by () inches, but the cows are meticulous: those standing on lower squares do not start stomping until the descending ground reaches their level. As a result, only the squares that are high enough are pushed down, and the whole block is flattened to a single floor level equal to the block's maximum elevation minus .
Formally, let be the highest elevation among the nine squares of the block and let . After the instruction, every square in the block whose elevation is greater than becomes exactly , while squares already at or below are unchanged. (An elevation may become negative.)
You are given the initial elevations, an ordered list of () stomp-digging instructions applied in order, and a final water level (). After all instructions are applied, each square holds water to a depth of minus its elevation whenever its elevation is below (a square at or above holds no water). The edges of the pasture act as barriers, so water never spills over the border and the depth is computed square by square.
Each square is , so its water volume equals its depth in inches times square inches. Report the total volume of water the lake holds, in cubic inches. The answer is guaranteed not to exceed .
Worked example. Consider a pasture with these initial elevations:
c1 c2 c3 c4 c5 c6
r1: 28 25 20 32 34 36
r2: 27 25 20 20 30 34
r3: 24 20 20 20 20 30
r4: 20 20 14 14 20 20
Apply the instruction 1 4 4 (upper-left square at row 1, column 4, depth 4). The block covers rows 1-3 and columns 4-6; its highest elevation is 36, so the floor is and only the three squares above 32 are lowered:
c1 c2 c3 c4 c5 c6
r1: 28 25 20 32 32 32
r2: 27 25 20 20 30 32
r3: 24 20 20 20 20 30
r4: 20 20 14 14 20 20
Next apply 1 1 10. The block covers rows 1-3 and columns 1-3; its highest elevation is 28, so the floor is and every square in the block drops to 18:
c1 c2 c3 c4 c5 c6
r1: 18 18 18 32 32 32
r2: 18 18 18 20 30 32
r3: 18 18 18 20 20 30
r4: 20 20 14 14 20 20
With a final water level of , the depths are as follows (a dot means the square holds no water):
c1 c2 c3 c4 c5 c6
r1: 4 4 4 . . .
r2: 4 4 4 2 . .
r3: 4 4 4 2 2 .
r4: 2 2 8 8 2 2
The total depth is 66 inches, so the volume is cubic inches.
Input
- Line 1: four space-separated integers , , , and .
- Lines 2 to : line contains space-separated integers giving the elevations of row .
- Lines to : line contains three space-separated integers , , and describing the -th stomp-digging instruction.
Output
- A single integer: the total volume of water the lake holds, in cubic inches.