Make Rounddog Happy

Time limit2sMemory limit512 MB

Summary
Count subarrays whose elements are all distinct and whose maximum minus length is at most k, for arrays up to 300,000 with values bounded by n.
Level

Hard9 of 10

Topics
Divide and conquer, Two pointers, Segment tree, Array
Solved
No attempts yet

Problem

Rounddog always carries an array a1,a2,…,ana_1, a_2, \ldots, a_n in his right pocket, satisfying 1≤ai≤n1 \le a_i \le n.

A subarray is a non-empty contiguous segment of the original array. Rounddog defines a good subarray as a segment al,al+1,…,ara_l, a_{l+1}, \ldots, a_r in which all elements are distinct and max⁡(al,al+1,…,ar)−(r−l+1)≤k\max(a_l, a_{l+1}, \ldots, a_r) - (r - l + 1) \le k.

Rounddog is not happy today. As his best friend, you want to find all good subarrays of aa to make him happy. For this problem, calculate the number of good subarrays of aa.

Input

The input contains several test cases. The first line contains a single integer TT (1≤T≤201 \le T \le 20), the number of test cases.

The first line of each test case contains two integers nn (1≤n≤300 0001 \le n \le 300\,000) and kk (1≤k≤300 0001 \le k \le 300\,000).

The second line contains nn integers, the ii-th of which is aia_i (1≤ai≤n1 \le a_i \le n).

The sum of nn over all test cases does not exceed 1 000 0001\,000\,000.

Output

For each test case, print a single line with a single integer: the number of good subarrays in the given array.

Examples1

  1. Example 1

    Input
    2
    5 3
    2 3 2 2 5
    10 4
    1 5 4 3 6 2 10 8 4 5
    
    Expected output
    7
    31