提交时间:2023-09-03 13:44:14

运行 ID: 100802

#include <bits/stdc++.h> using namespace std; int arr[500005]; int tmp[500005]; long long g_sort(int left,int right){ if(left>=right) return 0; int mid = (left+right)/2; long long count1 = g_sort(left,mid); long long count2 = g_sort(mid+1,right); int i = left,j = mid+1; long long count3 = 0; int e = left; while(i<=mid&&j<=right){ if(arr[i]>arr[j]){ count3+=mid-i+1; tmp[e++] = arr[j]; j++; } else { tmp[e++] = arr[i]; i++; } } while(i<=mid){ tmp[e++] = arr[i]; i++; } while(j<=right){ tmp[e++] = arr[j]; j++; } for(int i = left;i<=right;i++) { arr[i] = tmp[i]; } return count1+count2+count3; } int main(){ int n; cin>>n; for(int i = 0;i<n;i++){ scanf("%d",&arr[i]); } long long x = g_sort(0,n-1); cout<<x<<endl; return 0; }