Help in DQUERY [SPOJ , BIT+MO]

Question : “https://www.spoj.com/problems/DQUERY

My Solution:“https://www.ideone.com/TgvC5z

I tried more than 7 times since last night , unable to get AC , don’t know where it goes wrong .

Help @spaanse @galencolin @aneee004 @everule1