POJ 2299 -Ultra-QuickSort-树状数组求逆序数
2016-03-15 22:13
459 查看
POJ 2299 Ultra-QuickSort
使用树状数组记录逆序对数。
把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数。
使用树状数组记录逆序对数。
把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数。
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn = 500005; int reflect[maxn],c[maxn],N; struct Node { int val; int pos; bool operator < (const Node &r) const { return val < r.val; } }node[maxn]; int lowbit(int x) { return x&(-x); } void update(int x) { while(x<=N) { c[x] += 1; x += lowbit(x); } } int getsum(int x) { int sum = 0; while(x > 0) { sum += c[x]; x -= lowbit(x); } return sum; } int main() { while(~scanf("%d",&N) && N) { for(int i=1;i<=N;i++) { scanf("%d",&node[i].val); node[i].pos = i; } sort(node+1,node + N+1); for(int i=1;i<=N;i++) reflect[node[i].pos] = i; memset(c,0,sizeof c); long long ans = 0; for(int i=1;i<=N;i++) { update(reflect[i]); ans += i - getsum(reflect[i]); } printf("%lld\n",ans); } }
相关文章推荐
- HDU 5297 Y sequence 容斥/迭代
- UINavigationController和UIBarButtonItem的例子
- PAT 1007. Maximum Subsequence Sum (25)
- iOS UINavigationItem.backBarButtonItem 设置-利用系统自带返回箭头
- UICollectionViewCell水平缩放,中间大两边小效果的实现
- jquer 基础篇 dom操作
- jquer基础篇二
- UVALive 4255 Guess【拓扑排序 】
- 62. Unique Paths
- 线段树——Ultra-QuickSort
- iOS:SVN出错 Description : The working copy is locked due to a previous error
- UI基本控件
- request.getParameterMap()使用方法
- 常见UI界面库
- hue中使用oozie的workflow执行mr
- hue中使用oozie的workflow执行mr
- UML 顺序图(时序图) sequence diagram
- codeForces-Longest Subsequence
- miniui 结束日期不能小于起始日期
- Subset sequence