#P8231. [AGM 2022 资格赛] 农场

[AGM 2022 资格赛] 农场

Problem Description

You are a capitalist, managing a grid-shaped field of size n×mn \times m. Initially, the cell in row ii and column jj contains ai,ja_{i,j} units of wheat.

On this field, you have marked QQ rectangles, each representing a farm (different farms may overlap). The amount of wheat of a farm equals the sum of wheat over all cells inside the rectangle. A farm makes a profit if and only if the total wheat in this farm is greater than or equal to the required amount bib_i.

Unfortunately, there will be heavy rain for TT consecutive days. Each day, the wheat amount in some cells decreases.

For each farm, determine for how many days it can make a profit. If it is still profitable after TT days, output 1-1.

Input Format

The first line contains two integers n,mn, m.

The next nn lines each contain mm integers ai,ja_{i,j}.

The next line contains an integer QQ. The following QQ lines each contain five numbers l,r,L,R,bl, r, L, R, b, representing the top-left coordinate, the bottom-right coordinate, and the required wheat amount for profit of the ii-th farm.

The next line contains an integer TT. The following input consists of TT blocks. In each block, first an integer pp is given, indicating the events on that day, followed by pp lines. Each of these lines contains three integers x,y,zx, y, z, meaning the wheat in cell (x,y)(x, y) decreases by zz.

Output Format

Output one line with QQ integers, representing the answers.

5 6
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
5
2 1 2 3 10
2 2 5 6 15
3 4 4 5 1
1 2 5 4 13
5 1 5 6 6
4
2
5 6 1
5 4 1
3
1 3 1
2 2 1
2 3 1
1
3 3 1
2
4 6 1
1 1 1

0 3 -1 1 0

Hint

Constraints

For 100%100\% of the testdata, it is guaranteed that 1n,m5001 \leq n, m \leq 500, 1Q500001 \leq Q \leq 50000, 0ai,j1090 \leq a_{i,j} \leq 10^9, 1l<Ln1 \leq l < L \leq n, 1<r<Rm1 < r < R \leq m, 0bi10180 \leq b_i \leq 10^{18}, 1T500001 \leq T \leq 50000, 1xn1 \leq x \leq n, 1ym1 \leq y \leq m, 1z1091 \leq z \leq 10^9, 1p1051 \leq \sum p \leq 10^5. It is guaranteed that at any time zax,yz \leq a_{x,y}.

Notes

Translated from AGM 2022 Qualification Round C TimeToFarm.

Translated by ChatGPT 5