Could someone give a "for dummies" version of explanation of code for the problem SEGMENTQ?

I came across this problem:

https://discuss.codechef.com/questions/90675/segmentq-editorial

I understood the concept. But I am not able to understand the implementation, primarily because I am relatively new to Segment Trees. Can someone give clear, commented code that they wrote to solve the same problem and also explain the same?

Here’s the video I watched to understand Segment Trees

Here’s my code

Tried to keep it simple.

And I guess the video is itself really very great. It will clear your doubts regarding the implementation part.