PivotOJ

Frog Jump

시간 제한: 1000ms메모리 제한: 1024MB출처: ICPC Asia Seoul Regional 2022BOJ 26107

문제

A frog is living in a beautiful lake. On the lake, there are a lot of lotus leaves floating in a row, which are represented by closed intervals on the line. The frog likes to be on lotus leaves and moves between them.

The nn closed intervals, representing lotus leaves, on the line, that is, on the xx-axis are given and the frog is initially on some interval I0I_0. The frog can move from an interval II to an interval JJ if they overlap. Two intervals overlap if they share a common point. So the frog can move through overlapping intervals. When the frog is moving to the right (left) through the overlapping intervals, it may reach an interval HH, where it can no longer move to the right (left) from the right (left) endpoint of HH. In this case, the frog can jump to the interval KK with the smallest (largest) left (right) endpoint among intervals whose left (right) endpoint is greater (smaller) than the right (left) endpoint of HH if they exist. Then, the jump length is defined to be the length between the right (left) endpoint of HH and the left (right) endpoint of KK. See Figure F.1.

Figure F.1 Jump length

A sequence of kk intervals I1,I2,,IkI_1, I_2, \dots , I_k is given and the frog should visit the intervals in order from the initial interval I0I_0. In this travel, the frog has to jump if necessary.

For example, in Figure F.2, eight intervals [1,8][1, 8], [2,4][2, 4], [5,11][5, 11], [13,15][13, 15], [15,17][15, 17], [16,18][16, 18], [19,22][19, 22] and [20,22][20, 22] are given and numbered from 11 and 88. The frog is initially on interval 11. Intervals 3,7,4,6,33, 7, 4, 6, 3 which the frog should visit in a sequence are given. Then the frog moves from interval 11 to 33 with no jump, and it moves from 33 to 77 with two jumps, say, 3 → 4 and 6 → 7 whose jump length is 33 totally. In this movement, the frog passes through the interval 44. Nevertheless, it should visit the interval 44 after the interval 77. Then, there are two jumps during the movements from 77 to 44 and from 66 to 33 whose jump length is 33 totally. Thus after the frog visits all the given intervals, the total jump length is 66.

Figure F.2 The given eight intervals

Given nn intervals on the line and a sequence of kk intervals, write a program to output the total jump length during the travel that the frog visits the kk intervals in order from its initial interval 11.

입력

Your program is to read from standard input. The input starts with a line containing two integers, nn and kk (1 &le; n &le; 100\,000 and 1 &le; k &le; 1\,000\,000), where nn is the number of intervals and kk is the number of intervals which the frog should visit. The intervals are numbered from 11 to nn and the initial location of the frog is always 11. In the following nn lines, the ii-th line contains two integers aa and bb (0 &le; a < b &le; 10^9) that represent the left and right endpoints of interval ii, respectively. The intervals are given in increasing order of their left endpoints – if they are same, then in increasing order of the right endpoints. Also the intervals are all distinct. The next line contains kk integers that represent the intervals which the frog should visit in order. These integers are between 11 and nn and can be in duplicate.

출력

Your program is to write to standard output. Print exactly one line. The line should contain the total jump length of frog when it visits the given kk intervals in order.

예제

예제 1

입력
4 3
0 2
0 3
3 5
6 7
4 2 3
출력
2

예제 2

입력
4 3
0 2
0 3
3 5
6 7
2 3 2
출력
0

예제 3

입력
8 5
1 8
2 4
5 11
13 15
15 17
16 18
19 22
20 22
3 7 4 6 3
출력
6
코드를 제출하려면 로그인하세요.