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. asp.net mvc中包含webapi时,token失效产生302的解决方案

    public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOpt ...

  2. 通过UIView获取UIViewController

    需求很简单,通过UIViewController.view内的任意控件获取当前UIViewController: 立马开写: /** * @brief 通过viewController内的view,获 ...

  3. C#函数、参数数组(例子)★

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 【jQuery基础学习】12 jQuery学习感想

    学习完<锋利的jQuery>,用时13天. 这期间,私底下又用了一点时间去W3C上把HTML和CSS重新过了一遍. 总的来说,收获还是蛮多的. 其实在本书里面真正重要的也就前几章,后面的都 ...

  5. MySQL Query Profile

    MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用量, 执行过程 System lock, Table lock 花多少时间等等.从 ...

  6. poi 导出 excel

    private void exportAssetExcel(HttpServletRequest request, HttpServletResponse response) throws IOExc ...

  7. ButterKnife

    1.简介 ButterKnife是注解中相对简单易懂的很不错的开源框架 1.强大的View绑定和Click事件处理功能,简化代码,提升开发效率 2.方便的处理Adapter里的ViewHolder绑定 ...

  8. docker入门指南(转载)

    原文: http://bg.biedalian.com/2014/11/20/docker-start.html 关于 docker 今天云平台的同事提到, 现在的运维就是恶性循环, 因为大家都在申请 ...

  9. Vue基础理论

    一 vue的定位 (1)Vue.js是一个构建数据驱动的 web 界面的库. (2)Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. (3)Vue.js 自身不是一 ...

  10. Jquery easyui Tree的简单使用

    Jquery easyui Tree的简单使用 Jquery easyui 是jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻 ...