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. python2.5项目:个税计算器程序

    #开发个税计算器:应纳税所得额=工资收入金额-各项社会保险费-起征点(5000元)应纳税额=应纳税所得额*税率—速算扣除数(税率参考图片)m=float(input("请输入你的税前工资:& ...

  2. 使用git将本地仓库上传到远程仓库(转)

    第一步:创建一个工程目录 执行: git init 第二步:把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点".",意为添加文件夹下的所有 ...

  3. Ubuntu定时执行任务(定时爬取数据)

    cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效. 1:编辑crontab文件,用来存放你要执行的命令 s ...

  4. python深挖65万人的明星贴吧,探究上万个帖子的秘密

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 最近一直在关注百度明星吧,发现很多有趣的帖子,于是我就想用python把这 ...

  5. 【模式识别与机器学习】——最大似然估计 (MLE) 最大后验概率(MAP)和最小二乘法

    1) 极/最大似然估计 MLE 给定一堆数据,假如我们知道它是从某一种分布中随机取出来的,可是我们并不知道这个分布具体的参,即“模型已定,参数未知”.例如,我们知道这个分布是正态分布,但是不知道均值和 ...

  6. 数据源管理 | 分布式NoSQL系统,Cassandra集群管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Cassandra简介 1.基础描述 Cassandra是一套开源分布式NoSQL数据库系统.它最初由Facebook开发,用于储存收件箱 ...

  7. Linux内核之 内核同步

    上文我们介绍过进程调度,Linux内核从2.6版本开始支持内核抢占,所以内核很多代码也需要同步保护. 一.同步介绍 1.临界区与竞争条件 所谓临界区(critical regions)就是访问和操作共 ...

  8. Golang 简单爬虫实现,爬取小说

    为什么要使用Go写爬虫呢? 对于我而言,这仅仅是练习Golang的一种方式. 所以,我没有使用爬虫框架,虽然其很高效. 为什么我要写这篇文章? 将我在写爬虫时找到资料做一个总结,希望对于想使用Gola ...

  9. unity探索者之支付宝支付,非第三方插件

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8405044.html 支付宝的sdk接入方式和微信支付比较类似,大部分的工作也基本 ...

  10. Python实现电脑控制,这个库让你可以控制和监控输入设备

    前言 这个库让你可以控制和监控输入设备.对于每一种输入设备,它包含一个子包来控制和监控该种输入设备:pynput.mouse:包含控制和监控鼠标或者触摸板的类.pynput.keyboard:包含控制 ...