5503: CF EDU - Two Pointer Method - Step 3 - C. Che city
[Creator : ]
Description
This is a problem from the Russia High School Team Programming Contest 2013
In the center of Che city there is a pedestrian street, one of the most popular walking places for city residents. This street is very pleasant to walk, because along the street there are $n$ funny monuments.
The girl Masha from the city of Che likes two boys from her school, and she cannot make a choice between them. To make the final decision, she decided to date both boys at the same time. Masha wants to choose two monuments on the pedestrian street, near which the boys will be waiting for her. At the same time, she wants to choose such monuments so that the boys do not see each other. Masha knows that because of the fog, the boys will see each other only if they are on distance not more than $r$ meters. Masha got interested in how many ways there are to choose two different monuments for organizing dates.
In the center of Che city there is a pedestrian street, one of the most popular walking places for city residents. This street is very pleasant to walk, because along the street there are $n$ funny monuments.
The girl Masha from the city of Che likes two boys from her school, and she cannot make a choice between them. To make the final decision, she decided to date both boys at the same time. Masha wants to choose two monuments on the pedestrian street, near which the boys will be waiting for her. At the same time, she wants to choose such monuments so that the boys do not see each other. Masha knows that because of the fog, the boys will see each other only if they are on distance not more than $r$ meters. Masha got interested in how many ways there are to choose two different monuments for organizing dates.
Input
The first line of the input file contains two integers $n, r\ (2≤n≤300,000,\ 1≤r≤10^9)$, the number of monuments and the maximum distance at which boys can see each other.
The second line contains $n$ positive numbers $d_1,…,d_n$, where $d_i$ is the distance from the $i$-th monument to the beginning of the street. All monuments are located at different distances from the beginning of the street. Monuments are listed in ascending order of distance from the beginning of the street $(1≤d_1<d_2<…<d_n≤10^9)$.
The second line contains $n$ positive numbers $d_1,…,d_n$, where $d_i$ is the distance from the $i$-th monument to the beginning of the street. All monuments are located at different distances from the beginning of the street. Monuments are listed in ascending order of distance from the beginning of the street $(1≤d_1<d_2<…<d_n≤10^9)$.
Output
Print one number, the number of ways to choose two monuments for organizing dates.
Sample 1 Input
4 4
1 3 5 8
Sample 1 Output
2
Masha can choose monuments 1 and 4 or monuments 2 and 4.