标签:
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
Input
Output
Sample Input
1
3 1 2 3
Sample Output
1
Source
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 using namespace std; 9 const int mxn=100020; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 13 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 14 return x*f; 15 } 16 int n; 17 LL t[mxn+5]; 18 struct node{ 19 int x; 20 int id; 21 }a[mxn]; 22 int cmp(const node a,const node b){ 23 return a.x<b.x; 24 } 25 int lowbit(int x){return x&-x;} 26 void add(int p,int v){//k==0 个数 k==1 距离 27 while(p<=mxn){t[p]+=v;p+=lowbit(p);} 28 } 29 LL smm(int x){ 30 LL res=0; 31 while(x){res+=t[x];x-=lowbit(x);} 32 return res; 33 } 34 int T; 35 int main(){ 36 scanf("%d",&T); 37 int i,j; 38 while(T--){ 39 memset(t,0,sizeof t); 40 scanf("%d",&n); 41 for(i=1;i<=n;i++){ 42 a[i].x=read(); 43 a[i].id=i; 44 } 45 LL ans=0; 46 sort(a+1,a+n+1,cmp); 47 add(a[1].id,1); 48 for(i=2;i<n;i++){ 49 int ldn=smm(a[i].id);//所有id小于当前id,且x小于当前x的 50 int lup=a[i].id-1-ldn;//所有id小于当前id,但x大于当前x的 51 int rdn=(smm(mxn)-ldn);//所有id大于当前id,且x小于当前x的 52 int rup=n-rdn-a[i].id;//所有id大于当前id,且x大于当前x的 53 ans+=ldn*rup+lup*rdn; 54 add(a[i].id,1); 55 } 56 printf("%lld\n",ans); 57 } 58 return 0; 59 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5885326.html