标签:
http://lx.lanqiao.org/problem.page?gpid=T123
1 #include<iostream> 2 #include<memory.h> 3 #define MAX 1000010 4 #define N 100010 5 using namespace std; 6 int C1[MAX],C2[MAX],b[MAX]; 7 int num[N] , i; 8 long long total[N],ans; 9 10 int lowbit(int x) 11 { 12 return x&(-x); 13 } 14 15 void add(int pos,int num,int *C) 16 { 17 while(pos<=MAX) 18 { 19 C[pos]+=num; 20 pos+=lowbit(pos); 21 } 22 } 23 24 int Sum(int pos,int *C) 25 { 26 int sum=0; 27 while(pos>0) 28 { 29 sum+=C[pos]; 30 pos-=lowbit(pos); 31 } 32 return sum; 33 } 34 35 36 void init() 37 { 38 total[0]=0; 39 for(i=1;i<=N;i++) 40 { 41 total[i]=total[i-1]+i; 42 } 43 } 44 45 int main() 46 { 47 init(); 48 memset(C1,0,sizeof(C1)); 49 memset(C2,0,sizeof(C2)); 50 int n; 51 cin>>n; 52 for(i=0;i<n;i++) 53 { 54 cin>>num[i]; 55 add(num[i]+1,1,C1); 56 b[i]=i-Sum(num[i],C1); 57 b[i]=b[i]-(Sum(num[i]+1,C1)-Sum(num[i],C1)-1); 58 } 59 for(i=n-1;i>=0;i--) 60 { 61 add(num[i]+1,1,C2); 62 b[i]=b[i]+Sum(num[i],C2); 63 64 } 65 ans=0; 66 for(i=0;i<n;i++) 67 { 68 ans+=total[b[i]]; 69 } 70 cout<<ans<<endl; 71 return 0; 72 }
标签:
原文地址:http://www.cnblogs.com/jeff-wgc/p/4450941.html