Under construction

Still being built. Things may change

Why Did the Cow Cross the Road 12

Time limit2sMemory limit512 MB

Summary
Given the left and right orderings of N breeds along a road, count pairs whose segments cross and whose breed numbers differ by more than K.
Level

Medium6 of 10

Topics
Divide and conquer, Sorting, Array, Binary search
Solved
No attempts yet

Problem

This time we found John's farm without trouble, but the plan still fell through for a different reason: someone found an error in his theory of cow friendliness.

John's farm has NN breeds of cows, numbered 1,2,…,N1, 2, \ldots, N. Cows of breed aa and breed bb are friendly if ∣a−b∣≤K|a-b| \le K, and unfriendly otherwise. Pastures for different breeds are built so differently that each pasture can hold only one specific breed: pasture ii can graze only cows of breed ii. When a cow crosses the road, it walks to the pasture on the other side that grazes its own breed.

For the new members of the Help John Association, here is the farm layout again. The farm has one straight road with NN pastures on each side. On the left side, each breed occupies exactly one pasture, and the same holds on the right side. John paid no attention to the order when he built the pastures, so the paths of a breed aa cow and a breed bb cow may cross each other. Call such a pair of breeds (a,b)(a, b) a "crossing pair".

Before installing crosswalks, we want to count the crossing pairs whose breeds are unfriendly. The term of the association's president is almost over, so this is our last task. Let's help John one last time.

Input

The first line contains NN (1≤N≤100,0001 \le N \le 100{,}000) and KK (0≤K<N0 \le K < N). Each of the next NN lines contains one pasture number, listing the pastures on the left side of the road in order. Every breed appears exactly once. The next NN lines list the pastures on the right side of the road in the same format.

Output

Print the number of crossing pairs whose breeds are unfriendly.

Hint

In the first example, breeds 11 and 44, and breeds 11 and 33, are the crossing pairs that are unfriendly.

Examples1

  1. Example 1

    Input
    4 1
    4
    3
    2
    1
    1
    4
    2
    3
    
    Expected output
    2