简单的树状数组求逆序数,不过要用到离散化。。。。刚开始没用离散化,就跪了。。
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 1000005
#define eps 1e-7
#define mod 1000000007
#define INF 99999999
#define lowbit(x) (x&(-x))
typedef long long LL;
using namespace std;
LL tree[maxn];
struct node
{
int number;
LL v;
}tmp[maxn];
int num[maxn];
void add(int x, int c)
{
for(int i=x;i<maxn;i+=lowbit(i)) tree[i]+=c;
}
int sum(int x)
{
int ans=0;
for(int i=x;i>0;i-=lowbit(i)) ans+=tree[i];
return ans;
}
int cmp(node a, node b)
{
return a.v<b.v;
}
int comp(node a, node b)
{
return a.number<b.number;
}
int main(void)
{
int n, i;
LL ans;
while(scanf("%d",&n)!=EOF){
ans=0;
memset(tree, 0, sizeof tree);
for(i=0;i<n;i++){
tmp[i].number=i+1;
scanf("%I64d",&tmp[i].v);
}
sort(tmp, tmp+n, cmp);
for(i=0;i<n;i++) tmp[i].v=i+1;
sort(tmp, tmp+n, comp);
for(i=0;i<n;i++) num[i+1]=tmp[i].v;
for(i=n;i>=1;i--){
ans+=sum(num[i]);
add(num[i], 1);
}
printf("%I64d\n", ans);
}
}