Problem B
Flappy Bird
Help the bird Faby to navigate through a sequence of $n$ pairs of pipes, by finding the shortest line he can fly on to reach his destination. For simplicity, we represent Faby as a single point in the plane and assume every pipe has width zero. This way, the gap between every pair of pipes can be represented as an interval on the $y$ axis. The bird starts out at $s = (x_s, y_s)$ and his goal is to reach $t = (x_t, y_t)$. Find the shortest line from $s$ to $t$, passing through all intervals in between in increasing order of their $x$ coordinates.
![\includegraphics[width=0.7\textwidth ]{sample2}](/problems/flappy/file/statement/en/img-0001.png)
Input
The input consists of:
-
One line with four integers $x_s, y_s, x_t$ and $y_t$ ($-10^9 \le x_s, y_s, x_t, y_t \le 10^9$), the start and end points.
-
One line with an integer $n\ (0 \le n \le 10^6)$, the number of intervals.
-
$n$ lines, the $i$th of which contains three integer $x_i, y_{i,1}$ and $y_{i,2}$ ($-10^9 \le x_i, y_{i, 1}, y_{i, 2} \le 10^9$, $y_{i, 1} < y_{i, 2}$), the intervals.
It can be assumed that $x_s < x_1 < \dots < x_n < x_t$.
Output
Output a sequence of $k$ $(2 \le k \le n+2$) points $p_1, \dots , p_k$, one per line, such that:
-
All points have integer coordinates.
-
$p_1 = s$ and $p_k = t$.
-
Let $P$ be the path obtained by connecting $\overline{p_i p_{i+1}}$ for all $1 \le i < k$. Then:
-
$P$ passes through all intervals in increasing order of their $x$ coordinates.
-
The length of $P$ is minimal.
-
If there are multiple valid solutions, you may output any one of them.
Sample Input 1 | Sample Output 1 |
---|---|
0 0 10 0 1 5 -10 10 |
0 0 10 0 |
Sample Input 2 | Sample Output 2 |
---|---|
0 0 10 0 4 2 1 3 4 2 3 7 0 2 9 -2 -1 |
0 0 4 2 9 -1 10 0 |