let prefix_sums xs =
  let n = List.length xs in
  let arr = Array.make (n + 1) 0 in
  let i = ref 0 in
  List.iter (fun x ->
    arr.(!i + 1) <- arr.(!i) + x;
    i := !i + 1
  ) xs;
  arr

let range_sum prefixes lo hi = prefixes.(hi + 1) - prefixes.(lo)

;;

let p = prefix_sums [3; 1; 4; 1; 5; 9; 2; 6; 5; 3] in
range_sum p 0 4 + range_sum p 5 9 + range_sum p 2 7
