This page is still under construction.

Parts of this page are still being built. What you see may change.

Nuclearia

Time limit1sMemory limit1024 MB

Summary
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 W×HW \times H cells. Each plant occupies one cell and is given by two positive integers. aa is the amount of radiation caused to the cell where the plant stood, and bb says how fast that radiation drops as you move away from the plant.

The radiation caused to cell C=[xC,yC]C = [x_C, y_C] by the explosion of a plant in cell P=[xP,yP]P = [x_P, y_P] is max⁡(0,a−b⋅d(P,C))\max(0, a - b \cdot d(P, C)), where d(P,C)=max⁡(∣xP−xC∣,∣yP−yC∣)d(P, C) = \max(|x_P - x_C|, |y_P - y_C|) 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 a=7a = 7 and b=3b = 3 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 WW and HH, the width and the height of Nuclearia, with W⋅H≤2 500 000W \cdot H \le 2\,500\,000.

The second line contains a positive integer NN, the number of exploded plants (1≤N≤200 0001 \le N \le 200\,000).

Each of the next NN lines contains four positive integers xix_i, yiy_i, aia_i, bib_i (1≤xi≤W1 \le x_i \le W, 1≤yi≤H1 \le y_i \le H, 1≤ai,bi≤1091 \le a_i, b_i \le 10^9), describing a plant in cell [xi,yi][x_i, y_i] with parameters aia_i and bib_i. Each cell contains at most one plant.

The next line contains a positive integer QQ, the number of queries (1≤Q≤200 0001 \le Q \le 200\,000).

Each of the next QQ lines contains four positive integers x1jx_{1j}, y1jy_{1j}, x2jx_{2j}, y2jy_{2j} (1≤x1j≤x2j≤W1 \le x_{1j} \le x_{2j} \le W, 1≤y1j≤y2j≤H1 \le y_{1j} \le y_{2j} \le H), describing a query about the rectangle whose upper left corner is cell [x1j,y1j][x_{1j}, y_{1j}] and whose lower right corner is cell [x2j,y2j][x_{2j}, y_{2j}].

The total radiation in Nuclearia is less than 2632^{63}.

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 2×22 \times 2 square is 14, so the average is 14/4=3.514/4 = 3.5, which rounds to 4.
  • The total radiation in Nuclearia is 44, so the average is 44/12≈3.6744/12 \approx 3.67, which rounds to 4.
  • The average over a single cell is the radiation in that cell.
  • The average in the last row is 9/4=2.259/4 = 2.25, which rounds to 2.

Examples3

  1. Example 1

    Input
    4 3
    2
    1 1 7 3
    3 2 4 2
    4
    1 2 2 3
    1 1 4 3
    4 2 4 2
    1 3 4 3
    
    Expected output
    4
    4
    2
    2
    
  2. Example 2

    Input
    1 1
    1
    1 1 1000000000 1000000000
    1
    1 1 1 1
    
    Expected output
    1000000000
    
  3. Example 3

    Input
    2 1
    1
    1 1 4 1
    3
    1 1 2 1
    1 1 1 1
    2 1 2 1
    
    Expected output
    4
    4
    3