Here's the key formula. To find the sum from index to : Why does this work? contains . Subtracting removes , leaving exactly . One subtraction. per query.
With queries, total time is instead of . For , that's operations instead of .
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
The formula
Here's the key formula. To find the sum from index to : Why does this work? contains . Subtracting removes , leaving exactly . One subtraction. per query.
With queries, total time is instead of . For , that's operations instead of .