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 ...
随机推荐
- DB异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间
一,RECOVERY PENDING状态 今天修改了SQL Server的Service Account的密码,然后重启SQL Server的Service,发现有db处于Recovery Pendi ...
- Jenkins + Gitlab + Ansible--playbook 代码上线流程(文末有免费视频)
jenkins 构建是支持 git 选择分支 安装 Git Parameter 插件在系统管理中的插件管理 然后点击选插件,在右上角输入 Git Parameter,找到 Git Paramete ...
- python基础开发环境Pycharm的详细使用方法
PyCharm是由JetBrains打造的一款Python IDE(集成开发环境) 1. 创建Python文件 2. pycharm的操作界面 3. PyCharm修改字体大小的方式 4. pycha ...
- Altium中Logo的导入方法及大小调整
Altium中Logo的导入方法及大小调整 LOGO识别性是企业标志的重要功能之一,特点鲜明.容易辨认,很多客户需要在PCB设计阶段导入LOGO标示归属特性.如果LOGO是CAD图纸,可以直接按照 ...
- 用UGUI制作可根据手指位置自动定位的隐形遥杆
之前写过遥杆怎么做,这里依然用的是之前的方法,就不介绍了. 之前玩过<蜡烛人>,发现手游版的<蜡烛人>的遥杆是看不见的,手指直接在屏幕左边滑动人物就可以移动,可能是为了增强沉浸 ...
- SpringCloud 学习(二) :服务注册与发现Eureka
Spring Cloud应用中可以支持多种的服务治理框架,比如Eureka.Consul.Zookeeper等,现在我们用的是consul,本文以SpringCloud Dalston.SR5版本介绍 ...
- GitHub笔记(三)——分支管理和多人协作
三.分支管理 0 语句: 查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git ...
- IP地址相关知识
IP地址基本概念 ...
- 【Ansible】ansible 任务失败控制
任务失败控制 Ansible 通常默认会确保检测模块和命令的返回码并且会快速失败 – 专注于一个错误除非你另作打算. 有时一条命令会返回 0 但那不是报错.有时命令不会总是报告它 ‘改变’ 了远程系统 ...
- python—启动自带shell时报错(丢失api-ms-win-crt-runtime-l1-1-0.dll)已解决
备注: 有的伙伴安装完1后重启,问题可以解决,summer儿在安装完1依然未能解决,于是又进行了2的安装再次重启后问题解决!! 1,安装vc-redist.x64,微软官网搜索免费下载,安装后重启. ...