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

#PS0000229. Tổng khung viền chữ nhật (Rectangle Border Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng khung viền chữ nhật (Rectangle Border Sum)

Rectangle Border Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a static integer m×nm\times n matrix indexed from 1, sum the cells on the border of each inclusive rectangle (r1,c1,r2,c2)(r_1,c_1,r_2,c_2). A cell is counted once if its row is r1r_1 or r2r_2 or its column is c1c_1 or c2c_2, including single-row and single-column rectangles.

Input

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

Output

Print QQ lines, one border sum per query.

Subtasks

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

Example 1

Input

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

Output

40
15
5

Explanation

Outer border is 45−5=40. Row 2 sums to 15; the center is 5.

Example 2

Input

2 1 1
-4
2
1 1 2 1

Output

-2

Explanation

Count each cell once: −4+2=−2.