Railway
Time limit2sMemory limit512 MB
Given tunnels on a single-track line and two sets of departure times in opposite directions, decide whether any two trains meet strictly inside a tunnel.
- Level
Medium7 of 10
- Topics
- Math, Binary search, Sorting, Geometry
- Solved
- No attempts yet
Problem
There is a railway between Zürich and Lugano of length kilometers. The railway crosses the beautiful Alps, resulting in a spectacular scenery during the ride. Since some passes are too high for the railway, there are tunnels on the track. The -th of them starts kilometers from Zürich and ends kilometers from Zürich. (Thus, the length of the -th tunnel is .)
You have a timetable of the rail service between the two cities. There are services from Zürich to Lugano, the -th of which departs at minutes, and services from Lugano to Zürich, the -th of which departs at minutes. All trains operating on the track have a constant speed of kilometer per minute, regardless of their direction and whether they are in a tunnel or not. There are no stations on the route, and the trains never stop at semaphores. Hence, each service arrives to its destination in exactly minutes.
The length of a train is negligible in comparison to the length of the railway, so in this problem please assume that each train is a point that moves along the railway.
Usually, the railway has two tracks: one in each direction. The only exception are the tunnels. Each tunnel has just a single track that can be used in either direction.
Whenever two trains going in the opposite directions meet outside a tunnel, they can pass each other safely. This includes trains meeting exactly at either end of a tunnel. On the other hand, if a pair of trains meets strictly inside a tunnel, there is a collision.
Given the description of the tunnels and the train services, determine whether there will be any collision.
Input
The first line contains four space-separated integers , , , (, , ) — the length of the track, the number of tunnels, the number of services from Zürich and the number of services from Lugano, respectively.
The second line contains space-separated integers () — the starting positions of the tunnels.
The third line contains space-separated integers () — the ending positions of the tunnels.
For each between and , holds. Additionally, for each between and , . (In other words, each tunnel has a positive length, the tunnels are pairwise disjoint, and they are given in increasing order of distance from Zürich.)
The fourth line contains space-separated integers () — the starting times (in minutes) of the services starting in Zürich. The times are given in increasing order, that is, for all valid .
The fifth line contains space-separated integers () — the starting times (in minutes) of the services starting in Lugano. The times are given in increasing order, that is, for all valid .
Output
Output a single line, containing "YES" (quotes for clarity) if at least one crash occurs, or "NO" if all trains reach their destination safely.
Constraints
In all subtasks except the last one, the value of and all and are even.
Hint
In the first example there are two tunnels on a track of length kilometers: one to kilometers from Zürich, the other to kilometers from Zürich. The only train coming from Zürich manages to avoid all the Lugano services as follows:
- the first is met kilometers from Zürich,
- the second is met halfway between the tunnels,
- the third is met kilometers from Lugano,
- the fourth starts long after the Zürich train had arrived at its destination.
In the second example the only two trains meet exactly in the middle of the only tunnel, resulting in a crash.
In the third example the two trains meet exactly at the end of the tunnel that is closer to Zürich. In the fourth example they meet exactly at the other end of the tunnel. Both cases are fine, the trains pass each other and reach their destination safely.