Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 2000004
#define ll long long
#define setIO(s) freopen(s".in", "r" , stdin)
using namespace std;
namespace IO
{
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int readint() {int x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x;}
ll readll() {ll x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x;}
};
ll re = 0;
int a[N], bu[N], n ;
void solve(int l , int r)
{
if(l == r) return ;
int mid = (l + r) >> 1, tmp = 0, tl = l, tr = mid + 1;
solve(l, mid), solve(mid + 1, r);
while(tl <= mid && tr <= r)
{
if(a[tl] <= a[tr]) bu[++ tmp] = a[tl], ++tl;
else bu[++ tmp] = a[tr], ++tr , re += (ll) mid - tl + 1;
}
while(tl <= mid) bu[++ tmp] = a[tl], ++tl;
while(tr <= r) bu[++tmp] = a[tr], ++tr;
for(int i = 1; i <= tmp ; ++ i) a[l + i - 1] = bu[i];
}
int main()
{
using namespace IO;
// setIO("input");
int i , j;
ll oo;
n = readint();
for(i = 1 ; i <= n ; ++ i) a[i] = readint();
oo = readll();
solve(1, n), printf("%lld\n", re);
return 0;
}
原文:https://www.cnblogs.com/guangheli/p/11357321.html