In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 ,
Ultra-QuickSort produces the output 
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
const int maxn=5e5+;
typedef long long ll;
using namespace std;
struct node
{
ll l,r;
ll sum;
}tree[maxn<<];
int a[maxn],sub[maxn],cnt; void pushup(int m)
{
tree[m].sum=(tree[m<<].sum+tree[m<<|].sum);
}
//void pushdown(int m)
//{
// if(tree[m].lazy)
// {
// tree[m<<1].lazy=tree[m].lazy;
// tree[m<<1|1].lazy=tree[m].lazy;
// tree[m<<1].sum=tree[m].lazy*(tree[m<<1].r-tree[m<<1].l+1);
// tree[m<<1|1].sum=tree[m].lazy*(tree[m<<1|1].r-tree[m<<1|1].l+1);
// tree[m].lazy=0;
// }
// return ;
//}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
tree[m].sum=;
if(l==r)
{
tree[m].sum=;
return ;
}
int mid=(tree[m].l+tree[m].r)>>;
build(m<<,l,mid);
build(m<<|,mid+,r);
pushup(m);
return ;
}
void update(int m,int index ,int val)
{
if(tree[m].l==index&&tree[m].l==tree[m].r)
{
tree[m].sum++;
return ;
}
int mid=(tree[m].l+tree[m].r)>>;
if(index<=mid)
{
update(m<<,index,val);
}
else
{
update(m<<|,index,val);
}
pushup(m);
return ;
}
ll query(int m,int l,int r)
{
if(l>r)
{
return ;
}
if(tree[m].l==l&&tree[m].r==r)
{
return tree[m].sum;
}
// pushdown(m);
int mid=(tree[m].l+tree[m].r)>>;
if(r<=mid)
{
return query(m<<,l,r);
}
else if(l>mid)
{
return query(m<<|,l,r);
}
else
{
return query(m<<,l,mid)+query(m<<|,mid+,r);
}
} int main()
{
int n;
while(cin>>n)
{
if(n==)
{
break;
}
for(int i=;i<n;++i)scanf("%d",&sub[i]),a[i]=sub[i];
sort(sub,sub+n);
int size=unique(sub,sub+n)-sub;
for(int i=;i<n;i++)
a[i]=lower_bound(sub,sub+size,a[i])-sub+;
build(,,size);
ll sum=;
for(int t=;t<n;t++)
{
sum+=query(,a[t]+,size);
update(,a[t],);
}
printf("%lld\n",sum); }
return ;
}

最新文章

  1. 【Flask】Flask快速玩框架
  2. 20145225《Java程序设计》 第9周学习总结
  3. sql server 2005 32位+64位、企业版+标准版、CD+DVD 下载地址大全 .
  4. java类初始化优先级
  5. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)
  6. ASP.NET多次点击提交按钮以及Session超时和丢失过期问题
  7. lucene索引并搜索mysql数据库[转]
  8. HDOJ 2073 无限的路
  9. An internal error occurred during: &quot;Requesting JavaScript AST from selection&quot;. GC overhead limit exc
  10. excel 函数1
  11. mybatis-ehcache整合中出现的异常 ibatis处理器异常(executor.ExecutorException)解决方法
  12. Cpython解释器下实现并发编程
  13. [HDU1693]Eat the Trees
  14. Redis 内存模型
  15. Python基础(下)
  16. 《剑指offer》-数组中只出现一次的数字
  17. POJ2478(SummerTrainingDay04-E 欧拉函数)
  18. C# 房贷计算器
  19. SSH新学,关于面向对象的看法
  20. LM3S之boot loader学习笔记-2

热门文章

  1. Python 错误 异常
  2. 使用Python语言通过PyQt5和socket实现UDP服务器
  3. 2020-07-20:你觉得redis有什么缺点,给你改进的话你会怎么改进?
  4. JavaScript 通过prototype改变原型的两种方式
  5. 手把手教你NLTK WordNet使用方法
  6. 【Codeforces】CF Round #592 (Div. 2) - 题解
  7. C++ 不具有继承关系的类之间的显式,隐式转换 2013-07-11 15:41
  8. GUAVA-ListenableFuture实现回调
  9. 浏览器自动化的一些体会8 HttpWebRequest的几个问题
  10. 推荐一看的blog