problems / 246

Suki's Shortest Scent Trail

medium intervals heap sorting sweep-line 1000 ms · 256 MB

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.

Example 1

Input

4 6
1 10
5 100
3 4
8 8
7 4 8 2 101 100

Output

10 2 1 10 -1 96

Example 2

Input

2 4
1 1000000000
999999999 1000000000
1000000000 1 999999998 999999999

Output

2 1000000000 1000000000 2

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

Hint 1

The balls don't have to be answered in the order they are given. What would be easier if you looked at them from left to right?

Hint 2

Sweep the balls by increasing position. When you reach a ball at x, which trails have become relevant, and which trails can never matter again for this or any later ball?

Hint 3

Keep the started trails in a min-heap keyed by length. A trail that ended before the current ball only needs to be thrown away when it reaches the top of the heap.

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

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