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. HDU 2523 sort (hash)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> using ...

  2. ASP.NET Core 2.2 基础知识(十一) ASP.NET Core 模块

    ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...

  3. websocket、文件上传

    支持情况: 浏览器实现了websocket的浏览器:Chrome Supported in version 4+ Firefox Supported in version 4+ Internet Ex ...

  4. Scrum生命周期

    Recently while cleaning up my photo albums I found some interesting old pictures which were captured ...

  5. 某dalao贼快的hash?

    #include<map> #include<cstdio> #include<iostream> #include<ext/pb_ds/assoc_cont ...

  6. POJ 3180 The Cow Prom(SCC)

    [题目链接] http://poj.org/problem?id=3180 [题目大意] N头牛,M条有向绳子,能组成几个歌舞团?要求顺时针逆时针都能带动舞团内所有牛. [题解] 等价于求点数大于1的 ...

  7. 安装virtualenvwrapper

    理解:virtualenv 和 virtualenvwrapper 是两种东西,前者可以单独使用,后者是管理前者的工具,尤其是当有多个 virtualenv(隔离环境时).所以下面的配置都是在为了使用 ...

  8. [Eclipse]--Error:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path.

    一段时间没用eclipse后,再去打开以前的项目,发现一打开前线标红.查看错误的时候,如下图所示: Error:The superclass "javax.servlet.http.Http ...

  9. 【转载】linux2.6内核initrd机制解析

    题记 很久之前就分析过这部分内容,但是那个时候不够深入,姑且知道这么个东西存在,到底怎么用,来龙去脉咋回事就不知道了.前段时间工作上遇到了一个initrd的问题,没办法只能再去研究研究,还好,有点眉目 ...

  10. sqlserver 删除临时表

    sqlserver 删除临时表 if object_id('tempdb..#tempTable') is not null Begin drop table #tempTable End