Aquapark
Time limit1sMemory limit512 MB
Sum the grid values inside the Manhattan diamond of radius l_i around each lifeguard.
- Level
Medium6 of 10
- Topics
- Prefix sum, Matrix
- Solved
- No attempts yet
Problem
An aquapark is shaped like a square with side length and is divided into unit cells of side . Each cell is either a pool or a walkway. A pool cell has a positive number of children playing in it; a walkway has none.
There are lifeguards on duty. Under the safety rules a lifeguard may move only parallel to the walls, so the distance between two cells and is always the Manhattan distance . Lifeguard is responsible for every pool that lies at distance at most from their position.
A lifeguard's workload is the total number of children in all the pools they guard. Determine the workload of every lifeguard.
Input
The first line contains two integers and (, ): the side length of the aquapark and the number of lifeguards.
Each of the next lines describes one row of the map. The -th line contains non-negative integers (). If , cell is a walkway; otherwise it is a pool holding children.
Each of the next lines describes one lifeguard with three integers , , (, ): the row and column of the lifeguard's position, and the maximum distance to the pools they guard.
Output
Print exactly lines. The -th line must contain a single integer : the number of children guarded by lifeguard .
Hint
