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

#PS0000220. Tổng đường chéo Tây Bắc – Đông Nam (Diagonal Prefix NW-SE)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng đường chéo Tây Bắc – Đông Nam (Diagonal Prefix NW-SE)

Diagonal Prefix NW-SE

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An integer matrix aa has mm rows and nn columns, indexed from 1 from top to bottom and left to right. Each query (r,c,k)(r,c,k) asks for the sum of exactly kk cells on the down-right diagonal beginning at (r,c)(r,c): ar,c+ar+1,c+1+⋯+ar+k−1,c+k−1a_{r,c}+a_{r+1,c+1}+\cdots+a_{r+k-1,c+k-1}. Each query fits entirely inside the matrix. Values may be negative.

Input

The first line contains m,n,Qm,n,Q. The next mm lines each contain nn integers. Each of the following QQ lines contains three integers r,c,kr,c,k.

Output

Print QQ lines, one diagonal sum per query.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤121\le m,n\le 12; 1≤Q≤301\le Q\le 30; −109≤ai,j≤109-10^9\le a_{i,j}\le10^9; 1≤r≤m1\le r\le m; 1≤c≤n1\le c\le n; 1≤k≤min⁡(m−r+1,n−c+1)1\le k\le\min(m-r+1,n-c+1).
  • Subtask 2 (30 points): 1≤m,n≤10001\le m,n\le 1000; 1≤Q≤2000001\le Q\le 200000; −109≤ai,j≤109-10^9\le a_{i,j}\le10^9; 1≤r≤m1\le r\le m; 1≤c≤n1\le c\le n; 1≤k≤min⁡(m−r+1,n−c+1)1\le k\le\min(m-r+1,n-c+1); k=1k=1.
  • Subtask 3 (50 points): 1≤m,n≤10001\le m,n\le 1000; 1≤Q≤2000001\le Q\le 200000; −109≤ai,j≤109-10^9\le a_{i,j}\le10^9; 1≤r≤m1\le r\le m; 1≤c≤n1\le c\le n; 1≤k≤min⁡(m−r+1,n−c+1)1\le k\le\min(m-r+1,n-c+1).

Example 1

Input

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

Output

3
-2
12
1

Explanation

The sums are 2+5-4, -1-2+1, 4+8 and 1.

Example 2

Input

1 1 1
-1000000000
1 1 1

Output

-1000000000

Explanation

The only cell is negative and its value is returned unchanged.