###### http://acm.hdu.edu.cn/showproblem.php?pid=1394&&线段树之求逆序数
#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#define N 5005
#define L(x)(x<<1)
#define R(x) (x<<1|1)
#define M(x,y) ((x+y)>>1)
using namespace std;
typedef struct str
{
int l;
int r;
int key;
}Node;
Node node[N<<1];
int value[N];
void bulid(int t,int l,int r)
{
node[t].l=l;
node[t].r=r;
node[t].key=0;
if(l==r-1)return ;
int mid=M(l,r);
bulid(L(t),l,mid);
bulid(R(t),mid,r);
}
void update(int t,int a)
{
node[t].key++;
if(node[t].l==(node[t].r-1))  return;
int mid=M(node[t].l,node[t].r);
if(a>=mid) update(R(t),a);
else update(L(t),a);
}
int Quary(int t,int a,int b)
{
if(node[t].l==a&&node[t].r==b) return node[t].key;
int mid=M(node[t].l,node[t].r);
if(a>=mid) return Quary(R(t),a,b);
else
{
if(b<=mid) return Quary(L(t),a,b);
else return  Quary(L(t),a,mid)+Quary(R(t),mid,b);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int sum=0;
bulid(1,0,n);

for(int i=1;i<=n;++i)
{
scanf("%d",&value[i]);
sum+=Quary(1,value[i],n);
update(1,value[i]);
}
int k=sum;
for(int i=n;i>=2;--i)
{
sum=sum+value[i]-(n-1-value[i]);
if(sum<k) k=sum;
}
printf("%d\n",k);
}return 0;
}

#### hdu - 1576 - A/B（乘法逆元）

2014-11-03 13:07:08

#### http://acm.hdu.edu.cn/showproblem.php?pid=1541

2012-02-27 19:51:20

#### Red and Black&&http://acm.hdu.edu.cn/showproblem.php?pid=1312

2012-07-25 13:17:27

#### Robberies&&http://acm.hdu.edu.cn/showproblem.php?pid=2955

2012-01-10 09:46:54

#### 字符输入时注意的格式（特殊情况）http://acm.hdu.edu.cn/showproblem.php?pid=1860

2013-05-05 19:33:55

#### HDU-1274-展开字符串（dfs分段字符串处理）

2017-03-04 13:41:00

#### POJ2255 HDU1710【树的遍历】【水题】

2017-03-28 19:31:04

#### HDU 2141 Can you find it? 二分查找

2015-07-29 19:35:01

#### 题解： HDU 2141 Can you find it? （二分查找）

2015-12-06 19:03:48

#### HDU 2866 Special Prime 找规律

2016-03-18 18:55:21

## 不良信息举报

http://acm.hdu.edu.cn/showproblem.php?pid=1394&&线段树之求逆序数