poj2299 离散化+树状数组
来源:互联网 发布:中档女装品牌 知乎 编辑:程序博客网 时间:2024/05/20 01:47
/******************** * Author:fisty * Data:2014-12-8 * poj2299 * 树状数组求逆序数+离散化 *********************/#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define MAX_N 599999typedef long long ll;int bit[MAX_N], vec[MAX_N];int n;struct node{ int v; int id;}a[500100];int sum(int i){ int sum = 0; while(i > 0){ sum += bit[i]; i -= (i&(-i)); } return sum;}void add(int i, int x){ while(i <= MAX_N){ bit[i] += x; i += (i&(-i)); }}bool cmp(struct node t1, struct node t2){ return t1.v < t2.v;}void solve(){ ll ans = 0; for(int i = 1;i <= n; i++){ add(vec[i], 1); ans += i - sum(vec[i]); } printf("%lld\n", ans); }int main(){ while(scanf("%d", &n) && n){ for(int i = 1;i <= n; i++){ scanf("%d", &a[i].v); a[i].id = i; } memset(bit , 0, sizeof(bit)); sort(a+1, a + n + 1, cmp); //离散化 for(int i = 1;i <= n; i++){ vec[a[i].id] = i; } solve(); } return 0;}
0 0
- POJ2299 树状数组+离散化
- poj2299 离散化+树状数组
- poj2299 树状数组+离散化
- poj2299离散化,树状数组
- POJ2299 树状数组+离散化
- poj2299之树状数组+离散化
- poj2299 (树状数组+离散化)
- poj2299(离散化+树状数组求逆序)
- poj2299 Ultra-QuickSort (树状数组+离散化)
- POJ2299 Ultra-QuickSort 树状数组+离散化
- poj2299 Ultra-QuickSort 树状数组 + 离散化
- POJ2299-Ultra-QuickSort(树状数组+离散化)
- 离散化+树状数组 求逆序数POJ2299
- POJ2299 Ultra-QuickSor[树状数组+离散化 / 归并排序]
- poj2299:Ultra-QuickSort(树状数组+离散化)
- poj2299 Ultra-QuickSort(归并排序 || 离散化+树状数组)
- poj2299 (离散化+树状数组)或(归并排序)
- 【树状数组】poj2299 Ultra-QuickSort(离散化+树状数组求逆序数)
- DOM学习笔记三
- 最小生成树
- shell大小中括号作用
- Minimum Window Substring leetcode
- C语言结构体对齐
- poj2299 离散化+树状数组
- PHP时间戳使用方法(实际上跟乐观锁一个意思!)
- OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类
- 查询mysql总结和预处理一
- 网络爬虫工作原理分析
- Linux命令行与shell脚本编程-Part1-shell终端
- HDOJ-1686 Oulipo ---KMP
- JSP生命周期
- Java开发常见问题之 - java.lang.OutOfMemoryError:PermGen space