HDU2665_Kth number
给一个数组,求区间[l,r]中第k大的数。
今天被各种数据结构虐爆了,自己还是需要学习一下函数式线段树的,这个东西好像还挺常用。
函数式线段树的思想是这样的,对于每个时间状态,我们都建立一颗线段树,查询两个状态在某个区间的差的话,我们只要找到两个状态分别对应的点相减即可。
由于每次我使用线段树更新的时候,一路向下,所以我所涉及的更新的节点数量也只有log个,为了不改变原来的状态,可以选择新建这些节点。
这样所有的节点数量也不会超过n*log()个了。
对于此题,按照数组的顺序从左到右依次加入到线段树中,对于每个数组的位置都建立了一颗线段树,那么查找对于区间[l,r]的数字个数,我们只需要沿着两树的根节点一直往下面判断就可以了,每次判断两颗数的左二子数量相差是否大于K即可,也就是对于当前选择左走还是右走了,最终到达的点就是要找的那个第K大值了。
第一次使用 unique()和lower_bound(),内牛满面啊。 T_T !!!!!
召唤代码君:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define maxn 22222222
using namespace std; int L[maxn],R[maxn],sum[maxn];
int N,n,m,T;
int a[maxn],lisan[maxn],b[maxn],cnt; void build(int l,int r,int& p)
{
p=++N; sum[p]=;
if (l==r) return;
int mid=(l+r)>>;
build(l,mid,L[p]);
build(mid+,r,R[p]);
} void update(int pre,int& p,int l,int r,int x)
{
p=++N;
L[p]=L[pre],R[p]=R[pre],sum[p]=sum[pre]+;
if (l==r) return;
int mid=(l+r)>>;
if (x<=mid) update(L[pre],L[p],l,mid,x);
else update(R[pre],R[p],mid+,r,x);
} int query(int u,int v,int l,int r,int k)
{
if (l==r) return l;
int mid=(l+r)>>,num=sum[L[v]]-sum[L[u]];
if (num>=k) return query(L[u],L[v],l,mid,k);
else return query(R[u],R[v],mid+,r,k-num);
} int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
N=;
for (int i=; i<=n; i++) scanf("%d",&a[i]),lisan[i]=a[i];
sort(lisan+,lisan++n);
cnt=unique(lisan+,lisan++n)-lisan-;
build(,cnt,b[]);
for (int i=; i<=n; i++)
{
int tmp=lower_bound(lisan+,lisan++cnt,a[i])-lisan;
update(b[i-],b[i],,cnt,tmp);
}
while (m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int pos=query(b[l-],b[r],,cnt,k);
printf("%d\n",lisan[pos]);
}
}
return ;
}
HDU2665_Kth number的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- jmeter no-gui模式动态传递场景参数
jmeter进行性能压测时,有时候需要在linux上no-gui模式下运行,为了在no-gui模式下更方便的设置脚本的运行的场景, 将脚本的线程数,运行时间设置为动态参数,可以在脚本运行时动态设置“线 ...
- Python练习——矩形、直角三角形、乘法表
一.矩形 #******输入行和列,打印相应的矩形******# width = input("宽:") longth = input("长:") if (wi ...
- 初学者下载使用Python遇到的问题看它就行了
首先在python管网(www.python.org)中找到对应的版本与系统,以(window7系统64位python3.7.3为例) 打开电脑--打开浏览器--输入www.python.org--d ...
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- unity灯光烘焙设置详解
游戏场景中灯光照明的构成 现实生活中的光线是有反射.折射.衍射等特性的.对这些基本特性的模拟一直以来都是计算机图形图像学的重要研究方向. 在CG中,默认的照明方式都是不考虑这些光线特性的,因此出来的效 ...
- Unity3D — — Inspector面板编辑
转载官方文档,暂未深入研究 PropertyDrawer
- Datawhale MySQL 训练营 Task3 表操作
目录 学习内容 1.MySQL 表数据类型 2. 用SQL语句创建表 3. 用SQL语句向表中添加数据 4. 用SQL语句删除表 5. 用SQL语句修改表 作业 参考链接 学习内容 1.MySQL 表 ...
- Flink架构分析之资源分配
Task Slot Flink中每个真正执行任务的TaskManager都是一个JVM进程,其在多线程环境中执行一个或者多个子任务.为了控制一个JVM同时能运行的任务数量,flink引入了ta ...
- VMware vSphere 6.0 安装及管理手册
目录 1. VMWARE_VSPHERE安装 1.1. 底层ESXI 安装步骤 1.2. VCENTER安装步骤 1) 准备vCenter安装环境 2) vCenter安装步骤 2. VMWARE_V ...
- Spark计算模型RDD
RDD弹性分布式数据集 RDD概述 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行 ...