刚学,45pts求调

P1908 逆序对

luvsicpt @ 2024-12-26 19:04:03

#include <bits/stdc++.h>
using namespace std;

long long l,r;
long long res=0;
long long a[5000100],b[5000100];

void msort(int l,int r) {
  if (l>=r)return;
  long long mid=l+(r-l)/2;
  msort(l,mid);
  msort(mid+1,r);

  long long i=l,j=mid+1,k=l;
  while (i<=mid&&j<=r) {
    if (a[i]<=a[j]) {
      b[k++]=a[i++];
    }
    if (a[i]>=a[j]){
      b[k++]=a[j++];
      res+=mid-i+1;
    }
  }
  while (i<=mid) {
    b[k++]=a[i++];
  }
  while (j<=r) {
    b[k++]=a[j++];
  }
   for (int e=l;e<=r;e++) {
     a[e]=b[e];
   }

}

int main(){
  int n;
  cin>>n;
  for (int i=1;i<=n;i++) {
    cin>>a[i];
  }
  msort(1,n);
  cout<<res;
  return 0;
}

|