xddno1 @ 2022-01-07 14:37:54
用下面这行就ac while(l<=r) arr[l] = mergearr[l],l++; 但是用这个会爆零 while(l<=r) arr[l] = mergearr[l++]; 求大佬解释一下ToT
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int arr[500005];
int mergearr[500005];
long long ans;
void merge(int l,int r)
{
if(l == r) return ;
int mid = (l+r)/2;
merge(l,mid);
merge(mid+1,r);
int ll=l,rr=mid+1;
int mergecur=l;
while(rr<=r&&ll<=mid)
if(arr[ll]>arr[rr]) (mergearr[mergecur++] = arr[rr++],ans+=mid-ll+1);
else mergearr[mergecur++] = arr[ll++];
while(ll<=mid)mergearr[mergecur++] = arr[ll++];
while(rr<=r)mergearr[mergecur++] = arr[rr++];
while(l<=r) arr[l] = mergearr[l++];
// while(l<=r) arr[l] = mergearr[l],l++;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",arr+i);
merge(1,n);
printf("%lld",ans);
return 0;
}
by ud2_ @ 2022-01-07 14:46:53
编译器没看懂同时读取和修改 l
的值是什么意思。
by _maojun_ @ 2022-01-07 15:32:56
=运算符是先算出右边的值再赋值给左边的,当它算完mergearr[l++]时再去给arr[l],已经是原来的arr[l+1]了,当然会爆0
by _maojun_ @ 2022-01-07 15:33:41
就是在赋值时l已经++了