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 ;
}

POJ-2299-Ultra-QuickSort(单点更新 + 区间查询+离散化)的更多相关文章

  1. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  2. hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询

    单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...

  3. HihoCoder - 1336 二维数状数组(单点更新 区间查询)

    You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 ope ...

  4. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  5. NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ->  Link1  <- -> Link2  <- 以上两题题意是一样 ...

  6. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

  7. HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)

    题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...

  9. hdu1754线段树的单点更新区间查询

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. python5.3二进制文件的读写

    fh=open(r"C:\1.png","rb")#转换成二进制数据data=fh.read()#对二进制数据进行读取 fh1=open(r"C:\2 ...

  2. “随手记”开发记录day20

    练习软件的展示,尽量将软件全方面的展示给大众,希望不要像上次一样有许多遗漏的地方,让其他团队以为我们的软件没有完善的功能.

  3. 用Python绘制一套“会跳舞”的动态图形给你看看

    在读技术博客的过程中,我们会发现那些能够把知识.成果讲透的博主很多都会做动态图表.他们的图是怎么做的?难度大吗?这篇文章就介绍了 Python 中一种简单的动态图表制作方法. 看这优美的舞姿 很多人学 ...

  4. 【模式识别与机器学习】——3.5Fisher线性判别

    ---恢复内容开始--- 出发点 应用统计方法解决模式识别问题时,一再碰到的问题之一就是维数问题. 在低维空间里解析上或计算上行得通的方法,在高维空间里往往行不通. 因此,降低维数有时就会成为处理实际 ...

  5. canvas图片编辑操作:缩放、移动、保存(PC端+移动端)

    最近在写canvas关于图片的操作,看了网上的代码基本都是不行的,于是就自己写了一个. html代码 <canvas id="myCanvas" width="37 ...

  6. IPSec传输模式下的ESP报文的装包和拆包过程

    IPSec协议定义 IPsec将IP数据包的内容在装包过程在网络层先加密再传输,即便中途被截获,由于缺乏解密数据包所必要的密钥,攻击者也无法获取里面的内容. IPsec 对数据进行加密的方式 加密模式 ...

  7. scanf函数与getchar函数

    #define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#include<stdlib.h&g ...

  8. 听说同学你搞不懂Java的LinkedHashMap,可笑

    先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个有颜值却假装靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有我精心为你准备的一线大厂面试题 ...

  9. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

  10. Centos7修改用户名

    系统原来的用户lou,改为scrapy,要改以下个地方,注:没有修改对应密码 1.   # vi /etc/passwd 修改其中的用户名部分.用户组部分.主目录部分2.  修改用户组的配置文件 # ...