This problem can be solved in a few ways:
For easy sub-task, a simple for loop checking the query limit would suffice.
Concept of bucket sort can be used to store the indices of occurrences of each number in separate vectors and perform a binary search on sorted array when a query with the specific number comes.
The given array can be discomposed into square root N number of segments where N is the size of the array and then keep a map for each of the discomposed chunks.
Queries can be taken at once and the answers can be generated offline. Please refer to “SPOJ Dquery” for a similar test.
It can solved using Merge Sort Tree, although the time advantage may not be required for this case.