Ultra-QuickSort

Time Limit: 7000MS
Memory Limit: 65536K

Description

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

::本题其实就是要求出有多少逆序对。本题还要虚拟化,因为0<=a[i]<=999,999,999,开一个数组大小为1,000,000,000*4铁定超内存

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: using namespace std;

   6: #define lson l,m,rt<<1

   7: #define rson m+1,r,rt<<1|1

   8: typedef long long ll;

   9: const int maxn=555555;

  10: int col[maxn<<2];

  11: int a[maxn],b[maxn],n;

  12:  

  13: void build(int l,int r,int rt)

  14: {

  15:     col[rt]=0;

  16:     if(l==r) return ;

  17:     int m=(r+l)>>1;

  18:     build(lson);

  19:     build(rson);

  20: }

  21:  

  22: int find(int x)

  23: {

  24:     int l=1,r=n;

  25:     while(l<=r)

  26:     {

  27:         int m=(l+r)>>1;

  28:         if(x==a[m]) return m;

  29:         if(x>a[m]) l=m+1;

  30:         else r=m-1;

  31:     }

  32:     return 0;

  33: }

  34:  

  35: void update(int p,int l,int r,int rt)

  36: {

  37:     col[rt]++;

  38:     if(l==r) return ;

  39:     int m=(l+r)>>1;

  40:     if(p<=m) update(p,lson);

  41:     else update(p,rson);

  42: }

  43:  

  44: int query(int p,int l,int r,int rt)

  45: {

  46:     if(p<=l) return col[rt];

  47:     int m=(l+r)>>1;

  48:     int ans=0;

  49:     if(p<=m)

  50:         ans=col[rt<<1|1]+query(p,lson);

  51:     else

  52:         ans=query(p,rson);

  53:     return ans;

  54: }

  55:  

  56: int main()

  57: {

  58:     int i;

  59:     while(scanf("%d",&n),n)

  60:     {

  61:         build(1,n,1);

  62:         for(i=1; i<=n; i++)

  63:         {

  64:             scanf("%d",a+i);

  65:             b[i]=a[i];

  66:         }

  67:         sort(a+1,a+n+1);//让数组a升序排序,那么等下b就可以通过a来求出对应的树是第几大(这算是虚拟化吧)

  68:         ll sum=0;

  69:         for(i=1; i<=n; i++)

  70:         {

  71:             int t=find(b[i]);

  72:             sum+=(ll)query(t,1,n,1);

  73:             update(t,1,n,1);

  74:         }

  75:         printf("%lld\n",sum);

  76:     }

  77:     return 0;

  78: }

POJ 2299 Ultra-QuickSort(线段树入门)的更多相关文章

  1. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  2. POJ 2808 校门外的树(线段树入门)

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  3. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  4. POJ 3264 线段树入门解题报告

    题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...

  5. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  6. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  7. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  8. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  9. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  10. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. MVC应用程序实现上传文件

    学习MVC以来,一直想实现上传文件的功能,使用jQuery来实现上传,有很多插件.此篇演示中,Insus.NET是使用Uploadify.http://www.uploadify.com/,它有更多的 ...

  2. 调试报“The source file is different from when the module was built.”问题的解决

    It is related to the checksums which is used to ensure that you are stepping in matching source. You ...

  3. javaweb学习总结(三十二)——JDBC学习入门

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...

  4. .NET开发 正则表达式中的 Bug

    又发现了一个 .net 的 bug!最近在使用正则表达式的时候发现:在忽略大小写的时候,匹配值从 0xff 到 0xffff 之间的所有字符,正则表达式竟然也能匹配两个 ASCII 字符:i(code ...

  5. MySQL常用SQL语句

    一.数据库操作 1.创建数据库: Mysql> CREATE DATABASE databaseName; Mysql数据库的数据文件默认存放在/usr/local/mysql/var/目录下, ...

  6. 媒体对象 - Media Objects(摘录)

    原文链接:http://www.jianshu.com/p/6443be21efbd 一个媒体对象由以下及部分组成 父容器 .media 媒体部分 .media-left 或者 .media-righ ...

  7. html,xhtml和xml

    html,xhtml和xml的定义: 1.html即是超文本标记语言(Hyper Text Markup Language),是最早写网页的语言,但是由于时间早,规范不是很好,大小写混写且编码不规范: ...

  8. tomcat已 .war 包的形式发布项目

    一:首相将写好的工程打成.war 文件包, 借助eclipse工具完成. 右键项目名称 --> Export --> WAR file  进入如下图 二: 进入到Tomcat的 webap ...

  9. ubuntu修改源列表sourcelist的方法

    1.备份源列表 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 2.找到对应版本的源,可以在以下界面当中找到,主要版本要对.htt ...

  10. Android logcat

    logcat是Android中一个命令行工具,可以用于得到程序的log信息. 基本命令 logcat使用方法如下所示: [adb] logcat [<option>] ... [<f ...