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. RxJS 中的观察者和迭代器模式

    目录 前言 观察者模式 迭代器模式 RxJS 中两种模式的结合和实现 小结 参考 1. 前言 RxJS 是一个库,它通过使用observable(可观察对象)序列来编写异步和基于事件的程序.其结合了观 ...

  2. Windows-快速预览文件-QuickLook

    开源.免费的文件快速预览工具, 支持图片.文档.音视频.代码文本.压缩包等多种格式. 获得 Mac OS 空格键快速预览文件相同的体验 效果图 文件夹 音视频 浏览 压缩包,文本 支持的格式: 图片: ...

  3. C#LeetCode刷题之#704-二分查找(Binary Search)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3999 访问. 给定一个 n 个元素有序的(升序)整型数组 num ...

  4. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  5. jQuery的基础效果题

    Jquery第二次考核 之真金不怕火炼 1.  名词解释 实例对象:var p1=new Person();  p1就是实例对象 构造:function Person(){} 原型对象:在 JavaS ...

  6. 2-1关闭和重启linux系统

    0x01 shutdown命令 语法:shutdown [选项][时间][警告信息] 选项 含义 -k 并不执行shutdown,只是发出警告信息 -r 重新启动系统 -h 关闭系统 -c 取消运行s ...

  7. Tun/Tap接口使用指导

    Tun/Tap接口指导 目录 Tun/Tap接口指导 概述 工作机制 创建接口 举例 简单的程序 隧道 拓展 参考 概述 对tun接口的了解需求主要来自于openshift的网络,在openshift ...

  8. Uni-app从入门到实战

    前言 uni-app是一个使用vue.js开发跨平台应用的前端框架,开发者只需要编写一套代码,便可以发布到IOS.Android和微信小程序等多个平台.所以我打算学习下这个框架,快速浏览了一遍官网之后 ...

  9. C#,js和sql实用技巧选1

    我刚开始.net 开发的那几年,差不多每天坚持搜集实用的技巧和代码片断.几年下来也搜集了上千条.现在选出一些不太容易找或者自己有较多体会的,写在这里.内容太多,分两次发. 1.上传文件超过设置允许的最 ...

  10. 《Java从入门到失业》第一章:计算机基础知识(一):二进制和十六进制

    0 前言 最近7年来的高强度工作和不规律的饮食作息,压得我有些喘不过气,身体也陆续报警.2018年下半年的一场病,让我意识到了这个问题的严重性,于是开始强制自己有规律饮食和作息,并辅以健身锻炼,不到2 ...