PROBLEM LINK:Practice Setter: Noszály Áron DIFFICULTY:Medium PREREQUISITES:Observation would do. Manhattan distance and basic Math. PROBLEM:Using at most $Q$ queries, we need to identify the hidden rectangle chosen by Chef in a square grid of width and height $10^9$ each. In each query, we give a point $P(x, y)$ and chef tells us minimum Manhattan distance from point P to any point inside the rectangle. For full points, we have $Q = 7$ (excluding printing the hidden rectangle). $q(x, y)$ denote query for point $(x, y)$. Let hidden rectangle be $(x1, y1, x2, y2)$ (Rectangle with lower left corner at $(x1, y1)$ and upper right corner at $(x2, y2)$. QUICK EXPLANATION
EXPLANATIONFor the first subtask, we can simply query each possible point $(x, y)$ for $0 \leq x, y \leq 100$ in total $101^2$ queries and determine the hidden rectangle, as all points inside or on the border of rectangle shall have Minimum Manhattan distance zero. For subtask two, we have $70$ queries which are approximately $2*log_2(10^9)$ which hints towards a binary search solution using two binary searches or similar. Indeed, The original setter solution was this binary search solution. See the image below. Let us rewrite query answer as per our problem. Here, for Point $P(x, y)$ and hidden rectangle $(x1,y1,x2,y2)$, query gives $x1x$ if $x < x1$, $0$ if $x1 \leq x \leq x2$ and $xx2$ if $x > x2$ as steps moved parallel to line OX. Similar cases hold for y coordinate. Now, The red region denotes the hidden rectangle chosen by the chef. It can be seen that querying in this region is useless since the answer to each query shall be zero here. The green region is the most preferable region since, in this region, either the x component or the y component of Manhattan distance shall be zero, thus providing us with the value of the other component of Manhattan distance. This way, Once we find any value $x$ such that $x1 \leq x \leq x2$ (or similar for y coordinate), we can use two queries $(x, 0)$ and $(x, INF)$ to determine the values of $y1$ and $y2$ which can be substituted back so as to determine $x1$ and $x2$. This is where binary search comes in. We run binary search twice, Once for $x1$ and then for $x2$. See how it works in hidden box. Now, we have seen how to solve this problem by binary search, but need to solve it in even lesser queries. Now, Let's query Two nonopposite corners, say $(0,0)$ and $(10^9,0)$. It can be seen that $q(0,0) = x1+y1$ and $q(10^9,0) = 10^9x2+y1$. By eliminating $y1$, we get $x1+x2 = q(0,0)+10^9q(10^9,0)$. Here comes the interesting part. We were earlier finding any point of form $(x, 0)$ using binary search. We know, that average of two numbers always lie between the two numbers. Hence, $x$ = average of $x1$ and $x2$ = $(x1+x2)/2$. This implies that $x1 \leq x \leq x2$. Hence, Point $(x, 0)$ lies in green region. We can easily find $y1 = q(x, 0)$ and $y2 = 10^9q(x, 10^9)$. So, we have managed to solve the problem in four queries. For anyone still facing a problem, refer the box. Time ComplexityTime complexity is $O(1)$ per test case. AUTHOR'S AND TESTER'S SOLUTIONS:Setter's solution Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)
This question is marked "community wiki".
asked 14 Feb, 00:10

@admin Can you post code with binary search for subtask2? Also not able to access solutions posted here. answered 17 Feb, 14:49
Even I want to know how binary search works here. It would be good if someone posts an explanation.
(17 Feb, 15:54)
Testers binary search solution https://ideone.com/MR3WdX
(19 Feb, 12:53)

Only 4 equations are necessary for finding out the secret rectangle answered 18 Feb, 23:21
Thanks @rajput1999, your handwritten notes was very helpful for me. I wanted to upvote but due to less reputation couldn't.
(22 Feb, 08:47)

Hello please review my code or please let me know for which test case it is failinghttps://www.codechef.com/viewsolution/23066914import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main {
} ============================================================================ answered 22 Feb, 16:59
Please tend to share the submission link instead of entire code if the code is long.
(22 Feb, 18:54)

Solutions' links not working, as usual.