P16862 [GKS 2021 #G] Staying Hydrated

Description

With online classes in full swing, it is important for Grace to take breaks and keep herself hydrated at all times. She has decided to place a water bottle in her room in the most convenient place. This means that the position of this water bottle should be close to all the places in the room where she generally hangs out like the study desk, bed and coffee table among other places. The room is represented in the form of a coordinate plane. The number of steps Grace needs to go from Point $A$ to Point $B$ is equal to the Manhattan distance between the $2$ points. This means, Grace can only walk parallel to the axes of the coordinate plane and with each step, she can move $1$ unit in either of the $4$ directions. Can you help her find a position in the room to keep the bottle, such that the sum of steps from the bottle to all her favourite furniture pieces will be minimum? **Notes:** - All the furniture (like study desk, bed, or coffee table) can be represented as rectangles of non-$0$ area in the plane with edges parallel to the axes. - It is possible for furniture pieces to overlap, as she likes to work on her bed-table too. - Assume that Grace can simply pass through the furniture while walking and does not need to go around them.

Input Format

The first line of the input gives the number of test cases, $T$. $T$ test cases follow. The first line of each test case contains an integer $K$ which represents the number of objects in Grace's room. $K$ lines follow, each of them describing $1$ object. The $i$-th line contains $4$ integers, $x_{i,1}$, $y_{i,1}$, $x_{i,2}$, $y_{i,2}$, where $(x_{i,1}, y_{i,1})$ represents coordinates of the bottom left corner and $(x_{i,2}, y_{i,2})$ represents coordinates of the top right corner of the $i$-th rectangular object.

Output Format

For each test case, output $1$ line containing Case #$i$: $x$ $y$, where $i$ is the test case number (starting from $1$) and $x$ and $y$ are coordinates of the water bottle such that the sum of steps from these coordinates to all the furniture pieces will be minimum. Note, the bottle can lie on the floor or on top of any furniture but should be placed on integer coordinates only. If multiple solutions exist, output the solution with minimum $x$ coordinate; if multiple solutions have the same $x$ coordinate, output the solution with minimum $y$ coordinate.

Explanation/Hint

:::align{center} ![](https://cdn.luogu.com.cn/upload/image_hosting/v2z283jx.png) ::: In Sample Case #$1$, Grace can place the bottle at coordinates $(1, 3)$. It is at a distance of $2$ steps from the $1$st object, $1$ step from the $2$nd, and $0$ steps from the $3$rd one, which gives us $3$ as the minimum possible sum of steps from a point. In Sample Case #$2$, the water bottle can lie anywhere on the object itself, but coordinates $(0, 0)$ correspond to the minimum $x$ and $y$ coordinates. ### Limits $1 \le T \le 100$. $x_{i,1} < x_{i,2}$, for all $i$. $y_{i,1} < y_{i,2}$, for all $i$. **Test Set $1$** $1 \le K \le 20$. $-100 \le x_{i,1}, x_{i,2}, y_{i,1}, y_{i,2} \le 100$, for all $i$. **Test Set $2$** $1 \le K \le 10^5$. $-10^9 \le x_{i,1}, x_{i,2}, y_{i,1}, y_{i,2} \le 10^9$, for all $i$.