UFO
Time limit2sMemory limit256 MB
Simulate laser shots that each destroy up to R blocks at height h along one row or column, then find the P by P square holding the most surviving blocks.
- Level
Medium6 of 10
- Topics
- Segment tree, Simulation, Prefix sum
- Solved
- No attempts yet
Problem
An alien spaceship has been forced down in a desert and has to be destroyed. The ship is built from unit cube blocks, and its bottom layer is an rectangle. Cell of that rectangle carries a stack of blocks, and the layers of a stack are numbered , and so on from the ground up. Rows are numbered to from the north side to the south side, and columns are numbered to from the west side to the east side. The figure shows the top view of a ship with and .

The blocks are made of a metal that only a laser can cut, so laser guns were placed on the four sides of the ship. A ray flies perpendicular to the side it was fired from, parallel to the ground, along one fixed layer.
A shot is given by a side, an index, and a height . A ray fired from the west runs along the given row from column toward column , and a ray fired from the east runs along the same row from column toward column . A ray fired from the north runs along the given column from row toward row , and a ray fired from the south runs along the same column from row toward row .
The ray passes over the cells of that line one at a time. If a cell holds at least blocks, the ray destroys the block on layer , every block above it drops one layer, and the height of that cell falls by . At most one block is destroyed in a cell, and the ray moves straight on to the next one. A cell holding fewer than blocks lets the ray through unchanged. The ray stops once it has destroyed blocks, and it also stops if it leaves the ship before that.
After the shots an airstrike is called on a square area of size . The area with the largest number of surviving blocks is chosen, and the strike destroys every block inside it. Compute how many blocks the airstrike destroys.
Input
The first line contains five integers , , , , (, , , ).
Each of the next lines contains integers. The -th number on the -th line is the number of blocks stacked on cell , between and .
Each of the next lines describes one shot with a single character and two integers, separated by spaces. The character is W for west, E for east, S for south, or N for north. For W and E the first integer is a row number between and ; for N and S it is a column number between and . The second integer is the height of the shot, between and .
Output
Print one integer, the largest number of blocks left inside a area after all shots.
Hint

The figure shows the ship of the first example after every shot of that example. The shaded square is the area holding the most blocks.