Dijkstra Is Playing At My House
Time limit3sMemory limit1024 MB
Find the shortest Manhattan path from start to house around up to 250,000 non-overlapping axis-parallel rectangles, without entering any rectangle interior.
- Level
Hard9 of 10
- Topics
- Shortest path, Segment tree, Sorting, Geometry
- Solved
- No attempts yet
Problem
To celebrate your team's victory at ICPC World Finals, Edsger W. Dijkstra (the inventor and namesake of Dijkstra's algorithm) will throw a fabulous party at your house in New York City. The party starts in 4 hours, so he should start moving soon.
New York City is modeled as a 2-dimensional plane. Dijkstra is at coordinate , and your house is at . He can only move in directions parallel to the coordinate axes (you remember the Manhattan distance, right?). There are skyscrapers, each an axis-parallel rectangle. He may pass through a skyscraper's boundary, but he cannot pass through any point strictly inside one.
Dijkstra called you because computing the shortest path between his location and your house is too hard for him. He seems to be losing his edge. That is not bad news. It is your chance to impress the great Dijkstra. Can you?
All x coordinates are distinct, and all y coordinates are distinct. No pair of rectangles overlaps. Your house and Dijkstra's starting location are not inside any rectangle.
Input
The first line contains five space-separated integers , , , , .
The -th of the next lines contains four space-separated integers , , , . This means the -th skyscraper is a rectangle with corners at , , , and .
Output
Print the length of the shortest path between Dijkstra's location and your house, measured in Manhattan distance.
Constraints
- ()
- ()
- Let . All elements in are distinct.
- Let . All elements in are distinct.
- No pair of rectangles shares a common point.
- Dijkstra's location and your house are not in any rectangle.