标签:des style blog io color ar os sp java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4094 Accepted Submission(s):
1522
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 100005; 9 const int Mod = 1000000007; 10 int n; 11 int c[maxn<<2]; 12 struct Node 13 { 14 int id,k; 15 }node[maxn]; 16 bool cmp( Node a,Node b ) 17 { 18 return a.k < b.k; 19 } 20 int lowbit( int x ) 21 { 22 return x&(-x); 23 } 24 void add( int x ) 25 { 26 while( x <= n ) 27 { 28 c[x] += 1; 29 x += lowbit(x); 30 } 31 } 32 33 int sum( int x ) 34 { 35 int ans = 0; 36 while( x >= 1 ) 37 { 38 ans += c[x]; 39 x -= lowbit(x); 40 } 41 return ans; 42 } 43 int main() 44 { 45 #ifndef ONLINE_JUDGE 46 freopen("data.txt","r",stdin); 47 #endif 48 int cas; 49 scanf("%d",&cas); 50 while( cas -- ) 51 { 52 LL ans = 0,ld,rd; 53 scanf("%d",&n); 54 memset( c,0,sizeof(c) ); 55 for( int i = 1; i <= n; i ++ ) 56 { 57 scanf("%d",&node[i].k); 58 node[i].id = i; 59 } 60 sort( node+1,node+n+1,cmp ); 61 for( int i = 1; i <= n; i ++ ) 62 { 63 ld = sum( node[i].id ); //能力小于i且下标下与i的个数 64 rd = i-1 - ld; //能力小于i但下标大于i的个数 65 ans += ( ld*( n-node[i].id - rd ) ) + rd*( node[i].id - ld - 1 ); 66 add( node[i].id ); 67 } 68 printf("%I64d\n",ans); 69 } 70 return 0; 71 }
标签:des style blog io color ar os sp java
原文地址:http://www.cnblogs.com/767355675hutaishi/p/4099524.html