improved computation of segment sums