Matrix Subarray Sum Queries HARD Arrays 50 XP 0 solved
Mobile coding works. A laptop is faster for long sessions.

Problem

Given a matrix and Q queries, each query is four integers (r1, c1, r2, c2). For each query, print the sum of all elements in the submatrix with top-left (r1,c1) and bottom-right (r2,c2) (0-based, inclusive).

Input format

Input input.txt

- Three integers: N M Q (1 ≤ N, M ≤ 20, 1 ≤ Q ≤ 10) - N rows of M integers - Q lines, each with r1 c1 r2 c2 (0 ≤ r1 ≤ r2 < N, 0 ≤ c1 ≤ c2 < M)

Output format

Output output.txt

- Q lines: sum for each query

Example

input
2 2 2
1 2
3 4
0 0 0 1
0 0 1 1
output
3
10

Ready to solve this challenge?

Create a free account to write code, submit solutions, and track your progress.

⌨️ Keyboard Shortcuts

Code Editor
Run Code
Ctrl Enter
Submit Code
Ctrl Shift Enter
Format Code
Shift Alt F
Toggle Comment
Ctrl /
Undo
Ctrl Z
Redo
Ctrl Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc

🔍 Interactive Debugger

0 / 0

Analyzing your code...

📦 Variables

No variables yet

📚 Call Stack
main() line 1
📤 Output
We use cookies

Essential cookies are always active. You can choose to enable preference and analytics cookies. Learn more