aabbcczyy @ 2019-10-10 19:03:55
#include<stdio.h>
int solve(long long ,long long );
long long n,i,k,tot1,tot2,tot3,now,l1,l2,ans;
long long w[500010],a[500010],b[500010],c[500010];
int main()
{
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&w[i]);
solve(1,n);
printf("%d",ans);
return 0;
}
int solve(long long x,long long y)
{
if ((y-x)>1) {solve(x,(x+y)/2);solve((x+y)/2+1,y);}
if (y==x) return 0;
tot1=0;tot2=0;
for (k=x;k<=(x+y)/2;k++)
{
tot1++;
a[tot1]=w[k];
}
for (k=(x+y)/2+1;k<=y;k++)
{
tot2++;
b[tot2]=w[k];
}
now=0;l1=1;l2=1;
while ((l1<=tot1)&&(l2<=tot2))
{
if (a[l1]<=b[l2]) {now++;c[now]=a[l1];l1++;}
else {ans+=tot1-l1+1;now++;c[now]=b[l2];l2++;}
}
if (l1<=tot1) while (l1<=tot1) {now++;c[now]=a[l1];l1++;}
if (l2<=tot2) while (l2<=tot2) {now++;c[now]=b[l2];l2++;}
now=1;
for (k=x;k<=y;k++)
{
w[k]=c[now];
now++;
}
return 0;
}
by XyzL @ 2019-10-10 19:13:02
%%%
by nculemonlsy123 @ 2019-10-25 16:47:15
输出格式错了
by 一只野生蒟蒻 @ 2019-10-27 10:43:32
ans要用long long