There are many 2D rectangles in 2D space. Given a point, suppose this point did not hit (inside of) any rectangle, find the one rectangle it is closest to. Assume the rectangles are not rotated. Can it be faster than O(n)?

# | User | Rating |
---|---|---|

1 | tourist | 3434 |

2 | fateice | 3337 |

3 | Um_nik | 3292 |

4 | OO0OOO00O0OOO0O0…O | 3280 |

5 | Syloviaely | 3274 |

6 | Petr | 3223 |

7 | Swistakk | 3105 |

8 | mnbvmar | 3096 |

9 | yosupo | 3091 |

10 | dotorya | 3081 |

# | User | Contrib. |
---|---|---|

1 | rng_58 | 163 |

2 | tourist | 160 |

3 | csacademy | 152 |

4 | Petr | 151 |

5 | Swistakk | 148 |

6 | Um_nik | 144 |

7 | Vovuh | 142 |

7 | Nickolas | 142 |

9 | BledDest | 138 |

9 | PikMike | 138 |

9 | matthew99 | 138 |

There are many 2D rectangles in 2D space. Given a point, suppose this point did not hit (inside of) any rectangle, find the one rectangle it is closest to. Assume the rectangles are not rotated. Can it be faster than O(n)?

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jul/23/2018 16:44:00 (d1).

Desktop version, switch to mobile version.

User lists

Name |
---|

I will kinda gloss over technical details for now, feel free to ask for clarifications.

I suppose that you want to answer queries of type "What is the closest rectangle to this point?" in less than

O(n) time. Now shortest distance from the pointPto the rectangle is the shortest distance from thePto one of its sides. The shortest distance fromPto the segmentABis eithermin(|PA|, |PB|) or length of perpendicular fromPtowardsAB, if it lands on the segmentABof course.Now, let's see which case actually happens for rectangle, which is the closest to the point

P. Dealing with second case is simple: we just need to cast 4 rays in all coordinate directions from the pointP, for each ray find the first rectangle they intersect and remin answer with this rectangles (note that because all rectangles are axis-parrallel, all perpendiculars to the sides are axis-parallel too). To deal with the first case, you need to find the closest point toPamong the corners of rectangles: in general, such a problem can be solved with Voronoi diagram (and I am pretty sure that there is not anything significantly simplier in general case), in your case there may be some simpler solution, but I can't see it for now.Total time: precalculation (for Voronoi diagram), per query. It seems that you need something like a persistent treap to answer queries online, but offline you can do a scanline with set to handle both described cases (after you built the Voronoi diagram, of course)).