서로 겹치지 않는 최대 250,000개의 축 평행 직사각형 장애물이 있는 평면에서 두 점 사이의 맨해튼 최단 경로 길이를 구한다. 장애물의 경계는 지날 수 있다.
어려움9최단 경로그래프정렬기하아직 제출이 없습니다시간 제한3초메모리 제한1024 MBTo 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 better start moving.
New York City is modeled as a 2-dimensional plane. Dijkstra is now in coordinate (s_x, s_y), and your house is located in coordinate (e_x, e_y). Dijkstra should come to your house by only moving in a direction parallel to the coordinate axes (you remember the Manhattan distance, right?). Also, there are N skyscraper in an axis-parallel rectangular shape, which you can pass through its boundary, but cannot pass through anywhere strictly inside of it.
You got a phone call from Dijkstra, saying that it's too hard for him to compute the shortest path between his location and your house. Somehow, he is losing his edge. However, that's not bad news, because it's a chance for you to be cool in front of the great Dijkstra. Can you?
It is guaranteed that all x coordinates are distinct and all y coordinates are distinct. It is also guaranteed that no pair of rectangles overlap. It is also guaranteed that your house and Dijkstra's starting location are not inside of any rectangles.
The first line contains five space-separated integers N, s_x, s_y, e_x, e_y.
The i-th line of next N lines contain four space-separated integers a_i, b_i, c_i, d_i. This indicates that i-th skyscraper is a rectangle with its four corners located in (a_i, b_i), (a_i, d_i), (c_i, b_i), (c_i, d_i).
Print the length of the shortest path between Dijkstra's location and your house, using the Manhattan metric.
1≤N≤250,000
0≤s_x, s_y, e_x, e_y≤108
0≤a_i<c_i≤108 (1≤i≤n)
0≤b_i<d_i≤108 (1≤i≤n)
Let X=s_x, e_x, a_1, a_2, ⋯, a_N, c_1, c_2, ⋯, c_N, all elements in X are distinct.
Let Y=s_y, e_y, b_1, b_2, ⋯, b_N, d_1, d_2, ⋯, d_N, all elements in Y are distinct.
No pair of rectangles share a common point.
Dijkstra's location and your house's location are not in any of the rectangles.