给一个数组,求区间[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的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. 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.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. htm5移动端开发 和 pc端开发

    htm5移动端开发: 移动端开发需要注意的一些事情:http://www.duanliang920.com/learn/web/html5/321.html HTML5移动端手机网站开发流程:http ...

  2. 图片转换成word 公式

    1 下载安装mathpix, 利用该软件将图片转换成LaTeX公式 2 参考此帖,将LaTeX公式转换成word公式 https://www.douban.com/note/648629593/ ht ...

  3. Python学习之路:一天搞定基础部分

    ~代表省略的内容,如变量名.字符串等等 1.Pyhton中比较特别的运算: **:代表指数运算,例如2**3 = 8 //:代表整除运算,这一点和Java不同 2.Python的注释: #:单行注释 ...

  4. 如何在unix系统中用别的用户运行一个程序?

    1.问题的缘由 实际开发系统的时候,经常需要用别的用户运行一个程序.比如,有些系统为保证系统安全,不允许使用root来运行.这里,我们总结了unix系统下如何解决这个问题的一些方法.同时,我们还讨论如 ...

  5. 【坚持】Selenium+Python学习之从读懂代码开始 DAY4

    2018/05/21 [生成器详解:廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d ...

  6. python编辑修改haproxy配置文件--文件基础操作

    一.需求分析 有查询,删除,添加的功能 查询功能:查询则打印查询内容,如果不存在也要打印相应的信息 删除功能:查询到要删除内容则删除,打印信息. 添加功能:同上. 二.流程图 三.代码实现 本程序主要 ...

  7. RAID卡的结构详解

    软件RAID的缺点如此之多,使人们不断地思考更多实现RAID的方法.既然软件缺点太多,那么用硬件实现如何呢? RAID卡就是一种利用独立硬件来实现RAID功能的方法.要在硬件上实现RAID功能,必须找 ...

  8. Overlay 网络

  9. pssh命令详解

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/kevingrace/p/6378719.html pssh提供OpenSSH和相关工具的并行版本.包括pssh,psc ...

  10. python的Socket网络编程 使用模板

    本文给出的是TCP协议的Socket编程. 其中用了一个dbmanager数据库操作模块,这个模块是我自己定义的,可以在我的另一个文章中找到这个模块的分享.python操作mysql数据库的精美实用模 ...