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. (11)python 模块和包

    一.导入模块和包 模块相当于一个.py文件,包相当于带有个__init__.py一个文件夹,既可按模块导入也可按包导入. 1.导入模块或包 import 包名或模块名 (as 别名),包名或模块名 ( ...

  2. HDU 2549 壮志难酬(字符串,处理小数点)

    /* 给你一个小数x,让你算出小数点后第n位是什么,(1 <= n <= 6) Input 首先输入一个t,表示有t组数据,跟着t行: 每行输入一个小数(输入数据保证一定是a.b的形式,为 ...

  3. poj3233(等比矩阵求和)

    poj3233 题意 给出一个 \(n \times n\) 的矩阵 \(A\) ,求 \(A + A^2 + A^3 + ... + A^k\) . 分析 构造矩阵 \[ \begin{bmatri ...

  4. Linux命令之yum

    yum [选项] [命令] [包] yum命令是RedHat和SUSE基于rpm的软件管理器.能够从指定的服务器自动下载rpm包并安装,可以自动处理依赖关系,并且可以一次安装所有依赖关系. (扩展:域 ...

  5. HDU 6313: Hack it

    Hack It Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. Linux Shell 量的自增

    Linux Shell 中写循环时,常常要用到变量的自增,现在总结一下整型变量自增的方法.我所知道的,bash中,目前有五种方法:1. i=`expr $i + 1`;2. let i+=1;3. ( ...

  7. 【计算几何】URAL - 2101 - Knight's Shield

    Little Peter Ivanov likes to play knights. Or musketeers. Or samurai. It depends on his mood. For pa ...

  8. 1.6(学习笔记)Session

    一. Session简介 Session是用于解决HTTP无状态问题,HTTP协议本身是没有状态的, 就类似一个没有记性的商人,每次只交易当前的货物,交易完后就忘记了 以前的交易历史.我们和商人交易时 ...

  9. Scala高手实战****第20课:Scala提取器、注解深度实战详解及Spark源码鉴赏

    Spark中的源码的提取器和注解 @SparkContext.scala @ volatile 线程专用 保证线程间共享内容的一致性 @volatile private var _dagSchedul ...

  10. TCP长连接与短连接的区别(转)

    1. TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次 ...