PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000227. Đếm ô đạt ngưỡng trong hình chữ nhật (Threshold Rectangle Count)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm ô đạt ngưỡng trong hình chữ nhật (Threshold Rectangle Count)

Threshold Rectangle Count

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a static m×nm\times n matrix indexed from 1 with entries from 1 to 10, each query (r1,c1,r2,c2,K)(r_1,c_1,r_2,c_2,K) asks for the number of cells (i,j)(i,j) satisfying r1≤i≤r2r_1\le i\le r_2, c1≤j≤c2c_1\le j\le c_2 and ai,j≥Ka_{i,j}\ge K. Boundaries and equality are included; the matrix never changes.

Input

The first line contains m,n,Qm,n,Q, followed by mm rows of exactly nn integers. Each of the next QQ lines contains r1,c1,r2,c2,Kr_1,c_1,r_2,c_2,K.

Output

Print QQ lines, one count per query.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤121\le m,n\le 12; 1≤Q≤301\le Q\le 30; 1≤ai,j≤101\le a_{i,j}\le10; 1≤r1≤r2≤m1\le r_1\le r_2\le m; 1≤c1≤c2≤n1\le c_1\le c_2\le n; 1≤K≤101\le K\le10.
  • Subtask 2 (30 points): 1≤m,n≤5001\le m,n\le 500; 1≤Q≤1000001\le Q\le 100000; 1≤ai,j≤101\le a_{i,j}\le10; 1≤r1≤r2≤m1\le r_1\le r_2\le m; 1≤c1≤c2≤n1\le c_1\le c_2\le n; 1≤K≤101\le K\le10; K=1K=1.
  • Subtask 3 (50 points): 1≤m,n≤5001\le m,n\le 500; 1≤Q≤1000001\le Q\le 100000; 1≤ai,j≤101\le a_{i,j}\le10; 1≤r1≤r2≤m1\le r_1\le r_2\le m; 1≤c1≤c2≤n1\le c_1\le c_2\le n; 1≤K≤101\le K\le10.

Example 1

Input

2 3 4
1 5 10
7 5 2
1 1 2 3 5
1 2 2 2 5
1 1 1 3 10
2 1 2 3 1

Output

4
2
1
3

Explanation

The first query counts 5,10,7,5; the second counts two fives; the third counts 10; the last counts the entire second row.

Example 2

Input

1 2 2
1 1
1 1 1 2 2
1 1 1 2 1

Output

0
2

Explanation

Neither cell reaches 2; both reach 1.