#P4024. Counting 2D Inversion Pairs in a Submatrix

    ID: 17272 Type: Default 1000ms 256MiB

Counting 2D Inversion Pairs in a Submatrix

Counting 2D Inversion Pairs in a Submatrix

Given an N×MN \times M integer matrix {A[i,j]}\{A[i,j]\} (1iN1 \leq i \leq N, 1jM1 \leq j \leq M), answer KK queries. For the ii-th query, count the number of 2D inversion pairs (x1,y1,x2,y2)(x_1, y_1, x_2, y_2) satisfying the conditions:

ui,1x1x2ui,2u_{i,1} \le x_1 \le x_2 \le u_{i,2} vi,1y1y2vi,2v_{i,1} \le y_1 \le y_2 \le v_{i,2} A[x1,y1]>A[x2,y2]A[x_1,y_1] > A[x_2,y_2]

Note that the indices of the matrix are 1-indexed.

inputFormat

The first line contains three integers NN, MM, and KK, representing the number of rows, the number of columns of the matrix, and the number of queries, respectively.

The next NN lines each contain MM integers, where the jj-th integer on the ii-th line represents A[i,j]A[i,j].

Each of the following KK lines contains four integers ui,1u_{i,1}, ui,2u_{i,2}, vi,1v_{i,1}, and vi,2v_{i,2}, describing a query that asks for the number of inversion pairs in the submatrix with rows from ui,1u_{i,1} to ui,2u_{i,2} and columns from vi,1v_{i,1} to vi,2v_{i,2}.

outputFormat

For each query, print one line containing a single integer representing the number of inversion pairs in the specified submatrix.

sample

3 3 2
1 2 3
4 5 6
7 8 9
1 2 1 2
2 3 2 3
0

0

</p>