P1908-逆序对
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define _for(i,a,b) for(int i = (a);i < b;i ++) 4 const int maxn = 500009; 5 typedef long long ll; 6 int n; 7 ll a[maxn],c[maxn]; 8 ll ans = 0; 9 inline ll read() 10 { 11 ll ans = 0; 12 char ch = getchar(), last = ' '; 13 while(!isdigit(ch)) last = ch, ch = getchar(); 14 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 15 if(last == '-') ans = -ans; 16 return ans; 17 } 18 inline void write(ll x) 19 { 20 if(x < 0) x = -x, putchar('-'); 21 if(x >= 10) write(x / 10); 22 putchar(x % 10 + '0'); 23 } 24 void msort(int le,int rr) 25 { 26 if(le==rr) 27 return ; 28 int mid = (le+rr)/2; 29 msort(le,mid),msort(mid+1,rr); 30 int i = le,j = mid+1; 31 int k = le; 32 while(i <= mid && j <= rr) 33 { 34 if(a[i]<=a[j]) 35 c[k++] = a[i++]; 36 else 37 { 38 c[k++] = a[j++]; 39 ans += mid-i+1; 40 } 41 } 42 memcpy(c+k,a+i,(mid-i+1)*(sizeof(ll))); 43 k += mid-i+1; 44 memcpy(c+k,a+j,(rr-j+1)*(sizeof(ll))); 45 memcpy(a+le,c+le,(rr-le+1)*(sizeof(ll))); 46 } 47 int main() 48 { 49 n = read(); 50 _for(i,0,n) 51 a[i] = read(); 52 53 msort(0,n-1); 54 write(ans); 55 return 0; 56 }