Sky Walking

아직 제출이 없습니다시간 제한4초메모리 제한1024 MB

문제

Kenan drew a plan of the buildings and skywalks along one side of the main avenue of Baku. There are nn buildings numbered from 00 to n1n-1 and mm skywalks numbered from 00 to m1m-1. The plan is drawn on a two-dimensional plane, where the buildings and skywalks are vertical and horizontal segments respectively.

The bottom of building ii (0in1)(0 \leq i \leq n-1) is located at point (x\[i],0)(x\[i], 0) and the building has height h\[i]h\[i]. Hence, it is a segment connecting the points (x\[i],0)(x\[i], 0) and (x\[i],h\[i])(x\[i], h\[i]).

Skywalk jj (0jm1)(0 \leq j \leq m-1) has endpoints at buildings numbered l\[j]l\[j] and r\[j]r\[j] and has a positive yy-coordinate y\[j]y\[j]. Hence, it is a segment connecting the points (x\[l\[j]],y\[j])(x\[l\[j]], y\[j]) and (x\[r\[j]],y\[j])(x\[r\[j]], y\[j]).

A skywalk and a building intersect if they share a common point. Hence, a skywalk intersects two buildings at its two endpoints, and may also intersect other buildings in between.

Kenan would like to find the length of the shortest path from the bottom of building ss to the bottom of building gg, assuming that one can only walk along the buildings and skywalks, or determine that no such path exists. Note that it is not allowed to walk on the ground, i.e. along the horizontal line with yy-coordinate 00.

One can walk from a skywalk into a building or vice versa at any intersection. If the endpoints of two skywalks are at the same point, one can walk from one skywalk to the other.

Your task is to help Kenan answer his question.

제한

  • 1n,m100,0001 \leq n, m \leq 100\\,000
  • 0x\[0]<x\[1]<<x\[n1]1090 \leq x\[0] < x\[1] < \ldots < x\[n - 1] \leq 10^9
  • 1h\[i]1091 \leq h\[i] \leq 10^9 (for all 0in10 \leq i \leq n-1)
  • 0l\[j]<r\[j]n10 \leq l\[j] < r\[j] \leq n-1 (for all 0jm10 \leq j \leq m-1)
  • 1y\[j]min(h\[l\[j]],h\[r\[j]])1 \leq y\[j] \leq \min(h\[l\[j]], h\[r\[j]]) (for all 0jm10 \leq j \leq m-1)
  • 0s,gn10 \leq s, g \leq n - 1
  • sgs \neq g
  • No two skywalks have a common point, except maybe on their endpoints.