Suki's Shortest Scent Trail
Suki, the curly black dog with the grey beard and white paws, has found a pile of pink balls
scattered along the park's long straight footpath. The footpath is marked with whole-number
positions, and the morning's dogs have left n scent trails on it: trail i covers every
position from l_i to r_i, ends included, so its length is r_i − l_i + 1. Trails may
overlap, nest inside each other, or be exact copies.
For each of the q balls, lying at position p_j, Suki wants to sniff her way along the
shortest trail that passes over that spot. Help her: for every ball, print the length of the
shortest trail with l_i ≤ p_j ≤ r_i, or -1 if no trail covers its position.
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 2·10^5): the number of trails and
the number of balls.
Each of the next n lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^9).
The last line contains q integers p_1, p_2, …, p_q (1 ≤ p_j ≤ 10^9), the positions of the
balls. Several balls may lie at the same position.
Output
Print q integers separated by spaces on one line: the j-th is the length of the shortest trail
covering p_j, or -1 if there is none. Answers must be in the same order as the balls in the
input.
Notes
In the first example, the ball at 7 lies on trails [1, 10] (length 10) and [5, 100]
(length 96), so the answer is 10, even though [5, 100] starts closer to the ball. The ball at 8
also lies on the one-spot trail [8, 8], of length 1. No trail reaches position 101, while
position 100 is still covered by [5, 100], because trails include both ends.