码迷,mamicode.com
首页 > 编程语言 > 详细

poj 2299 Ultra-QuickSort(线段树/树状数组/归并 求逆序对)

时间:2015-07-23 00:32:44      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

Problem: 2299		User: shu_dayang
Memory: 7380K		Time: 500MS
Language: C++		Result: Accepted
Source Code
//树状数组 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> typedef long long LL; #define MAXN 500005 #define MID(l,r) ((l + r) >> 1) using namespace std; int c[MAXN],aa[MAXN]; int n; struct Node { int val; int order; }a[MAXN]; bool cmp(Node a, Node b) { return a.val < b.val; } int lowbit(int x) { return x &(-x); } void update(int x) { while(x <= n) { c[x] += 1; x += lowbit(x); } } int query(int x) { int sum = 0; while(x > 0) { sum += c[x]; x -= lowbit(x); } return sum; } int main() { LL ans; while(~scanf("%d",&n) && n != 0) { memset(c,0,sizeof(c)); for(int i = 1; i <= n; i++) { scanf("%d",&a[i].val); a[i].order = i; } sort(a+1,a+1+n,cmp); for(int i = 1; i <= n; i++) aa[a[i].order] = i; ans = 0; for(int i = 1; i <= n; i++) { update(aa[i]); ans += i - query(aa[i]); } printf("%I64d\n",ans); } return 0; }

 

poj 2299 Ultra-QuickSort(线段树/树状数组/归并 求逆序对)

标签:

原文地址:http://www.cnblogs.com/yong-hua/p/4669042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!