#P9314. Train Collision on Alpine Railway
Train Collision on Alpine Railway
Train Collision on Alpine Railway
There is a railway connecting Zurich and Lugano with a length of \(s\) kilometers. The railway passes through the beautiful Alps, offering spectacular scenery during the journey. However, due to high mountain passes, \(t\) tunnels have been built along the route. The \(i\)th tunnel starts at \(a_i\) kilometers from Zurich and ends at \(b_i\) kilometers, so that its length is \(b_i - a_i\).
You are given a train timetable between the two cities. There are \(m\) trains departing from Zurich toward Lugano at times \(c_j\) minutes, and \(n\) trains departing from Lugano toward Zurich at times \(d_k\) minutes. All trains run at a constant speed of \(1\) kilometer per minute and take exactly \(s\) minutes to complete the trip. Assume that each train is a point moving along the railway.
The railway has two tracks for opposite directions except inside tunnels where there is only one track (which can be used in either direction). At any time, when two trains traveling in opposite directions meet outside a tunnel (including exactly at the tunnel endpoints), they can safely pass by each other. However, if they meet strictly inside a tunnel (i.e. not at an endpoint), a collision will occur.
Let a train departing from Zurich at time \(c\) and a train from Lugano at time \(d\) have positions given by:
[ \text{Position from Zurich: } x = t - c, \quad \text{for } c \le t \le c+s,] [ \text{Position from Lugano: } x = s - (t - d), \quad \text{for } d \le t \le d+s.]
They meet when \(x\) satisfies \(t - c = s - (t - d)\); solving, we obtain
[ t = \frac{s + c + d}{2} \quad \text{and} \quad x = \frac{s + d - c}{2}.]
Note that the two trains are both on the track if \(|c - d| \le s\). A collision happens if for any pair of trains (one from Zurich and one from Lugano) the meeting point \(x\) satisfies \(a_i < x < b_i\) for some tunnel \(i\).
Your task is to determine whether any collision will occur.
inputFormat
The first line contains four integers \(s\), \(t\), \(m\), and \(n\): the length of the railway (in kilometers), the number of tunnels, and the number of trains departing from Zurich and Lugano respectively.
The next \(t\) lines each contain two integers \(a_i\) and \(b_i\) representing the start and end positions of the \(i\)th tunnel.
The following line contains \(m\) integers \(c_1, c_2, \ldots, c_m\) denoting the departure times (in minutes) of trains from Zurich.
The last line contains \(n\) integers \(d_1, d_2, \ldots, d_n\) denoting the departure times (in minutes) of trains from Lugano.
outputFormat
Output a single line: Yes
if a collision occurs, and No
otherwise.
sample
10 1 1 1
3 7
0
2
Yes