提交时间:2019-11-24 18:41:39

运行 ID: 2410

#include<iostream> #include<cstring> #include<cstdio> typedef long long ll; using namespace std; const int sz = 100010; ll a[sz],tmp[sz]; ll ans,n; inline void merge(ll l,ll m,ll r) { ll i=l; ll j=m+1; ll k=l; while(i<=m&&j<=r) { if(a[i]>a[j]) { tmp[k++]=a[j++]; ans+=m-i+1; } else tmp[k++]=a[i++]; } while(i <= m) tmp[k++]=a[i++]; while(j <= r) tmp[k++]=a[j++]; for(int i=l;i<=r;i++) a[i] = tmp[i]; return; } inline void merge_sort(ll l,ll r) { if(l<r) { ll m=(l+r)>> 1; merge_sort(l,m); merge_sort(m+1,r); merge(l,m,r); } return; } int main() { scanf("%lld",&n); for(ll i=0;i<n;i++) scanf("%lld",&a[i]); merge_sort(0,n-1); printf("%lld\n",ans); return 0; }