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. OAuth2.0-4整合网关

    .antMatchers("/**").access("#oauth2.hasScope('scope1')")上面这行代码,只是控制大范围的访问权限,具体到方 ...

  2. Java实现短信验证码

    前言 本人使用的是阿里短信服务,一开始尝试了许多不同的第三方短信服务平台,比如秒滴科技.梦网云通讯.当初开始为什么会选择这两个,首先因为,他们注册就送10元钱(#^.^#),但是后来却发现他们都需要认 ...

  3. 14、Cahin of Responsibility 责任链 COR设计模式

    1.责任链模式 chain of responsibility 责任链模式 责任链,顾名思义,就是用来处理相关事务责任的一条执行链,执行链上有多个节点,每个节点都有机会(条件匹配)处理请求事务,如果某 ...

  4. Docker 搭建 SonarQube

    Docker 搭建 SonarQube Docker 搭建 SonarQube 步骤 创建项目目录 mkdir -p /usr/local/sonarqube && cd /usr/l ...

  5. 全面介绍eBPF-概念

    全面介绍eBPF-概念 前面介绍了BCC可观测性和BCC网络,但对底层使用的eBPF的介绍相对较少,且官方欠缺对网络方面的介绍.下面对eBPF进行全面介绍. 目录 全面介绍eBPF-概念 BPF概述 ...

  6. geth常用命令

    转载地址 https://blog.csdn.net/qq_36124194/article/details/83686823 geth常用命令 初始化私链 geth --datadir /path/ ...

  7. [算法入门]——深度优先搜索(DFS)

    深度优先搜索(DFS) 深度优先搜索叫DFS(Depth First Search).OK,那么什么是深度优先搜索呢?_? 样例: 举个例子,你在一个方格网络中,可以简单理解为我们的地图,要从A点到B ...

  8. SQLserver 查询某个表的字段及字段属性

    SELECT C.name as [字段名],T.name as [字段类型] ,convert(bit,C.IsNullable) as [可否为空] ,convert(bit,case when ...

  9. mycat数据库集群系列之mycat读写分离安装配置

    最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据库安装.mycat部署安装.数据库之读写分离 ...

  10. iNeuOS工业互联平台,WEB组态(iNeuView)集成图报组件,满足实时数据图形化展示的需求

    目       录 1.      概述... 1 2.      平台演示... 2 3.      应用过程... 3 4.      实时数据展示效果... 5 1.   概述 市场和开源社区有 ...