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

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

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

题目描述

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| \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.

输入格式

The first line of input contains $N$ ($1 \leq N \leq 100,000$) and $K$ ($0 \leq K

输出格式

Please output the number of unfriendly crossing pairs of breeds.
4 1
4
3
2
1
1
4
2
3
2

提示

In this example, breeds 1 and 4 are unfriendly and crossing, as are breeds 1 and 3. 标签: P3658|2017|USACO|树状数组|cdq 分治

来源

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