先一遍reverse+逆序对个数。
要开long long啊。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100500
using namespace std;
int n,a[maxn],pos[maxn],l,bit[maxn];
long long ans=;
int lowbit(int x) {return (x&(-x));}
void rev(int l,int r)
{
for (int i=l;i<=r;i++)
{
if ((i<<)>=l+r) break;
swap(a[i],a[l+r-i]);
}
}
int ask(int now)
{
int ret=;
for (int i=now;i>=;i-=lowbit(i))
ret+=bit[i];
return ret;
}
void insert(int now,int val)
{
for (int i=now;i<=n;i+=lowbit(i))
bit[i]+=val;
}
int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
l=;
for (int i=;i<=n;i++)
{
if (a[i]>a[i-])
{
rev(l,i-);
l=i;ans++;
}
}
rev(l,n);ans++;
for (int i=;i<=n;i++) pos[a[i]]=i;
for (int i=n;i>=;i--)
{
ans+=(long long)ask(pos[i]-);
insert(pos[i],);
}
printf("%lld\n",ans);
return ;
}
//Flaze naive!