This page is still under construction.

Parts of this page are still being built. What you see may change.

Frog Jump

Time limit1sMemory limit1024 MB

Summary
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 nn closed intervals on the xx-axis, and the frog starts on some interval I0I_0. Two intervals overlap if they share a common point. The frog can move from an interval II to an interval JJ 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 HH from which it cannot move further right (left) past the right (left) endpoint of HH. In this case, the frog jumps to the interval KK with the smallest left endpoint (largest right endpoint) among the intervals whose left endpoint is greater than the right endpoint of HH (whose right endpoint is smaller than the left endpoint of HH), if such an interval exists. The jump length is the distance between the right (left) endpoint of HH and the left (right) endpoint of KK. 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 nn intervals on the line and a sequence of kk intervals, output the total jump length while the frog visits the kk intervals in order, starting from interval 1.

Input

The first line contains two integers nn and kk (1≤n≤1000001 \le n \le 100000, 1≤k≤10000001 \le k \le 1000000), where nn is the number of intervals and kk is the number of intervals the frog should visit. The intervals are numbered from 1 to nn, and the frog starts at interval 1. The next nn lines contain two integers aa and bb (0≤a<b≤1090 \le a < b \le 10^9) each, the left and right endpoints of interval ii on the ii-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 kk integers, the intervals the frog should visit in order. Each integer is between 1 and nn, and the integers may repeat.

Output

Print exactly one line containing the total jump length of the frog when it visits the given kk intervals in order.

Examples3

  1. Example 1

    Input
    4 3
    0 2
    0 3
    3 5
    6 7
    4 2 3
    
    Expected output
    2
    
  2. Example 2

    Input
    4 3
    0 2
    0 3
    3 5
    6 7
    2 3 2
    
    Expected output
    0
    
  3. Example 3

    Input
    8 5
    1 8
    2 4
    5 11
    13 15
    15 17
    16 18
    19 22
    20 22
    3 7 4 6 3
    
    Expected output
    6