Fishing
InterviewTime limit2sMemory limit1024 MB
For each query (W, P), the task is to sum the fish caught by bait placed at (W, P) that is reeled diagonally up-left until it leaves the grid.
- Level
Medium4 of 10
- Topics
- Prefix sum, Matrix
- Solved
- No attempts yet
Problem
Geondeok sits on Warudo, a small island attached to Ilgam Lake, reeling in time.

Ilgam Lake can be represented as an grid. The grid is divided into square cells, and each cell holds several fish. The cell that is the deepest and farthest from Geondeok is .
Geondeok can swing a fishing rod to throw bait into any cell he wants. If he attaches a sinker of weight and swings the rod with strength , bait is placed in cell .
The bait catches fish inside the lake grid. It catches all fish in every cell from the cell where the bait sits up to the surface. In other words, bait at catches all fish in for every integer with .
When Geondeok reels the line in by one turn, the bait at moves to and catches the fish in its new cell. If the bait leaves the lake grid, it is retrieved immediately.
Each time Geondeok gives a sinker weight and a swing strength, find how many fish are caught by the time the bait is retrieved after the swing and reeling in. Help Geondeok catch as many fish as possible!
Input
The first line contains the integers for the lake size and , the number of times Geondeok swings the rod, separated by spaces. (; )
From the second line through the lines that follow, the number of fish in each cell is given. The -th number on the -th line means cell has fish. ()
From line through the lines that follow, the integer , the weight of the sinker Geondeok attaches to the bait, and the integer , the strength of the swing, are given separated by a space. (; )
Output
For each of the fishing attempts, print on its own line how many fish are caught before the bait is retrieved.
Hints
Since the input is large, fast I/O is recommended. Fast I/O for common languages is shown below.
- C++: When using
cinandcout, you must callcin.tie(nullptr); ios::sync_with_stdio(false);once before any I/O. For a newline, use'\n'instead ofendl. - Java: You must use
BufferedReaderandBufferedWriter. - Python3, PyPy3: Use
sys.stdin.readline().rstrip()instead ofinput().