PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000178. Khoảng cách gần thứ K (K-th Nearest)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khoảng cách gần thứ K (K-th Nearest)

K-th Nearest

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN fixed points A1,A2,…,ANA_1,A_2,\ldots,A_N on a number line with integer coordinates a1,a2,…,aNa_1,a_2,\ldots,a_N. Answer QQ independent queries.

In query jj, you are given the integer coordinate bjb_j of point BjB_j and an integer kjk_j. The distance from AiA_i to BjB_j is ∣ai−bj∣|a_i-b_j|. Sort all NN distances in nondecreasing order, including ties, and output the value in one-based position kjk_j.

Different fixed points may share a coordinate. They are still separate points and all count when determining the rank.

Input

  • The first line contains NN and QQ, the number of fixed points and queries.
  • The second line contains exactly NN integers a1,a2,…,aNa_1,a_2,\ldots,a_N in arbitrary order.
  • Each of the following QQ lines contains bj,kjb_j,k_j, the query coordinate and desired distance rank.

Output

Print exactly QQ lines, each containing the integer distance for its corresponding query.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤201\le N,Q\le 20; −1000≤ai≤1000-1000\le a_i\le 1000 for every 1≤i≤N1\le i\le N; −1000≤bj≤1000-1000\le b_j\le 1000 và 1≤kj≤N1\le k_j\le N for every 1≤j≤Q1\le j\le Q; coordinates may be equal.

  • Subtask 2 (30 points): 1≤N,Q≤20001\le N,Q\le 2000; −1000000≤ai≤1000000-1000000\le a_i\le 1000000 for every 1≤i≤N1\le i\le N; −1000000≤bj≤1000000-1000000\le b_j\le 1000000 và 1≤kj≤N1\le k_j\le N for every 1≤j≤Q1\le j\le Q; coordinates may be equal.

  • Subtask 3 (50 points): 1≤N,Q≤1000001\le N,Q\le 100000; −100000000≤ai≤100000000-100000000\le a_i\le 100000000 for every 1≤i≤N1\le i\le N; −100000000≤bj≤100000000-100000000\le b_j\le 100000000 và 1≤kj≤N1\le k_j\le N for every 1≤j≤Q1\le j\le Q; coordinates may be equal.

Examples

Example 1

Input

4 3
-3 -1 5 6
-2 3
2 1
10 4

Output

7
3
13

Explanation

The fixed coordinates are −3,−1,5,6-3,-1,5,6. For (−2,3)(-2,3), the distances are 1,1,7,81,1,7,8, so the third is 77. For (2,1)(2,1), they are 5,3,3,45,3,3,4, sorted as 3,3,4,53,3,4,5, so the first is 33. For (10,4)(10,4), they are 13,11,5,413,11,5,4, sorted as 4,5,11,134,5,11,13, so the fourth is 1313.

Example 2

Input

2 2
0 0
0 1
0 2

Output

0
0

Explanation

Both A1A_1 and A2A_2 have coordinate 00, but they are two separate points. Their distances to B=0B=0 are both 00, so the first and second closest distances are both 0.