Snapping Seeks to Keyframes
You are working on a video player. A compressed video can only start decoding at a keyframe, so when the user drags the progress bar to some time, the player jumps to the keyframe closest to that time instead.
You are given the timestamps of the video's n keyframes in milliseconds, in increasing order, and
q seek requests. For each request at time t, find the keyframe k that minimizes |t − k|. If
two keyframes are equally close (one before t and one after it), choose the earlier one.
A request may lie before the first keyframe or after the last one.
Input
The first line contains two integers n and q (1 ≤ n, q ≤ 2·10^5): the number of keyframes and
the number of seek requests.
The second line contains n integers k_1, k_2, …, k_n (0 ≤ k_1 < k_2 < … < k_n ≤ 10^9): the
keyframe timestamps in milliseconds, strictly increasing.
Each of the next q lines contains one integer t (0 ≤ t ≤ 10^9): the time of a seek request in
milliseconds.
Output
For each request, in input order, print one line with two integers: the timestamp of the chosen
keyframe and its distance |t − k| from the request.
Notes
In the first example, the request at 3500 is exactly halfway between the keyframes at 2000 and 5000, so the earlier one, 2000, is chosen. The request at 7000 is a keyframe itself, and 9999 lies after the last keyframe.
In the second example there is only one keyframe, so every request snaps to it.
With n and q up to 2·10^5, scanning all keyframes for every request is too slow.