SPOJ - HORRIBLE WA

Hey there. I am getting wrong answer in spoj horrible queries. Here is the link to the problem. [http://www.spoj.com/problems/HORRIBLE/][1]. And the link to my code. [http://ideone.com/pVIWNd][2].

Please help!
[1]: http://
[2]: http://ideone.com/pVIWNd

I found this useful
link

I hope u will find it useful too.

Fenwick Tree could be useful