fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1<<20;
  6.  
  7. int tests,n,a[N],b[N];
  8.  
  9. long long count_inversions(int l, int r) {
  10. if(l==r) return 0;
  11. int m=(l+r)>>1,i,j,sz=0;
  12. long long ans=0;
  13. ans+=count_inversions(l,m);
  14. ans+=count_inversions(m+1,r);
  15. i=l;j=m+1;
  16. while(i<=m && j<=r) if(a[j]<a[i]) ans+=m-i+1,b[++sz]=a[j++]; else b[++sz]=a[i++];
  17. while(i<=m) b[++sz]=a[i++];
  18. while(j<=r) b[++sz]=a[j++];
  19. for(i=1;i<=sz;i++) a[l+i-1]=b[i];
  20. return ans;
  21. }
  22.  
  23. int main() {
  24. int i;
  25. scanf("%d", &tests);
  26. while(tests--) {
  27. scanf("%d", &n);
  28. for(i=1;i<=n;i++) scanf("%d", &a[i]);
  29. printf("%lld\n", count_inversions(1,n));
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 11648KB
stdin
Standard input is empty
stdout
Standard output is empty