DRANGE(TLE)

The question has very strict time linit.I ma getting TLE.

Question Link:- http://www.codechef.com/problems/DRANGE

My Solution :- http://www.codechef.com/viewsolution/6828550

Editorial:- http://discuss.codechef.com/questions/1942/drange-editorial

Here In the editorial there are 2 ways of solving.Though I have solved with The type given at first.
The second type has implemented I think Segment tree concept(I didn’t understand How to solve it).
Plz help me out if it could be solved by the type of first kind

Terima kasih dan salam kenal.
codechef Link

code Link