7025.Nearest Campsites II

Time Limit: 1s Memory Limit: 512MB

A campground is represented as a grid where each square can contain a campsite that is either reserved or free. The distance between two squares $(x_1, y_1)$ and $(x_2, y_2)$ is the Manhattan distance $|x_1 - x_2| + |y_1 - y_2|$ .

Your task is to find the distance from each free campsite to the nearest reserved campsite.

Input Format(From the terminal/stdin)

The first line has two integers $n$ and $m$ :
the number of reserved and free campsites.

The next $n$ lines describe the __cpLocations of the reserved campsites. Each line has two integers $x$ and $y$ .

The next $m$ lines describe the __cpLocations of the free campsites. Each line has two integers $x$ and $y$ .

You can assume that each square contains at most one campsite.

  • $1 \le n, m \le 10^5$
  • $1 \le x, y \le 10^6$

Output Format(To the terminal/stdout)

Print $m$ integers: the distances from each free campsite to the nearest reserved campsite, in the input order.

Sample Input

Copy
4 2
1 1
5 2
2 6
4 7
1 3
7 5
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
2 5
 · \n

The distance from the first free campsite (on the left) to the nearest reserved campsite is $2$ , and the distance from the second free campsite (on the right) to the nearest reserved campsite is $5$ .

Source: CSES, Additional Problems I, 3307

Submit

请先 登录

© 2025 FAQs