| Time Limit: 7000MS |  | Memory Limit: 65536K | 
| Total Submissions: 46293 |  | Accepted: 16846 | 
Description
 In
 this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
In
 this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence Input
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
我们先将原数组每个值附上一个序号index,再将它排序。如题目的例子:
num: 9 1 0 5 4
index: 1 2 3 4 5
排序后:
num: 0 1 4 5 9
index: 3 2 5 4 1
然后由于排序后num为0的点排在原来数组的第3个,所以为了将它排到第一个去,那就至少需要向前移动两次,同时它也等价于最小的数0之前有2个数比它大(所以要移动两次),将0移到它自己的位置后,我们将0删掉(目的是为了不对后面产生影响)。再看第二大的数1,它出现在原数组的第二个,他之前有一个数比它大所以需要移动一次。这样一直循环下去那么着5个数所需要移动的次数就是:
num: 0 1 4 5 9
次数 2 1 2 1 0
将次数全部要加起来就是最后所需要移动的总次数。
方法章爷是已经告诉我了,但是最初我一直是觉得不好实现。到后来才慢慢、慢慢弄好。方法就是在建一棵树时,不是直接将原来的num放进树里面,而是将它的下标放进树里面,最初每个节点上赋值为1.然后当查找第一个num时,由于是找的下标为3的位置,所以我们就直接找区间[1,3)之间有多少个1(就是求前导和),这里面1的个数就是第一个num=0索要移动的次数,然后我们把0去掉,其实也就是吧下标为3的那个1去掉。这样每个值就依次计算出来了。
<span style="font-family:KaiTi_GB2312;">/*******************************************************************************
 * Author :          jinbao
 * Email :           dongjinbao913106840144@gmail.com
 * Last modified :   2015-05-04 00:19
 * Filename :        poj2299.cpp
 * Description :     
 * *****************************************************************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX = 500000+5;
int tree[4*MAX];
struct array{
	int index;
	int value;
}a[MAX];
int n;
bool cmp(array x,array y){
	if (x.value<y.value)
		return true;
	return false;
}
void build(int node,int begin,int end){
	if (begin==end)
		tree[node]=1;
	else{
		build(node*2,begin,(begin+end)/2);
		build(node*2+1,(begin+end)/2+1,end);
		tree[node]=tree[node*2]+tree[node*2+1];
	}
}
int query(int node,int begin,int end,int left,int right){
	if (left>right)
		return 0;
	if (end<left || right<begin)
		return 0;
	if (begin>=left && right>=end)
		return tree[node];
	return query(node*2,begin,(begin+end)/2,left,right)+query(node*2+1,(begin+end)/2+1,end,left,right);
}
void update(int node,int begin,int end,int index,int value){
	if (begin==end){
		tree[node]=value;
		return;
	}
	int mid=(begin+end)/2;
	if (mid>=index)
		update(node*2,begin,mid,index,value);
	else
		update(node*2+1,mid+1,end,index,value);
	tree[node]=tree[node*2]+tree[node*2+1];
}
int main(){
	while (~scanf("%d",&n)){
		if (n==0)
			break;
		for (int i=1;i<=n;i++){
			scanf("%d",&a[i].value);
			a[i].index=i;
		}
		sort(a+1,a+n+1,cmp);
		build(1,1,n);;
		ll ans=0;
		for (int i=1;i<=n;i++){
			ans+=query(1,1,n,1,a[i].index-1);
			update(1,1,n,a[i].index,0);
		}
		printf("%lld\n",ans);
	}
	return 0;
}
</span>poj2299 Ultra-QuickSort(线段树计数问题)
原文:http://blog.csdn.net/codeforcer/article/details/45467571