luogu#P4800. [CEOI 2015] 核能国度 (Day2)

[CEOI 2015] 核能国度 (Day2)

Problem Description

Nuclear Country can be seen as a rectangle made of a W×HW \times H grid of cells. There are NN nuclear power plants in Nuclear Country, and each plant occupies one cell. Unfortunately, Nuclear Country has suffered a once-in-a-century major earthquake, causing all nuclear power plants to leak radiation.

The leakage level of each nuclear power plant can be described by two integers a,ba, b. If the plant at P=[xP,yP]P=[x_P,y_P] explodes, then a cell C=[xC,yC]C=[x_C,y_C] will receive an additional max(0,\mathrm{max}(0, ab×d(P,C))a-b\times d(P,C)) becquerels of radiation (becquerel is a unit), where d(P,C)d(P,C) is the Chebyshev distance between the two cells, i.e. d(P,C)=d(P,C) = max(xPxC,\mathrm{max}(|x_P - x_C|, yPyC)|y_P - y_C|).

A cell may be affected by radiation leaks from multiple plants.

For example, if a plant with a=7,a = 7, b=3b = 3 explodes, the cell XX where it is located will receive 77 becquerels of radiation. The 88 cells YY with d(X,Y)=1d(X,Y) = 1 will receive 44 becquerels, and the 1616 cells ZZ with d(X,Z)=2d(X,Z) = 2 will receive 11 becquerel.

The environmental agency gives you QQ queries. Each query specifies a rectangle within Nuclear Country. You need to answer: what is the average radiation received (per cell) within that rectangular area.

Input Format

The first line contains two positive integers WW and HH (W×H2.5×106)(W \times H \leq 2.5\times 10^6), representing the width and height of Nuclear Country.

The second line contains one positive integer NN, the number of nuclear power plants (1N2×105)(1 \leq N \leq 2\times 10^5).

In the next NN lines, each line contains four positive integers xi,yi,ai,bix_i,y_i,a_i,b_i $(1 \leq x_i \leq W, 1 \leq y_i \leq H, 1 \leq a_i,b_i \leq 10^9)$, meaning there is a plant at cell [xi,yi][x_i,y_i] with parameters aia_i and bib_i. Each cell contains at most one plant.

Line N+3N+3 contains one positive integer QQ, the number of queries (1Q2×105)(1 \leq Q \leq 2\times 10^5).

In the next QQ lines, each line contains four positive integers x1j,y1j,x2j,y2jx_{1j},y_{1j},x_{2j},y_{2j} $(1 \leq x_{1j} \leq x_{2j} \leq W, 1 \leq y_{1j} \leq y_{2j} \leq H)$, describing a query rectangle whose top-left corner is [x1j,y1j][x_{1j},y_{1j}] and whose bottom-right corner is [x2j,y2j][x_{2j},y_{2j}].

You may assume that the total radiation within Nuclear Country is less than 2632^{63}.

Output Format

For each query, output one line with the average radiation over all cells in the given rectangle, rounded to the nearest integer.

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
4
4
2
2

Hint

The following shows the radiation in each cell after two explosions:

7 6 3 2
4 6 5 2
1 3 3 2
  • The total radiation in a 222^2 square area is 1414, so the average is 14÷4=3.514\div 4=3.5, which rounds to 44.

  • The total radiation in the whole Nuclear Country is 4444, so the average is 44÷123.6744\div 12 \approx 3.67, which rounds to 44.

  • For a single cell, the average radiation is exactly the radiation it receives.

  • For the last row, the average is 9÷4=2.259\div 4=2.25, which rounds to 22.

There are 14 test cases. The odd-numbered test cases contain only plants where aa is a multiple of bb. Further limits for each subtask are as follows:

Test case Additional constraints Score
11 H=1,NW108,QW108H=1,N\cdot W \leq 10^8,Q \cdot W \leq 10^8 33
22 22
33 $N\cdot W \cdot H \leq 10^8,Q \cdot W \cdot H \leq 10^8$ 33
44 22
55 H=1,NW108H=1,N\cdot W \leq 10^8 66
66 44
77 NWH108N\cdot W \cdot H \leq 10^8 66
88 44
99 H=1H=1 1515
1010 1010
1111 There are no explosion events that satisfy the definition of boundary 1515
1212 1010
1313 None 1212
1414 88

If a nuclear power plant is on the border of Nuclear Country, or close to the border, its explosion may also affect cells outside Nuclear Country. An explosion that affects cells outside Nuclear Country is called a boundary.

Translated by ChatGPT 5