#P7181. Maximizing Rectangle Intersections
Maximizing Rectangle Intersections
Maximizing Rectangle Intersections
Given a 2D plane with n axis-aligned rectangles, you are to choose a point B from one of two segments such that the line segment AB intersects as many rectangles as possible. Point A is fixed at \((0,0)\). Additionally, let \(C=(x_{\max},0)\), \(D=(0,y_{\max})\) and \(E=(x_{\max},y_{\max})\). The candidate point B must lie on either the segment \(CE\) or the segment \(DE\) (i.e. the vertical segment from \(C\) to \(E\) or the horizontal segment from \(D\) to \(E\)).
The vertices of the rectangles are given by non-negative integers, with \(x\) coordinates not exceeding \(x_{\max}\) and \(y\) coordinates not exceeding \(y_{\max}\). Note that the rectangles may overlap, coincide, or be completely separate. The intersection is defined in a generous way: even if the line \(AB\) just touches a rectangle at a single vertex, it counts as an intersection.
Your task is to select a valid point \(B\) so that the number of rectangles intersected by the segment \(AB\) is maximized. If there are multiple optimal choices, you may output any one of them.
Input Format:
- The first line contains three space-separated integers: \(n\), \(x_{\max}\), and \(y_{\max}\).
- Then follow \(n\) lines, each containing four space-separated integers: \(x_1\), \(y_1\), \(x_2\), \(y_2\), representing a rectangle with bottom-left vertex \((x_1,y_1)\) and top-right vertex \((x_2,y_2)\) (where \(x_1 < x_2\) and \(y_1 < y_2\)).
Output Format:
- Output two space-separated integers \(B_x\) and \(B_y\), the coordinates of the chosen point \(B\).
- In the next line, output a single integer representing the maximum number of rectangles that the line segment \(AB\) intersects.
Note: A point \(P=(x,y)\) is considered inside a rectangle if \(x_1 \le x \le x_2\) and \(y_1 \le y \le y_2\). The line segment \(AB\) is defined by the parametric equation \((t\cdot B_x, t\cdot B_y)\) for \(t\) in \([0,1]\).
Hint for Solving: Since the candidate \(B\) points lie on only two line segments (vertical or horizontal), you can iterate over all integer points on these segments and check for each how many rectangles the corresponding line \(AB\) intersects.
inputFormat
The first line contains three integers: \(n\), \(x_{\max}\), and \(y_{\max}\). The next \(n\) lines each contain four integers \(x_1, y_1, x_2, y_2\), representing the coordinates of a rectangle.
outputFormat
Output two lines. The first line contains the coordinates of the chosen point \(B\) (\(B_x\) and \(B_y\)) separated by a space, and the second line contains the maximum number of rectangles that the segment \(AB\) intersects.
sample
3 10 10
1 1 3 3
2 2 8 8
0 5 5 10
10 10
3
</p>