Under construction

Still being built. Things may change

Lake Making

Time limit1sMemory limit128 MB

Summary
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 RR (3≤R≤1003 \le R \le 100) by CC (3≤C≤1003 \le C \le 100) grid of six-foot-by-six-foot squares and recorded the average elevation of each square in inches (10≤elev≤500010 \le \text{elev} \le 5000).

He has also trained the cows in "stomp digging". One instruction sends a herd that exactly covers a 3×33 \times 3 block of squares whose upper-left square is at row RsR_s (1≤Rs≤R−21 \le R_s \le R-2) and column CsC_s (1≤Cs≤C−21 \le C_s \le C-2). The herd stomps the ground down by DsD_s (1≤Ds≤401 \le D_s \le 40) 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 DsD_s.

Formally, let mm be the highest elevation among the nine squares of the block and let f=m−Dsf = m - D_s. After the instruction, every square in the block whose elevation is greater than ff becomes exactly ff, while squares already at or below ff are unchanged. (An elevation may become negative.)

You are given the initial elevations, an ordered list of NN (1≤N≤200001 \le N \le 20000) stomp-digging instructions applied in order, and a final water level EE (0≤E≤50000 \le E \le 5000). After all instructions are applied, each square holds water to a depth of EE minus its elevation whenever its elevation is below EE (a square at or above EE 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 6 ft×6 ft=72 in×72 in6\text{ ft} \times 6\text{ ft} = 72\text{ in} \times 72\text{ in}, so its water volume equals its depth in inches times 72×7272 \times 72 square inches. Report the total volume of water the lake holds, in cubic inches. The answer is guaranteed not to exceed 2,000,000,0002{,}000{,}000{,}000.

Worked example. Consider a 4×64 \times 6 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 36−4=3236 - 4 = 32 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 28−10=1828 - 10 = 18 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 E=22E = 22, 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 66×72×72=34214466 \times 72 \times 72 = 342144 cubic inches.

Input

  • Line 1: four space-separated integers RR, CC, EE, and NN.
  • Lines 2 to R+1R+1: line i+1i+1 contains CC space-separated integers giving the elevations of row ii.
  • Lines R+2R+2 to R+N+1R+N+1: line i+R+1i+R+1 contains three space-separated integers RsR_s, CsC_s, and DsD_s describing the ii-th stomp-digging instruction.

Output

  • A single integer: the total volume of water the lake holds, in cubic inches.

Examples2

  1. Example 1

    Input
    4 6 22 2
    28 25 20 32 34 36
    27 25 20 20 30 34
    24 20 20 20 20 30
    20 20 14 14 20 20
    1 4 4
    1 1 10
    
    Expected output
    342144
    
  2. Example 2

    Input
    3 3 0 1
    10 10 10
    10 10 10
    10 10 10
    1 1 5
    
    Expected output
    0