The problem closely resembles a codechef problem: https://www.codechef.com/NOV18B/problems/HMAPPY1
Given you were talking about points in the other thread, I'm not going to help you with this, OP, except to say that you're approaching the problem incorrectly. Just reading the input is O(N), but you can perform the queries themselves faster than linear time.