Ping pong
题意:
n个人的能力值,任意两个人找第三人能力值位于两者之间,组成一场比赛,问一共能组成几场比赛。
分析:
对于第i个人,BIT求出前面小于它的人数c和后面小于他的人数d,根据乘法原理,c*(n-i-d)+(i-c-1)*d;
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef pair<int,int> PII; typedef long long ll; #define lson l,m,rt<<1 #define pi acos(-1.0) #define rson m+1,r,rt<<11 #define All 1,N,1 #define N 100010 #define read freopen("in.txt", "r", stdin) const ll INFll = 0x3f3f3f3f3f3f3f3fLL; const int INF= 0x7ffffff; const int mod = 1000000007; int a[20010],b[20010],n,c[20010],tmp[N]; void add(int x,int d){ while(x<=N){ tmp[x]+=d; x+=(x&(-x)); } } int sum(int x){ int num=0; while(x>0){ num+=tmp[x]; x-=(x&(-x)); } return num; } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d",&n); memset(tmp,0,sizeof(tmp)); for(int i=1;i<=n;++i){ scanf("%d",&c[i]); a[i]=sum(c[i]); add(c[i],1); } memset(tmp,0,sizeof(tmp)); for(int i=n;i>0;--i) { b[i]=sum(c[i]); add(c[i],1); } ll total=0; for(int i=1;i<=n;++i){ total+=(1LL*a[i]*(n-i-b[i])+1LL*b[i]*(i-1-a[i])); } printf("%lld\n",total); } return 0; }