Two Platforms
Contest: Codeforces Round 667 (Div. 3)
Difficulty rating: 1800
Tags: binary search, dp, sortings, two pointers

There are $$$n$$$ points on a plane. The $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$. You have two horizontal platforms, both of length $$$k$$$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $$$y$$$-coordinate) and have integer borders. If the left border of the platform is $$$(x, y)$$$ then the right border is $$$(x + k, y)$$$ and all points between borders (including borders) belong to the platform.

Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $$$y$$$-coordinate.

When you place both platforms on a plane, all points start falling down decreasing their $$$y$$$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost.

Your task is to find the maximum number of points you can save if you place both platforms optimally.

You have to answer $$$t$$$ independent test cases.

For better understanding, please read the Note section below to see a picture for the first test case.

Input format:
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow.

The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le k \le 10^9$$$) — the number of points and the length of each platform, respectively. The second line of the test case contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^9$$$), where $$$x_i$$$ is $$$x$$$-coordinate of the $$$i$$$-th point. The third line of the input contains $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$1 \le y_i \le 10^9$$$), where $$$y_i$$$ is $$$y$$$-coordinate of the $$$i$$$-th point. All points are distinct (there is no pair $$$1 \le i < j \le n$$$ such that $$$x_i = x_j$$$ and $$$y_i = y_j$$$).

It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

Output format:
For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The picture corresponding to the first test case of the example:

Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $$$(1, -1)$$$ and $$$(2, -1)$$$ and the second one between points $$$(4, 3)$$$ and $$$(5, 3)$$$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $$$(3, 7)$$$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $$$(5, 3)$$$ doesn't fall at all because it is already on the platform.

Example input:
4
7 1
1 5 2 3 1 5 4
1 3 6 7 2 5 4
1 1
1000000000
1000000000
5 10
10 7 5 15 8
20 199 192 219 1904
10 10
15 19 8 17 20 10 9 2 10 19
12 13 6 17 1 14 7 9 19 3

Example output:
6
1
5
10

Editorial:
Firstly, we obviously don't need $$$y$$$-coordinates at all because we can place both platforms at $$$y=-\infty$$$. Let's sort all $$$x$$$-coordinates in non-decreasing order.
Calculate for each point $$$i$$$ two values $$$l_i$$$ and $$$r_i$$$, where $$$l_i$$$ is the number of points to the left from the point $$$i$$$ (including $$$i$$$) that are not further than $$$k$$$ from the $$$i$$$-th point (i.e. the number of such points $$$j$$$ that $$$|x_i - x_j| \le k$$$). And $$$r_i$$$ is the number of points to the right from the point $$$i$$$ (including $$$i$$$) that are not further than $$$k$$$ from the $$$i$$$-th point. Both these parts can be done in $$$O(n)$$$ using two pointers.
Then let's build suffix maximum array on $$$r$$$ and prefix maximum array on $$$l$$$. For $$$l$$$, just iterate over all $$$i$$$ from $$$2$$$ to $$$n$$$ and do $$$l_i := max(l_i, l_{i-1})$$$. For $$$r$$$, just iterate over all $$$i$$$ from $$$n-1$$$ to $$$1$$$ and do $$$r_i := max(r_i, r_{i + 1})$$$.
The question is: what? What did we do? We did the following thing: the answer always can be represented as two non-intersecting segments of length $$$k$$$ such that at least one endpoint of each segment is some input point (except the case $$$n=1$$$). Now, let's fix this border between segments. Iterate over all $$$i$$$ from $$$1$$$ to $$$n-1$$$ and update the answer with $$$max(l_{i}, r_{i + 1})$$$. So we took some segment that starts at some point to the left from $$$i$$$ (including $$$i$$$) and goes to the left and took some segment that starts further than $$$i+1$$$ (including $$$i+1$$$) and goes to the right. With this model, we considered all optimal answers that can exist.
Time complexity: $$$O(n \log{n})$$$.