Segment Trees

www.spoj.com/problems/KGSS