Addition of submatrix
Submissions: 5515   Accuracy:

55.21%

  Difficulty: Easy   Marks: 2

Given a matrix C of size N x M. You are given position of submatrix as X1, Y1 and X2, Y2 inside the matrix. Find the sum of all elements inside that submatrix.

Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is n and m,n is the number of rows and m is the number of columns. The second line of each test case contains C[N][M]. The third line contains four value of X1, Y1, X2, Y2. X1, Y1 is the top left cell and X2, Y2 is the bottom right cell.

Output:
Print the sum of all elements inside that submatrix.

Constraints:
1 ≤ T ≤ 15
1 ≤ N, M ≤ 103
1 ≤ C[N][M] ≤ 106

1 <= X1, Y1, X2, Y2 <= M

Example:
Input:
2
5 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
3 4 4 5
3 3
9 8 7 4 2 1 6 5 3
1 2 3 3

Output:
78
26
Explanation:
Testcase 2:
Sum from cell starting at position (1, 2) (1-based indexing) and ending at (3, 3) is 26.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: rajatjha


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.