#P15637. [2019 KAIST RUN Spring] Dijkstra Is Playing At My House

[2019 KAIST RUN Spring] Dijkstra Is Playing At My House

Problem Description

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 5 hours, so he should better start moving.

New York City is modeled as a 2-dimensional plane. Dijkstra is now in coordinate (sx,sy)(s_x, s_y), and your house is located in coordinate (ex,ey)(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\textit{Manhattan distance}, right?). Also, there are NN 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 xx coordinates are distinct and all yy 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.

Input Format

The first line contains five integers N,sx,sy,ex,eyN, s_x, s_y, e_x, e_y. ($1 \le N \le 250 000, 0 \le s_x, s_y, e_x, e_y \le 10^8$)

The next NN lines contain four integers ai,bi,ci,dia_i, b_i, c_i, d_i. This indicates that ii-th skyscraper is a rectangle with its four corners located in (ai,bi),(ai,di),(ci,bi),(ci,di)(a_i, b_i), (a_i, d_i), (c_i, b_i), (c_i, d_i). (0ai<ci1080 \le a_i < c_i \le 10^8, 0bi<di1080 \le b_i < d_i \le 10^8).

It is guaranteed that:\textbf{It is guaranteed that:}

  • Let $X = \{s_x, e_x, a_1, a_2, \cdots, a_N, c_1, c_2, \cdots, c_N\}, Y = \{s_y, e_y, b_1, b_2, \cdots, b_N, d_1, d_2, \cdots, d_N\}$. All elements in XX are distinct, and all elements in YY 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.

Output Format

Print the length of the shortest path between Dijkstra's location and your house, using the Manhattan metric.

3 2 14 5 1
4 6 6 10
0 7 3 9
1 2 8 5
20
1 0 500 100 503
1 0 99 1000
1097
2 2 8 10 3
3 6 6 10
7 1 8 7
15