Define as the sum of all elements in the rectangle from to . To build it: You add the top and left rectangles, but they overlap at the top-left corner, so you subtract it once.
To query a rectangle from to : Same inclusion-exclusion principle.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Inclusion-exclusion
Define as the sum of all elements in the rectangle from to . To build it: You add the top and left rectangles, but they overlap at the top-left corner, so you subtract it once.
To query a rectangle from to : Same inclusion-exclusion principle.