题目连接:https://vjudge.net/problem/UVALive-4329
lowbit..
1 #include<cstdio> 2 #include<cstring> 3 #define ll long long 4 const int maxn=1e6+10; 5 6 int c[maxn]; 7 int a[20005]; 8 int m1[20005],m2[20005]; 9 10 int lowbit(int x) 11 { 12 return x&(-x); 13 } 14 15 int sum(int x) 16 { 17 int ret=0; 18 while(x>0) 19 { 20 ret+=c[x]; 21 x-=lowbit(x); 22 } 23 return ret; 24 } 25 26 void add(int x,int d) 27 { 28 while(x<maxn) 29 { 30 c[x]+=d; 31 x+=lowbit(x); 32 } 33 } 34 35 int main() 36 { 37 int t,n; 38 scanf("%d",&t); 39 while(t--) 40 { 41 memset(c,0,sizeof(c)); 42 scanf("%d",&n); 43 for(int i=1;i<=n;i++) 44 { 45 scanf("%d",&a[i]); 46 m1[i]=sum(a[i]); 47 add(a[i],1); 48 } 49 memset(c,0,sizeof(c)); 50 for(int i=n;i>=1;i--) 51 { 52 m2[i]=sum(a[i]); 53 add(a[i],1); 54 } 55 ll ans=0; 56 for(int i=1;i<=n;i++) 57 ans+=m1[i]*1ll*(n-i-m2[i])+m2[i]*1ll*(i-m1[i]-1); 58 printf("%lld\n",ans); 59 } 60 }
时间: 2024-10-25 00:44:11