#abc364d. D - K-th Nearest

D - K-th Nearest

Score : 425425 points

问题陈述

在数轴上有 N+QN+Q 个点 A1,,AN,B1,,BQA_1,\dots,A_N,B_1,\dots,B_Q,其中点 AiA_i 的坐标为 aia_i,点 BjB_j 的坐标为 bjb_j

对于每个 j=1,2,,Qj=1,2,\dots,Q,回答以下问题:

  • XX 是点 A1,A2,,ANA_1,A_2,\dots,A_N 中距离点 BjB_jkjk_j 近的点。找出点 XX 和点 BjB_j 之间的距离。更正式地,设 did_i 为点 AiA_i 和点 BjB_j 之间的距离。将 (d1,d2,,dN)(d_1,d_2,\dots,d_N) 按升序排列得到序列 (d1,d2,,dN)(d_1',d_2',\dots,d_N')。找出 dkjd_{k_j}'

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are N+QN+Q points A1,,AN,B1,,BQA_1,\dots,A_N,B_1,\dots,B_Q on a number line, where point AiA_i has a coordinate aia_i and point BjB_j has a coordinate bjb_j.

For each j=1,2,,Qj=1,2,\dots,Q, answer the following question:

  • Let XX be the point among A1,A2,,ANA_1,A_2,\dots,A_N that is the kjk_j-th closest to point BjB_j. Find the distance between points XX and BjB_j. More formally, let did_i be the distance between points AiA_i and BjB_j. Sort (d1,d2,,dN)(d_1,d_2,\dots,d_N) in ascending order to get the sequence (d1,d2,,dN)(d_1',d_2',\dots,d_N'). Find dkjd_{k_j}'.

Constraints

  • 1N,Q1051 \leq N, Q \leq 10^5
  • 108ai,bj108-10^8 \leq a_i, b_j \leq 10^8
  • 1kjN1 \leq k_j \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN QQ

a1a_1 a2a_2 \dots aNa_N

b1b_1 k1k_1

b2b_2 k2k_2

\vdots

bQb_Q kQk_Q

Output

Print QQ lines. The ll-th line (1lQ)(1 \leq l \leq Q) should contain the answer to the question for j=lj=l as an integer.

Sample Input 1

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

Sample Output 1

7
3
13

Let us explain the first query.

The distances from points A1,A2,A3,A4A_1, A_2, A_3, A_4 to point B1B_1 are 1,1,7,81, 1, 7, 8, respectively, so the 3rd closest to point B1B_1 is point A3A_3. Therefore, print the distance between point A3A_3 and point B1B_1, which is 77.

Sample Input 2

2 2
0 0
0 1
0 2

Sample Output 2

0
0

There may be multiple points with the same coordinates.

Sample Input 3

10 5
-84 -60 -41 -100 8 -8 -52 -62 -61 -76
-52 5
14 4
-2 6
46 2
26 7

Sample Output 3

11
66
59
54
88