Under construction

Still being built. Things may change

Counting Haybales

Interview

Time limit2sMemory limit512 MB

Summary
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 NN haybales (1≤N≤100 0001 \le N \le 100\,000) at various points along the one-dimensional road that runs across his farm. To confirm that the spacing is right, he needs answers to QQ queries (1≤Q≤100 0001 \le Q \le 100\,000). Each query asks how many haybales lie within a given interval of the road.

Input

The first line contains NN and QQ.

The second line contains NN distinct integers, each in the range 00 to 1 000 000 0001\,000\,000\,000. A haybale sits at each of those positions.

Each of the next QQ lines contains two integers AA and BB (0≤A≤B≤1 000 000 0000 \le A \le B \le 1\,000\,000\,000), asking for the number of haybales whose position is between AA and BB, inclusive.

Output

Print QQ lines. For each query, in input order, print the number of haybales in its interval on its own line.

Examples5

  1. Example 1

    Input
    4 6
    3 2 7 5
    2 3
    2 4
    2 5
    2 7
    4 6
    8 10
    
    Expected output
    2
    2
    3
    4
    1
    0
    
  2. Example 2

    Input
    1 1
    0
    0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    1 3
    1000000000
    0 999999999
    1000000000 1000000000
    0 1000000000
    
    Expected output
    0
    1
    1
    
  4. Example 4

    Input
    5 8
    0 2 4 6 1000000000
    0 0
    1 1
    2 2
    3 3
    6 6
    999999999 999999999
    1000000000 1000000000
    0 1000000000
    
    Expected output
    1
    0
    1
    0
    1
    0
    1
    5
    
  5. Example 5

    Input
    6 5
    10 20 30 40 50 60
    0 9
    61 1000000000
    11 19
    10 60
    35 45
    
    Expected output
    0
    0
    0
    6
    1