#P3658. [USACO17FEB] Why Did the Cow Cross the Road III P

    ID: 2864 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>2017USACO树状数组cdq 分治

[USACO17FEB] Why Did the Cow Cross the Road III P

Problem Description

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 aa and bb are now friendly if abK|a - b| \leq 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.

Input Format

The first line of input contains NN (1N100,0001 \leq N \leq 100,000) and KK (0K<N0 \leq K < N). The next NN lines describe the order, by breed ID, of fields on one side of the road; each breed ID is an integer in the range 1N1 \ldots N. The last NN 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.

Output Format

Please output the number of unfriendly crossing pairs of breeds.

4 1
4
3
2
1
1
4
2
3
2

Hint

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