1 条题解
-
3
#include<bits/stdc++.h> using namespace std;long long read(){long long ans=0,f=1;char ch=getchar();while(!isdigit(ch))f*=(ch=='-')?-1:1,ch=getchar();while(isdigit(ch))ans=ans10+ch-'0',ch=getchar();return ansf;}struct Tuple{long long id,val;}tup[1000000],temp[1000000];long long cnt1[1000000],cnt2[1000000],sum,ans,n;void CDQ(long long l,long long r,long long cnt){if(l==r)return;long long mid=(l+r)>>1;CDQ(l,mid,cnt),CDQ(mid+1,r,cnt);for(long long i=l,j=mid+1,k=l,sum=0;k<=r;k++){if(j>r||i<=mid&&tup[i].val<=tup[j].val)temp[k]=tup[i++],sum++;else cnt[tup[j].id]+=sum,temp[k]=tup[j++];}for(long long i=l;i<=r;i++)tup[i]=temp[i];}int main(){cin>>n;for(long long i=1;i<=n;i++){tup[i]=(Tuple){i,read()};}CDQ(1,n,cnt1);for(long long i=1;i<=n;i++)while(i!=n+1-tup[i].id)swap(tup[i],tup[n+1-tup[i].id]);CDQ(1,n,cnt2);for(long long i=1;i<=n;i++)ans+=(long long)cnt1[i](n-i-cnt2[i])+(long long)(i-1-cnt1[i])*cnt2[i];cout<<ans;return 0;}
信息
- ID
- 1558
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- 递交数
- 90
- 已通过
- 9
- 上传者