#P3088. Crowded Cows

    ID: 16346 Type: Default 1000ms 256MiB

Crowded Cows

Crowded Cows

Farmer John has N cows grazing along a one-dimensional fence. The i-th cow is located at position \(x_i\) and has height \(h_i\). A cow is considered crowded if there is at least one cow within distance \(D\) to its left and at least one cow within distance \(D\) to its right whose height is at least twice its own. Formally, cow \(i\) is crowded if and only if:

[ \begin{array}{l} \text{There exists some cow } j \text{ with } x_i - D \le x_j < x_i \text{ such that } h_j \ge 2h_i, \[6pt] \text{and there exists some cow } k \text{ with } x_i < x_k \le x_i + D \text{ such that } h_k \ge 2h_i. \end{array} ]

Your task is to count the number of crowded cows.

inputFormat

The first line contains two integers \(N\) and \(D\) where \(1 \le N \le 50000\) and \(1 \le D \le 10^9\). Each of the next \(N\) lines contains two integers \(x_i\) and \(h_i\) (\(1 \le x_i, h_i \le 10^9\)) representing the position and height of the \(i\)-th cow. The cows may be given in arbitrary order.

outputFormat

Output a single integer: the number of cows that are considered crowded.

sample

6 4
10 3
6 2
5 3
9 7
12 3
14 10
2