LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Inversions After Removing a Segment

HardProblem #64
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Cutting out more can never create an inversion. What does that tell you about the pairs that work?

An inversion in an array is a pair of positions where the earlier element is the bigger one - formally, positions i<j with bi​>bj​.

You are given an array a of n positive integers. Pick two positions l and r with 1≤l<r≤n, cut out everything strictly between them, and you are left with

b=a1​a2​…al​ar​ar+1​…an​

Note that r=l+1 cuts out nothing at all, so b is then the whole array.

Count the pairs (l,r) for which the array b has at most k inversions.

Input

First line of input will be a single integer t - the number of testcases.
The first line of each testcase contains two integers n and k - the length of the array and the largest number of inversions allowed.
The second line contains n integers a1​,a2​,…,an​.

Output

For every testcase print a single line with the number of pairs (l,r) that leave at most k inversions.

Example

Input
4
3 1
1 3 2
3 0
1 3 2
5 2
1 5 4 1 100
5 4
1 5 4 1 100
Output
3
1
6
10

The first two testcases use the same array 1,3,2, which has three possible pairs. The pair (1,3) leaves b=1,2 with no inversions; the pairs (1,2) and (2,3) both cut out nothing and leave the whole array, which has one inversion. So one pair works when k=0 and all three work when k=1. In the last testcase every one of the 10 pairs stays within 4 inversions.

Constraints

1≤t≤10
2≤n≤105
0≤k≤1018
1≤ai​≤109
The sum of n over all testcases does not exceed 2⋅105


This problem was adapted, with permission, from Inverzije nakon izbacivanja segmenata, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit