1 #include <cstring> 2 #include <cstdio> 3 #include <iostream> 4 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 1e5+100; 9 int a[maxn]; 10 struct Bittree 11 { 12 int tr[maxn]; 13 void init() 14 { 15 memset(tr,0,sizeof(tr)); 16 } 17 void update(int x,int v) 18 { 19 for (;x<maxn;x+=(x&-x)) tr[x] += v; 20 } 21 int query(int x) 22 { 23 int res = 0; 24 for (;x;x-=(x&-x)) res += tr[x]; 25 return res; 26 } 27 }L,R; 28 29 30 void slove() 31 { 32 int i,n,m; 33 LL cur = 0,ans; 34 L.init();R.init(); 35 scanf("%d%d",&n,&m); 36 for (i = 0;i<n;i++) 37 scanf("%d",&a[i]); 38 for (int i = n-1;i>=m;--i) 39 { 40 cur += R.query(a[i]-1); 41 R.update(a[i],1); 42 } 43 ans = cur; 44 45 for (i = 0;i+m<n;++i) 46 { 47 R.update(a[i+m],-1); 48 cur+=R.query(a[i]-1); 49 cur-=R.query(a[i+m]-1); 50 cur+=L.query(n+1-(a[i]+1)); 51 cur-=L.query(n+1-(a[i+m]+1)); 52 L.update(n+1-(a[i]),1); 53 ans = min(ans,cur); 54 } 55 printf("%I64d ",ans); 56 } 57 58 int main() 59 { 60 int T; 61 scanf("%d",&T); 62 while (T--) 63 { 64 slove(); 65 } 66 return 0; 67 }