lucy2012 @ 2024-06-05 21:18:58
#include<bits/stdc++.h>
using namespace std;
int a[1000010],b[1000010],n,ans;
void merge(int l,int r){
int i,j,k,mid;
i=l;
mid=(l+r)/2;
j=mid+1;
k=l;
while(i<=mid&&j<=r){
if(b[i]<=b[j])
a[k++]=b[i++];
else{
a[k++]=b[j++];
ans+=mid-i+1;
}
}
while(i<=mid)
a[k++]=b[i++];
while(j<=r)
a[k++]=b[j++];
for(i=l;i<=r;i++)
b[i]=a[i];
}
void mergesort(int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(l,mid);
mergesort(mid+1,r);
merge(l,r);
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>b[i];
mergesort(1,n);
cout<<ans;
return 0;
}
by wjr_jok @ 2024-06-05 21:30:04
@lucy2012
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[1000010],b[1000010],n,ans;
void merge(int l,int r){
int i,j,k,mid;
i=l;
mid=(l+r)/2;
j=mid+1;
k=l;
while(i<=mid&&j<=r){
if(b[i]<=b[j])
a[k++]=b[i++];
else{
a[k++]=b[j++];
ans+=mid-i+1;
}
}
while(i<=mid)
a[k++]=b[i++];
while(j<=r)
a[k++]=b[j++];
for(i=l;i<=r;i++)
b[i]=a[i];
}
void mergesort(int l,int r){
if(l<r){
int mid=(l+r)/2;
mergesort(l,mid);
mergesort(mid+1,r);
merge(l,r);
}
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>b[i];
mergesort(1,n);
cout<<ans;
return 0;
}
by wjr_jok @ 2024-06-05 21:30:20
@lucy2012 十年OI一场空,__。
by lucy2012 @ 2024-06-05 21:30:52
@wjr_jok wssb
by __little__Cabbage__ @ 2024-07-04 11:02:55
@wjr_jok 不开long long见祖宗!