Counting in a Range

easy binary-search sorting arrays 1000 ms · 256 MB

Counting in a Range

You are given an array of n integers a_1, a_2, …, a_n, in no particular order. Values may repeat.

Answer q queries. Each query gives two integers l and r and asks: how many indices i satisfy l ≤ a_i ≤ r? Both ends of the range are inclusive. The array never changes between queries.

A query may have l > r; then no value lies in the range and the answer is 0.

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·10^5, 1 ≤ q ≤ 2·10^5).

The second line contains n integers a_1, a_2, …, a_n (−10^9 ≤ a_i ≤ 10^9).

Each of the next q lines contains two integers l and r (−2·10^9 ≤ l, r ≤ 2·10^9).

Output

Print q lines. The j-th line must contain the answer to the j-th query: the number of indices i with l ≤ a_i ≤ r.

Notes

In the first example the array is [5, 1, 9, 3, 7, 3]. The values in [3, 7] are 5, 3, 7, 3, so the first answer is 4. No element lies in [4, 4], all six lie in [-10, 100], and only the 9 lies in [9, 9].

In the second example the last query has l > r, so its answer is 0.

Example 1

Input

6 4
5 1 9 3 7 3
3 7
4 4
-10 100
9 9

Output

4
0
6
1

Example 2

Input

5 5
-2 -2 0 -2 4
-2 -2
-3 -1
0 4
5 1000000000
4 -2

Output

3
3
2
0
0

Hints · reveal one at a time if you're stuck

Hint 1

The order of the array does not matter for these queries. What does the set of values lying between l and r look like if you sort the array first?

Hint 2

In a sorted array the values in any range form one contiguous block, whose two ends you can find with binary searches. Be careful that both ends are inclusive, and that a query with l > r must give 0.

report an issue Prefer your own editor? sooks prep 40 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.