#BS0000178. Khoảng cách gần thứ K (K-th Nearest)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 fixed points on a number line with integer coordinates . Answer independent queries.
In query , you are given the integer coordinate of point and an integer . The distance from to is . Sort all distances in nondecreasing order, including ties, and output the value in one-based position .
Different fixed points may share a coordinate. They are still separate points and all count when determining the rank.
Input
- The first line contains and , the number of fixed points and queries.
- The second line contains exactly integers in arbitrary order.
- Each of the following lines contains , the query coordinate and desired distance rank.
Output
Print exactly lines, each containing the integer distance for its corresponding query.
Subtasks
-
Subtask 1 (20 points): ; for every ; và for every ; coordinates may be equal.
-
Subtask 2 (30 points): ; for every ; và for every ; coordinates may be equal.
-
Subtask 3 (50 points): ; for every ; và for every ; 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 . For , the distances are , so the third is . For , they are , sorted as , so the first is . For , they are , sorted as , so the fourth is .
Example 2
Input
2 2
0 0
0 1
0 2
Output
0
0
Explanation
Both and have coordinate , but they are two separate points. Their distances to are both , so the first and second closest distances are both 0.