PivotOJ

Why Did the Cow Cross the Road III (Platinum)

시간 제한: 2000ms메모리 제한: 512MB출처: USACO 2017 February Contest, PlatinumBOJ 14460

문제

 

Farmer John is continuing to ponder the issue of cows crossing the road through his farm, introduced in the preceding two problems. He realizes now that the threshold for friendliness is a bit more subtle than he previously considered -- breeds a and b are now friendly if |a−b| ≤ K, and unfriendly otherwise.

Given the orderings of fields on either side of the road through FJ's farm, please count the number of unfriendly crossing pairs of breeds, where a crossing pair of breeds is defined as in the preceding problems.

 

입력

The first line of input contains N (1 ≤ N ≤ 100,000) and K (0 ≤ K < N). The next N lines describe the order, by breed ID, of fields on one side of the road; each breed ID is an integer in the range 1…N. The last N lines describe the order, by breed ID, of the fields on the other side of the road. Each breed ID appears exactly once in each ordering.

 

출력

Please output the number of unfriendly crossing pairs of breeds.

 

힌트

In this example, breeds 1 and 4 are unfriendly and crossing, as are breeds 1 and 3.

예제

예제 1

입력
4 1
4
3
2
1
1
4
2
3
출력
2
코드를 제출하려면 로그인하세요.