LeetCode 304 Range Sum Query 2D - Immutable - Why Naive Fails

The trap

For each query, loop through all elements in the rectangle and sum them.

A rectangle can have up to m×nm \times n elements. With qq queries, that's O(qmn)O(q \cdot m \cdot n).

For a 200×200200 \times 200 matrix with 10410^4 queries, you'd access 400400 million elements. Too slow.