Approach hintTry it first, then revealHide
Store P[i][j], the sum of the rectangle from (0, 0) to (i - 1, j - 1). Any sub-rectangle sum then follows from four corner values by inclusion-exclusion, giving O(1) queries.
Store P[i][j], the sum of the rectangle from (0, 0) to (i - 1, j - 1). Any sub-rectangle sum then follows from four corner values by inclusion-exclusion, giving O(1) queries.