Frog Jump
Time limit1sMemory limit1024 MB
Given n closed intervals and a sequence of k target intervals, find the total jump length the frog pays while visiting the targets in order from interval 1.
- Level
Hard8 of 10
- Topics
- Intervals, Binary search, Sorting
- Solved
- No attempts yet
Problem
A frog lives in a beautiful lake. A lot of lotus leaves float in a row on the lake, and each leaf is a closed interval on a line. The frog likes to stay on lotus leaves and moves between them.
There are closed intervals on the -axis, and the frog starts on some interval . Two intervals overlap if they share a common point. The frog can move from an interval to an interval if they overlap, so it can move through a chain of overlapping intervals. When the frog moves to the right (left) through overlapping intervals, it may reach an interval from which it cannot move further right (left) past the right (left) endpoint of . In this case, the frog jumps to the interval with the smallest left endpoint (largest right endpoint) among the intervals whose left endpoint is greater than the right endpoint of (whose right endpoint is smaller than the left endpoint of ), if such an interval exists. The jump length is the distance between the right (left) endpoint of and the left (right) endpoint of . See Figure F.1.

Figure F.1 Jump length
In Figure F.2, eight intervals [1, 8], [2, 4], [5, 11], [13, 15], [15, 17], [16, 18], [19, 22] and [20, 22] are given and numbered from 1 to 8. The frog is initially on interval 1. The frog should visit the intervals 3, 7, 4, 6, 3 in this order. The frog moves from interval 1 to 3 with no jump. It moves from 3 to 7 with two jumps, from 3 to 4 and from 6 to 7, whose jump lengths total 3. During this movement the frog passes through interval 4, but it must visit interval 4 only after interval 7. So there are two more jumps, from 7 to 4 and from 6 to 3, whose jump lengths total 3. After the frog visits all the given intervals, the total jump length is 6. In this travel, the frog has to jump if necessary.

Figure F.2 The given eight intervals
Given intervals on the line and a sequence of intervals, output the total jump length while the frog visits the intervals in order, starting from interval 1.
Input
The first line contains two integers and (, ), where is the number of intervals and is the number of intervals the frog should visit. The intervals are numbered from 1 to , and the frog starts at interval 1. The next lines contain two integers and () each, the left and right endpoints of interval on the -th line. The intervals are given in increasing order of their left endpoints, and in increasing order of their right endpoints when the left endpoints are equal. All intervals are distinct. The last line contains integers, the intervals the frog should visit in order. Each integer is between 1 and , and the integers may repeat.
Output
Print exactly one line containing the total jump length of the frog when it visits the given intervals in order.