Nuclearia
Time limit1sMemory limit1024 MB
Add up the linearly decaying king-move radiation from every plant in each grid cell, then answer each rectangle query with its rounded average.
- Level
Hard8 of 10
- Topics
- Prefix sum, Math
- Solved
- No attempts yet
Problem
Long ago the people of Nuclearia built several nuclear plants. The country prospered for many years, then a very strong earthquake hit the land. Every plant exploded and radiation spread across the country. Once the people had stopped any further radiation from escaping, the Ministry of Environment started to measure how polluted the individual regions were. Write a program that answers the queries of the Ministry.
How radiation spreads
Nuclearia is a rectangle of cells. Each plant occupies one cell and is given by two positive integers. is the amount of radiation caused to the cell where the plant stood, and says how fast that radiation drops as you move away from the plant.
The radiation caused to cell by the explosion of a plant in cell is , where is the smallest number of moves a chess king needs between the two cells.
The total radiation in a cell is the sum of the amounts that the individual explosions caused to it.
For example, a plant with and causes 7 units of radiation to its own cell, 4 units to the 8 adjacent cells, and 1 unit to the 16 cells at distance 2. If such a plant sits on the border of Nuclearia or one cell away from it, the explosion also reaches cells outside Nuclearia. What happens outside the country is never asked about.
Queries
The Ministry of Environment asks several times about the average radiation per cell in a given rectangular region. The queried regions may overlap or repeat.
Input
The first line contains two space separated positive integers and , the width and the height of Nuclearia, with .
The second line contains a positive integer , the number of exploded plants ().
Each of the next lines contains four positive integers , , , (, , ), describing a plant in cell with parameters and . Each cell contains at most one plant.
The next line contains a positive integer , the number of queries ().
Each of the next lines contains four positive integers , , , (, ), describing a query about the rectangle whose upper left corner is cell and whose lower right corner is cell .
The total radiation in Nuclearia is less than .
Output
For each query print one line with the average radiation per cell in the queried region, rounded to the nearest integer. A value that ends in exactly 0.5 is rounded up.
Notes on the first example
In the first example the radiation in Nuclearia after the two explosions is
7 6 3 2
4 6 5 2
1 3 3 2
and the four queries work out as follows.
- The total radiation in the square is 14, so the average is , which rounds to 4.
- The total radiation in Nuclearia is 44, so the average is , which rounds to 4.
- The average over a single cell is the radiation in that cell.
- The average in the last row is , which rounds to 2.