https://cn.vjudge.net/problem/HihoCoder-1483
思路:二分第k小值;尺取比该值小的区间val数量;
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
#define sfi(x) scanf("%d",&x)
#define sfc(x) scanf("%c",x)
#define sfl(x) scanf("%lld",&x)
#define sfs(x) scanf("%s",x)
#define rint register int
#define pb push_back
#define fl() printf("flag!\n")
#define INF 0x3f3f3f3f
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int maxn=1e6+9;
const int mod=1e9+7;
int a[maxn],b[maxn];
int num[maxn];
ll n,k;
bool ch(ll tot,ll k)
{
for(int i=0;i<=n;i++) num[i]=0;
ll ans=0;
ll sum=0;
int st=1,ed=1;
while(st<=n)
{
while(ed<=n)
{
if(sum+num[a[ed]]<=tot)
{
sum+=num[a[ed]];
num[a[ed]]++;
ed++;
}
else break;
}
ans+=(ed-st);
num[a[st]]--;
sum-=num[a[st]];
st++;
}
return ans>=k;
}
int main()
{
//FAST_IO;
//freopen("input.txt","r",stdin);
int T;
sfi(T);
while(T--)
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=a[i];
}
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
{
int pos=lower_bound(b,b+n,a[i])-b;
a[i]=pos;
}
//for(int i=1;i<=n;i++) cout<<a[i]<<" ";
ll ans=0;
ll l=0,r=n*(n+1)/2;
while(l<=r)
{
ll mid=(l+r)>>1;
if(ch(mid,k))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
cout<<ans<<endl;
}
return 0;
}