Find optimised solution

In ques 2 I am facing a problem regarding the optimised solution of q query. Can you help me figure it out?
this is my code

Hi,
It is not a fair deal to ask long challenges doubt, according to codechefs guidelines its unfair to ask for help, these challenges are meant to exercise your brain.
I hope u understand
Any request for help after the contest end will be appreciated.