Cyber Donut Crime Investigation
Time limit5sMemory limit128 MB
For each query point find the database point minimizing the L1 distance, over many test cases with up to 100000 points and 50000 queries.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Binary search, Sorting, Geometry
- Solved
- No attempts yet
Problem
By the year 2042, the internet has advanced far enough to create virtual reality, and cyber crimes happen every day. To fight them, the winner of the 2041 SWERC contest built an agent that drops a single donut at the scene whenever a cyber crime occurs. Every donut carries a unique id, and the Madrid Police Department keeps a large database that stores each crime together with the unique id of the donut left at its scene.
Today is your day. Your mission is to develop a new agent that reads the records stored in the database and finds the donut most similar to the one at a new crime scene.
Virtual-criminology experts proposed the criterion for judging how similar two donuts are: their similarity is the absolute difference of their hole radii plus the absolute difference of their outer radii. That is, writing for the hole radius and for the outer radius, the similarity of two donuts and is . A smaller value means the two donuts are more alike.
Input
The input consists of several test cases.
The first line of each test case contains the number of donuts stored in the database, ().
Each of the next lines contains two integers, the hole radius and the outer radius of one database donut ().
The next line contains , the number of donuts to look up ().
Each of the next lines contains two integers, the hole radius and outer radius of one donut to look up.
Consecutive test cases are separated by a blank line, and a single line containing marks the end of the input.
Output
For each test case, print lines. The -th line must contain, as an integer, the similarity between the -th queried donut and the most similar (smallest-similarity) donut in the database.
The outputs of different test cases must be separated by a single blank line.