http://acm.hdu.edu.cn/showproblem.php?pid=2689

树状数组求逆序数应用,,不解释。。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#define M 1000000001
using namespace std;
typedef long long L;
int  s[M];
int lowbit(int x)
{ return x&(-x);}
void update(int x)
{  while(x<=M)
   {  s[x]++;
      x+=lowbit(x);
   }
}
int Quary(int x)
{   int sum=0;
	while(x>0)
   {  sum+=s[x];
	   x-=lowbit(x);
   }
	return sum;
}
int main()
{ int n,T;
   scanf("%d",&T);
   while(T--)
   {   scanf("%d",&n);
	   memset(s,0,sizeof(s));
	   int a;
      L sum=0;
      for(int i=1;i<=n;++i)
	  {  scanf("%d",&a);
	      update(a);
		  sum+=(i-Quary(a));
      }
	  printf("%d\n",sum);
   }return 0;
}


你可能感兴趣的:(http://acm.hdu.edu.cn/showproblem.php?pid=2689)