Two-Dimensional Range Sum
InterviewTime limit1sMemory limit256 MB
Compute the sum of each queried rectangle in an N by N table using a 2D prefix sum.
- Level
Medium4 of 10
- Topics
- Prefix sum, Matrix
- Solved
- No attempts yet
Problem
An table is filled with numbers. Write a program that computes the sum of the entries from to . Here means row , column .
For example, take and the table below.
The sum from to is , and the sum from to is .
Given the numbers in the table and the sum queries, write a program that answers them.
Input
The first line contains the table size and the number of sums to compute, . (, )
Each of the next lines holds one row of the table, starting from row 1. A line lists the numbers of that row, starting from column 1. Every number in the table is a natural number no larger than 1,000.
Each of the next lines holds four integers , , , . (, )
Output
Print lines. On each line print the sum from to , one per query, in input order.