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

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

HỌC THEO LỘ TRÌNH

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

1 lộ trình

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

Diagonal Prefix NE-SW

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An integer matrix has mm rows and nn columns, indexed from 1. For each query (r,c,k)(r,c,k), sum exactly kk cells beginning at (r,c)(r,c) and moving one row down and one column left each step. Both endpoints are included; all queries fit within the matrix. Values may be negative.

Input

The first line contains m,n,Qm,n,Q, followed by mm rows of nn integers. Each of the next QQ lines contains r,c,kr,c,k.

Output

Print QQ lines, one answer per query.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤121\le m,n\le 12; 1≤Q≤301\le Q\le 30; ∣ai,j∣≤109|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,c)1\le k\le\min(m-r+1,c).
  • Subtask 2 (30 points): 1≤m,n≤10001\le m,n\le 1000; 1≤Q≤2000001\le Q\le 200000; ∣ai,j∣≤109|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,c)1\le k\le\min(m-r+1,c); k=1k=1.
  • Subtask 3 (50 points): 1≤m,n≤10001\le m,n\le 1000; 1≤Q≤2000001\le Q\le 200000; ∣ai,j∣≤109|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,c)1\le k\le\min(m-r+1,c).

Example 1

Input

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

Output

17
13
8

Explanation

The requested diagonals sum to 17, 13 and 8.

Example 2

Input

2 2 1
0 -5
-2 1
1 2 2

Output

-7

Explanation

The two cells contain −5 and −2.