这道题很难,我不会
代码
1 #include<iostream> 2 using namespace std; 3 const int N = 5e5+5; 4 int q[N],tmp[N],n; 5 long long ans = 0; 6 void merge_sort(int q[],int l,int r) 7 { 8 if(l>=r) 9 return; 10 int mid = l+r >> 1; 11 merge_sort(q,l,mid); 12 merge_sort(q,mid+1,r); 13 int i = l, j = mid + 1,k = 0; 14 while(i<=mid && j<=r) 15 { 16 if(q[i]<=q[j]) tmp[k++] = q[i++]; 17 else tmp[k++] = q[j++],ans += mid - i + 1; 18 } 19 while(i<=mid) 20 tmp[k++] = q[i++]; 21 while(j<=r) 22 tmp[k++] = q[j++]; 23 for(int i = l,j = 0; i<=r ;i++,j++) 24 q[i] = tmp[j]; 25 } 26 int main() 27 { 28 cin>>n; 29 for(int i=0; i < n ;i++) 30 cin>>q[i]; 31 merge_sort(q,0,n-1); 32 cout<<ans<<endl; 33 }