All Tracks Data Structures Advanced Data Structures Fenwick (Binary Indexed) Trees Problem

Micro and Array Function
/

Data Structures, Data Structures, Fenwick Tree, Fenwick tree

Problem
Editorial
Analytics

Micro has made a breakthrough. He made a function which he proudly calls Micro's Array Fucntion. It takes an array of integers A and an integer K as input and optimally finds out the number of unordered pairs \((i, j), i \neq j\) such that \(|A[i]-A[j]| \ge K\).
Micro is getting a lot of recognition because of it. His best friend Mike wants to be a part of it, but for that he needs to make some contribution. He thought of extending Micro's Array function. He wants to make a new function \(g(A,K)\) that also takes an array of integers A and an integer K as input and optimally calculates \( \sum{f(X, K)} \) for all contiguous subarrays X of A. He need your help in this and help here means do the entire task. He'll give you an integer K and an array A having N integers and you need to compute \(g(A,K)\).

Input:
First line consists of a single integers T denoting the number of test cases.
First line of each test case consists of two space separated integer denoting N and K.
Second line of each test case consists of a N space separated integers denoting the array A.

Output:
For each test case, print the value of \(g(A,K)\) in a new line.

Constraints:
\(1 \le T \le 5\)
\(1 \le N \le 10^5\)
\(1 \le A[i], K \le 10^9\)

SAMPLE INPUT
1
3 2
1 2 3
SAMPLE OUTPUT
1
Explanation

\(X = [1]\), \(f(X,K) = 0\)
\(X = [2]\), \(f(X,K) = 0\)
\(X = [3]\), \(f(X,K) = 0\)
\(X = [1, 2]\), \(f(X,K) = 0\)
\(X = [2, 3]\), \(f(X,K) = 0\)
\(X = [1, 2, 3]\) , \(f(X,K) = 1\)

Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB

Best Submission

Similar Problems

Contributors

This Problem was Asked in

Initializing Code Editor...
Notifications
View All Notifications

?