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


题意:
求逆序数

#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],n,tot,r[N]; struct node
{
LL x,s,id;
} a[N]; 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;
}

SGU180:Inversions(树状数组)的更多相关文章

  1. SGU180(树状数组,逆序对,离散)

    Inversions time limit per test: 0.25 sec. memory limit per test: 4096 KB input: standard output: sta ...

  2. HDU5196--DZY Loves Inversions 树状数组 逆序数

    题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...

  3. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  4. Infinite Inversions(树状数组+离散化)

    思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085 There is an infinite sequence cons ...

  5. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  6. Dynamic Inversions 50个树状数组

    Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...

  7. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  8. CF #301 E:Infinite Inversions(逆序数,树状数组)

    A-Combination Lock  B-School Marks   C-Ice Cave   D-Bad Luck Island   E-Infinite Inversions E:Infini ...

  9. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

随机推荐

  1. Linux命令之vi/vim(一)

    vi仅仅是一个文本编辑器,可以给字符着色,可以自动补全,但不具备windows下word的排版功能.Vim是Vi improved的缩写,是vi的改进版. vi和vim的区别: 多级撤销.在vi中按u ...

  2. 【莫队算法】URAL - 2080 - Wallet

    http://www.cnblogs.com/icode-girl/p/5783983.html 要注意卡片没有都被使用的情况. #include<cstdio> #include< ...

  3. 【并查集】bzoj1015 [JSOI2008]星球大战starwar

    倒着处理删点,就变成了加点,于是并查集. #include<cstdio> using namespace std; #define N 400001 int fa[N],kill[N], ...

  4. 【R笔记】glm函数报错原因及解析

    R语言glm函数学习:  [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. glm函数介绍: glm(for ...

  5. Asp.Net MVC part45 过滤器、模板页

    过滤器 使用方式自定义类继承自相应的类或接口,重写方法,作为特性使用在控制器类中重写方法 特性方式的使用注意:如果继承自接口需要让类实现FilterAttribute,才可以作为特性使用使用方式1:作 ...

  6. iOS 公司开发者账号申请清单

    公司开发者账号申请清单: Apple ID账号申请: (有账号请提供账号密码) Apple ID:       (最好是公司邮箱账号) Apple ID密码:  (大于8位, 字母或数字组成,  包含 ...

  7. projecteuler----&gt;problem=11----Largest product in a grid

    In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 1 ...

  8. iOS:扩展UIColor,支持十六进制颜色设置

    来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...

  9. 【Todo】Nodejs学习计划

    /Users/baidu/Documents/Data/Interview/Web-Server开发/深入浅出Node.js-f46c.pdf /Users/baidu/Documents/Data/ ...

  10. mongodb权限管理(转)

    Mongodb 预定义角色 Mongodb 中预定义了一些角色,把这些角色赋予给适当的用户上,用户就只能进行角色范围内的操作. 数据库用户角色 (所有数据库都有) read 用户可以读取当前数据库的数 ...