5

珂朵莉的数列

http://www.nowcoder.com/questionTerminal/81d257fd018642e3a12e54b271642bba

include <bits/stdc++.h>

define mid (l+r>>1)

using namespace std;
struct wtf
{
int wei,id;
} a[2000001],tem[2000001];
int n;
__int128 dfs(int l,int r)
{
if(l==r) return 0;
__int128 ret=0;
ret+=dfs(l,mid);ret+=dfs(mid+1,r);
__int128 poi=mid+1,ans=0;
for(int i=l;i<=mid;i++)
{
while(poi<=r && a[poi].wei<a[i].wei)
{
ans+=n-a[poi].id+1;
poi++;
}
ret+=ans*a[i].id;
}
int len=0;
for(int i=l,j=mid+1;i<=mid || j<=r;)
if((i<=mid) && (j>r || a[i].wei<a[j].wei))
tem[++len]=a[i++];
else
tem[++len]=a[j++];
for(int i=1;i<=len;i++)
a[l+i-1]=tem[i];
return ret;
}
std::ostream& operator<<(std::ostream& os, __int128 t) {
if (t==0) return os << "0";
if (t<0) {
os<<"-";
t=-t;
}
int a[50],ai=0;
memset(a,0,sizeof a);
while (t!=0){
a[ai++]=t%10;
t/=10;
}
for (int i=1;i<=ai;i++) os<<abs(a[ai-i]);
return os<<"";
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i].wei),a[i].id=i;
cout << dfs(1,n) << endl;
return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务