POJ 2299 Ultra-QuickSort(线段树入门)
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(线段树入门)的更多相关文章
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- POJ 2808 校门外的树(线段树入门)
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- POJ 3264 线段树入门解题报告
题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- jQuery实现隐藏标签
要求:用户进入该页面时,品牌列表默认是精简显示,用户可以单击商品列表下方的“显示全部品牌”按钮来显示全部的品牌. <%@ Page Language="C#" Inherit ...
- 数据库一次性插入10w条数据,怎么插入效率快
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题 下面介绍SQL Server支持的两种批量 ...
- 使用win2d实现萤火虫粒子效果
这几天我在做游戏的标题画面,需要实现随机飞舞的萤火虫.萤火虫会闪烁黄绿色的光,远近不同,并且飞出屏幕边界不久会自动飞回来. 我前一阵子用win2d写了个简单的游戏引擎(现在还是alpha阶段),用它可 ...
- 老外还是喜欢Ubuntu的
有图为证? 开效果应该是Ubuntu的界面了,当然,不知是不是backtrack. 这里面的Hacker用的电脑都不是水果.是没有给钱么.
- ahjesus sql2005+游标示例
DECLARE @TypeId INT, @Price1 FLOAT, @Original FLOAT DECLARE my_cursor CURSOR SCROLL FOR SELECT TypeI ...
- 安装性能测试工具:sysbench和使用apache的ab
一.软件的用途,它主要包括以下几种方式的测试:1.cpu性能2.磁盘io性能3.调度程序性能4.内存分配及传输速度5.POSIX线程性能6.数据库性能(OLTP基准测试) 这个软件为什么找不到官网呢? ...
- MySQL 数据库 InnoDB 和 MyISAM 数据引擎的差别
InnoDB和MyISAM是在使用MySQL最常用的两个表类型,各有优缺点,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyISAM类型的表强调的是 ...
- [zz] JIT&HotSpot
zz from 百度百科 最早的Java建置方案是由一套转译程式(interpreter),将每个Java指令都转译成对等的微处理器指令,并根据转译后的指令先后次序依序执行,由于一个Java指令可能被 ...
- springmvc+mybatis+spring 整合
获取[下载地址] [免费支持更新]三大数据库 mysql oracle sqlsever 更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] ...
- CSS选择器性能分析
写了几篇关于js的博客,也是关于性能的,现在,我觉得有必要那css来认真分析一下了.之前只是看别人这么写就跟着写,但是没有去研究这样写或者是不是正确的写法,性价比怎么样,渲染的效率好么!这些都没有考虑 ...