Here is the problem link and problem statement is pretty short you can have a go at it.

I was able to get 30 points on this question and then I thought if there is a way to order the queries to reduce time complexity but wasn't able to come up with anything.

Even though the editorial is pretty clear I am not able to understand it, Can someone share their approach on how to solve this problem.