#P5812. Building and Overpass Shortest Path
Building and Overpass Shortest Path
Building and Overpass Shortest Path
Kenan has drawn a plan of one side of the buildings and overpasses along Baku Avenue. There are \(n\) buildings numbered from \(0\) to \(n-1\) and \(m\) overpasses numbered from \(0\) to \(m-1\). The plan is drawn on a 2D plane where the buildings are represented by vertical line segments and the overpasses by horizontal line segments.
The \(i\)-th building (where \(0 \le i \le n-1\)) has its base located at \((x[i],0)\) and a height of \(h[i]\), forming the segment from \((x[i],0)\) to \((x[i],h[i])\). The \(j\)-th overpass (where \(0 \le j \le m-1\)) connects the buildings \(l[j]\) and \(r[j]\) at a positive height \(y[j]\) and is represented by the horizontal segment from \((x[l[j]], y[j])\) to \((x[r[j]], y[j])\).
An overpass and a building are said to intersect if they share a common point. In particular, an overpass always intersects the two buildings at its endpoints, and it may also intersect with other buildings along its span.
A pedestrian can only travel along the buildings (vertically) and the overpasses (horizontally) – walking on the ground (i.e. along \(y=0\)) is not allowed except when starting or ending. A pedestrian may switch between a building and an overpass at any intersection, and may switch between overpasses if they share an intersection point.
Your task is to determine the shortest path length from the base of building \(s\) (\((x[s],0)\)) to the base of building \(g\) (\((x[g],0)\)), or report \(-1\) if no such path exists.
inputFormat
The first line contains two integers \(n\) and \(m\): the number of buildings and the number of overpasses.
The second line contains \(n\) integers: \(x[0], x[1], \ldots, x[n-1]\), where \(x[i]\) is the x-coordinate of the base of the \(i\)-th building.
The third line contains \(n\) integers: \(h[0], h[1], \ldots, h[n-1]\), where \(h[i]\) is the height of the \(i\)-th building.
The fourth line contains \(m\) integers: \(l[0], l[1], \ldots, l[m-1]\), representing one endpoint (the building index) of each overpass.
The fifth line contains \(m\) integers: \(r[0], r[1], \ldots, r[m-1]\), representing the other endpoint of each overpass.
The sixth line contains \(m\) integers: \(y[0], y[1], \ldots, y[m-1]\), where \(y[j]\) is the height of the \(j\)-th overpass.
The last line contains two integers \(s\) and \(g\): the starting building index and the destination building index.
outputFormat
Output a single integer representing the length of the shortest path from the base of building \(s\) to the base of building \(g\). If no such path exists, output \(-1\).
sample
2 1
0 10
10 10
0
1
5
0 1
20