1774 最接近神的人
早就做过,没理解透
树状数组的模板题,不过需要进行离散化排序
很简单
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#define lowbit(x) x&-x
using namespace std;
const int SIZE=5e5+5;
typedef long long ll;
ll n;
ll a[SIZE];
ll b[SIZE];
ll tree[SIZE];
ll ans;
inline bool cmp (int x, int y)
{
return a[x]==a[y]?x>y:a[x]>a[y];
}
inline void add (int x)
{
for (int i=x;i<=n;i+=lowbit(i))
tree[i]++;
}
inline int ask(int x)//查询
{
int sum = 0;
for (int i=x;i>0;i-=lowbit(i))
sum+=tree[i];//进行求和
return sum;
}
int main ()
{
cin>>n;
for (int i = 1; i <= n; i ++)
{
cin>>a[i];
b[i]=i;
}
sort (b+1,b+1+n,cmp);//进行排序
for (int i=1;i<=n;i++)
{
add(b[i]);
ans+=ask(b[i] - 1);
}
cout<<ans<<endl;
return 0;
}