This is Range Sum Query 2D (two-dimensional) from LeetCode. Given a 2D matrix, handle multiple queries that each ask for the sum of elements in a rectangle defined by its upper-left and lower-right corners.
The 1D (one-dimensional) formula was . For 2D, you need something more sophisticated. The 2D version needs prefix sum lookups per query using inclusion-exclusion on the rectangle corners.