Spoj Problem - FREQUENT

Please tell me - How to slove this Spoj Problem using segment tree?
I have tried some times but not understood how to solve that problem.