#P5812. [IOI 2019] 天桥

[IOI 2019] 天桥

Description

Kenan has drawn a plan of the buildings and skybridges along one side of Baku Avenue. The plan contains nn buildings, numbered from 00 to n1n-1. It also contains mm skybridges, numbered from 00 to m1m-1. The plan is drawn on a two-dimensional plane, where buildings and skybridges are vertical and horizontal line segments, respectively.

The bottom of building ii (0in10 \le i \le n-1) is located at coordinate (x[i],0)(x[i],0), and its height is h[i]h[i]. Therefore, it corresponds to the line segment connecting (x[i],0)(x[i],0) and (x[i],h[i])(x[i],h[i]).

Skybridge jj (0jm10 \le j \le m-1) has its two endpoints on building l[j]l[j] and building r[j]r[j], and has a positive yy-coordinate y[j]y[j]. Therefore, it corresponds to the line segment connecting (x[l[j]],y[j])(x[l[j]],y[j]) and (x[r[j]],y[j])(x[r[j]],y[j]).

A skybridge and a building are said to intersect if they share at least one common point. Thus, a skybridge intersects the two buildings at its endpoints, and it may also intersect other buildings in between.

Kenan wants to find the length of the shortest path from the bottom of building ss to the bottom of building gg, or determine that no such path exists. Pedestrians can only walk along buildings and skybridges, and are not allowed to walk on the ground, meaning they are not allowed to walk along the horizontal line with yy-coordinate 00.

At any intersection point, a pedestrian can move from a skybridge onto a building, or from a building onto a skybridge. If an endpoint of one skybridge is at the same point as an endpoint of another skybridge, the pedestrian can also move from one skybridge to the other.

Your task is to help Kenan answer his question.

Implementation Details

You need to implement the following function.
int64 min_distance(int[] x,int[] h,int[] l,int[] r,int[] y,int s,int g)

  • xx and hh: integer arrays of length nn.
  • ll, rr, and yy: integer arrays of length mm.
  • ss and gg: two integers.
  • If the shortest path from the bottom of building ss to the bottom of building gg exists, the function should return the length of the shortest path. Otherwise, it should return -1.

Input Format

  • Line 11: nn, mm.
  • Line 2+i2+i (0in10 \le i \le n-1): x[i]x[i], h[i]h[i].
  • Line n+2+jn+2+j (0jm10 \le j \le m-1): l[j]l[j], r[j]r[j], y[j]y[j].
  • Line n+m+2n+m+2: ss, gg.

Output Format

A single line containing the return value of the function min_distance.

7 7
0 8
3 7
5 9
7 7
10 6
12 6
14 9
0 1 1
0 2 6
0 6 8
2 3 1
2 6 7
3 4 2
4 6 5
1 5
27

Hint

Sample Explanation

Constraints

  • 1n,m1051 \le n,m \le 10^5.
  • 0x[0]<x[1]<<x[n1]1090 \le x[0] < x[1] < \cdots < x[n-1] \le 10^9.
  • 1h[i]1091 \le h[i] \le 10^9 (for all 0in10 \le i \le n-1).
  • 0l[j]r[j]n10 \le l[j] \le r[j] \le n-1 (for all 0jm10 \le j \le m-1).
  • 1y[j]min(h[l[j]],h[r[j]])1 \le y[j] \le \min(h[l[j]],h[r[j]]) (for all 0jm10 \le j \le m-1).
  • 0s,gn10 \le s,g \le n-1.
  • sgs \ne g.
  • Except at endpoints, any two skybridges do not have any other common points.

Subtasks

  1. (1010 points) n,m50n,m \le 50.
  2. (1414 points) Each skybridge intersects at most 1010 buildings.
  3. (1515 points) s=0s=0, g=n1g=n-1, and all buildings have equal height.
  4. (1818 points) s=0s=0, g=n1g=n-1.
  5. (4343 points) No additional constraints.

Translated by ChatGPT 5