# nyoj117求逆序数(离散化+树状数组/归并排序)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1000005;
int reflect[N],c[N],n;
struct node
{
int val;
int pos;
}s[N];
bool cmp(node x,node y)
{
if(x.val == y.val) return x.pos < y.pos;
return x.val < y.val;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x <= n)
{
c[x] += 1;
x += lowbit(x);
}
}
long long getsum(int x)//统计比x小的数
{
long long sum = 0;
while(x > 0)
{
sum += c[x];
x -= lowbit(x);
}
return sum;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i = 1; i <= n; i ++)
scanf("%d",&s[i].val), s[i].pos = i;
sort(s+1,s+n+1,cmp);
for(i = 1; i <= n; i ++)
reflect[s[i].pos] = i;
memset(c,0,sizeof(c));
long long ans = 0;
for(i = 1; i <= n; i ++)
{
update(reflect[i]);
ans += i - getsum(reflect[i]);//i表示已经插入的数的个数
}
printf("%lld\n",ans);
}
return 0;
}


#include<stdio.h>
int a[1000005],temp[1000005];
long long sum;
void merge(int left,int mid,int right)
{
int i = left,j = mid + 1;
int k = left;
while(i <=mid && j <= right)
{
if(a[i] <= a[j])
temp[k ++] = a[i ++];
else
{
temp[k ++] = a[j ++];
sum += mid - i + 1;//关键
}
}
while(i <= mid)
temp[k ++] = a[i ++];

while(j <= right)
temp[k ++] = a[j ++];

for(i = left ; i <= right; i ++)
a[i] = temp[i];
}
void msort(int left,int right)
{
if(left < right)
{
int mid = (left + right) >> 1;
msort(left,mid);
msort(mid + 1,right);
merge(left,mid,right);
}
}
int main()
{
int i,n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i = 0;i < n; i ++)
scanf("%d",&a[i]);
sum = 0;
msort(0,n - 1);
printf("%lld\n",sum);
}
return 0;
}

• 本文已收录于以下专栏：

## poj2299Ultra-QuickSort【树状数组求逆序数、离散化】、【归并排序模板】

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce...

## poj-2299-Ultra-QuickSort-归并排序求逆序数--或树状数组

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce...

## 归并排序 树状数组 求逆序数

Description Bob is a game programming specialist. In his new car race game, there are some racers...

## 【树状数组or归并排序求逆序数】HDU 1394

http://acm.hdu.edu.cn/showproblem.php?pid=1394  先说一下逆序数的概念： 在一个排列中，如果一对数的前后位置与大小顺序相反，即前面的数大于后面的数...
• leolin_
• 2011年05月17日 13:22
• 679

举报原因： 您举报文章：nyoj117求逆序数(离散化+树状数组/归并排序) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)