2^Sort
Contest: Codeforces Round 799 (Div. 4)
Difficulty rating: 1400
Tags: data structures, dp, sortings, two pointers

Given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$, find the number of indices $$$1 \leq i \leq n - k$$$ such that the subarray $$$[a_i, \dots, a_{i+k}]$$$ with length $$$k+1$$$ (not with length $$$k$$$) has the following property:

- If you multiply the first element by $$$2^0$$$, the second element by $$$2^1$$$, ..., and the ($$$k+1$$$)-st element by $$$2^k$$$, then this subarray is sorted in strictly increasing order.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq k < n$$$) — the length of the array and the number of inequalities.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.

The sum of $$$n$$$ across all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output a single integer — the number of indices satisfying the condition in the statement.

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

Note:
In the first test case, both subarrays satisfy the condition:

- $$$i=1$$$: the subarray $$$[a_1,a_2,a_3] = [20,22,19]$$$, and $$$1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$$$.
- $$$i=2$$$: the subarray $$$[a_2,a_3,a_4] = [22,19,84]$$$, and $$$1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$$$.

- $$$i=1$$$: the subarray $$$[a_1,a_2] = [9,5]$$$, and $$$1 \cdot 9 < 2 \cdot 5$$$.
- $$$i=2$$$: the subarray $$$[a_2,a_3] = [5,3]$$$, and $$$1 \cdot 5 < 2 \cdot 3$$$.
- $$$i=3$$$: the subarray $$$[a_3,a_4] = [3,2]$$$, and $$$1 \cdot 3 < 2 \cdot 2$$$.
- $$$i=4$$$: the subarray $$$[a_4,a_5] = [2,1]$$$, but $$$1 \cdot 2 = 2 \cdot 1$$$, so this subarray doesn't satisfy the condition.

Example input:
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12

Example output:
2
3
2
3
1
0

Editorial:
Note that $$$2^x \cdot a_i < 2^{x+1} \cdot a_{i+1}$$$ is the same as $$$a_i < 2a_{i+1}$$$, since we can divide by $$$2^x$$$. This means that we only need to check whether $$$a_i < 2a_{i+1}$$$ for each pair of adjacent elements.
Let's consider as an example $$$[20,22,19]$$$. Note that $$$20 < 2 \cdot 22$$$ and $$$22 < 2 \cdot 19$$$, so if you multiply the first element by $$$1$$$, the second by $$$2$$$, and the third by $$$2^2$$$, the array is sorted.
So let's make a new array $$$b$$$ where $$$b_i = 1$$$ if $$$a_i < 2a_{i+1}$$$ and $$$b_i = 0$$$ otherwise. Then we know that the whole chain of inequalities holds true if all values in a subarray of length $$$k$$$ in $$$b$$$ have all their values equal to $$$1$$$.
For example, if $$$a=[9, 5, 3, 2, 1]$$$, $$$b=[1,1,1,0]$$$. Say $$$k=2$$$. Then $$$[9,5,3]$$$ works, since $$$[9,5 \cdot 2, 3 \cdot 2^2]$$$ is sorted. We can write this as $$$9 < 2 \cdot 5$$$ and $$$5 < 2 \cdot 3$$$. This is equivalent to $$$b_1 = 1$$$ (since $$$a_1 < 2a_2$$$) and $$$b_2 = 1$$$ (since $$$a_2 < 2a_3$$$).
So the problem is equivalent to counting the number of subarrays of length $$$k$$$ in $$$b$$$ whose elements are all equal to $$$1$$$. There are many ways to do this. For example, you can compute prefix sums and then find the sum of all subarrays of length $$$k$$$, and count the number whose sum is $$$k$$$. The model solution uses a sliding window and updates the number of ones in the current subarray as we move from left to right.
Time complexity is $$$\mathcal{O}(n)$$$.