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. CF982 C Cut 'em all!【树/DFS/思维】

    [链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数, ...

  2. NOI2013部分题解

    Day 1 T1:向量内积 直接暴力有60.发现将n个向量合成$n\times d$的矩阵$A$,然后求$A\times A^T$,得到的矩阵包含了所有的答案. 先考虑$k=2$,将答案矩阵和全1矩阵 ...

  3. [Contest20171028]火神的鱼

    火神最爱的就是吃鱼了,所以某一天他来到了一个池塘边捕鱼.池塘可以看成一个二维的平面,而他的渔网可以看成一个与坐标轴平行的矩形.池塘里的鱼不停地在水中游动,可以看成一些点.有的时候会有鱼游进渔网,有的时 ...

  4. [xsy1515]小学生数学题

    题意:求$\begin{align*}\left(\sum\limits_{i=1}^n\dfrac 1i\right)\%\ p^k\end{align*}$ 数学真是太可爱了== 直接推公式 设$ ...

  5. 【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics

    三道题都很类似.给出1741的代码 #include<cstdio> #include<algorithm> #include<cstring> using nam ...

  6. [CF865C]Gotta Go Fast

    题目大意: 一个游戏关卡有$n(n\le50)$个任务,若在$m$秒内按顺序完成所有任务则算作通过当前关卡.每个关卡有三个属性$a_i,b_i,p_i(1\le a_i<b_i\le100,80 ...

  7. 重写规则为什么要options +followsymlinks

    重写规则为什么要options +followsymlinks Web服务器的Apache安装编译成功mod_rewrite编译成模块,但当我在网站根目录下放了一个.htaccess配置文件,却得到了 ...

  8. RxJava 2.x 理解-1

    在RxJava 1.x 系列中,讲解了RxJava的大致用法,因为现在都用RxJava 2了,所以Rxjava 1就不细讲,主要来学习RxJava 2. 基本使用: /** * rajava2 的基本 ...

  9. Python的编码注释# -*- coding:utf-8 -*-

    # -*- coding:utf-8 -*-的主要作用是指定文件编码为utf-8, 因为一般默认的是ASCII码,如果要在文件里面写中文,运行时会出现乱码,加上这句之后会把文件编码强制转换为utf-8 ...

  10. GetAdaptersInfo & GetAdaptersAddresses

    I use GetAdaptersInfo to get MAC addresses of interfaces.   GetAdaptersInfo exist on old and new ver ...