SGU180:Inversions(树状数组)
2015-06-24 09:46
369 查看
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5 2 3 1 5 4
Output
3
题意:
求逆序数
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5 2 3 1 5 4
Output
3
题意:
求逆序数
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <bitset> #include <list> #include <algorithm> #include <climits> using namespace std; #define lson 2*i #define rson 2*i+1 #define LS l,mid,lson #define RS mid+1,r,rson #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define LL long long #define N 67000 #define INF 0x3f3f3f3f #define EXP 1e-8 #define lowbit(x) (x&-x) const int mod = 1e9+7; LL c ,n,tot,r ; struct node { LL x,s,id; } a ; int cmp(node a,node b) { if(a.x!=b.x) return a.x<b.x; return a.id<b.id; } LL sum(LL x) { LL ret = 0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret; } void add(LL x,LL d) { while(x<=n) { c[x]+=d; x+=lowbit(x); } } int main() { LL i,j,k; while(~scanf("%lld",&n)) { MEM(c,0); for(i = 1; i<=n; i++) { scanf("%lld",&a[i].x); a[i].id = i; } sort(a+1,a+1+n,cmp); for(i = 1; i<=n; i++) { r[a[i].id] = i; } LL ans = 0; for(i = 1; i<=n; i++) { add(r[i],1); ans+=(i-sum(r[i])); } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 联网查看图片的Demo
- 使用windbg調試iis應用程序池崩溃的asp.net 2.0 bug.
- Mysql导入大容量SQL文件数据问题
- 许多js框架或js库的min版本是怎么做出来的?
- android开发 获取logcat日志并记录(方便离线调试)
- JScrollPane中添加JPanel不出现滚动条
- mybatis 模糊查询报错: java.sql.SQLException: ORA-01006: 绑定变量不存在
- 嫌工资少,先来看看你值多少钱
- jQuery的$("#id")和document.getElementById("id")的区别
- Devexpress VCL Build v2015 vol 15.1.2发布
- Block
- 【动态规划】bzoj1664 [Usaco2006 Open]County Fair Events 参加节日庆祝
- extern "C" 使用的原因
- 各分词工具介绍
- Ubuntu Server 管理权限解读
- Linux的SOCKET编程详解
- Swift 2 if case 用法
- [技巧篇]06.关于防止SQL注入的方式,不使用预处理
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- 清华附小给的书单