Counting Haybales
InterviewTime limit2sMemory limit512 MB
Given N distinct haybale positions and Q interval queries, count how many positions fall inside each inclusive range [A, B].
- Level
Medium4 of 10
- Topics
- Sorting, Binary search, Array, Prefix sum
- Solved
- No attempts yet
Problem
Farmer John placed his haybales () at various points along the one-dimensional road that runs across his farm. To confirm that the spacing is right, he needs answers to queries (). Each query asks how many haybales lie within a given interval of the road.
Input
The first line contains and .
The second line contains distinct integers, each in the range to . A haybale sits at each of those positions.
Each of the next lines contains two integers and (), asking for the number of haybales whose position is between and , inclusive.
Output
Print lines. For each query, in input order, print the number of haybales in its interval on its own line.