A 2D prefix sum array stores cumulative sums over rectangles.
For matrix : You get the sum of all elements in the rectangle with top-left and bottom-right .
Building this array takes time for an matrix. After that, any submatrix sum is .
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Rectangle sums
A 2D prefix sum array stores cumulative sums over rectangles.
For matrix : You get the sum of all elements in the rectangle with top-left and bottom-right .
Building this array takes time for an matrix. After that, any submatrix sum is .