Well, I just tried my hands on the segment_sum. You can check if out here: https://gist.github.com/bbrighttaer/207dc03b178bbd0fef8d1c0c1390d4be 116
You can let me know about its correctness after your verification.
Thanks.