See https://www.chiark.greenend.org.uk/~sgtatham/algorithms/cumulative.html
