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 ...
随机推荐
- TCP/IP 协议簇 端口 三次握手 四次挥手 11种状态集
第1章 概念介绍 1.1 VLAN 1.1.1 什么是VLAN VLAN(Virtual LAN),翻译成中文是“虚拟局域网”.LAN可以是由少数几台家用计算机构成的网络,也可以是数以百计的计算机构成 ...
- Linux入门基础(四):Linux网络基本配置
网络基础 ip编址 ip编址是一个双层编址方案(网络部分和主机部分),一个ip地址标识一个主机(或一个网卡接口) 现在应用最广泛的是IPv4编址,已经开始逐渐向IPv6编址切换 IPv4地址32位长, ...
- Python 字符串 整数 浮点数
• 几个函数: str() : 将一个整数或者浮点数变成字符串 int() : 将一个浮点数或一个字符串变成整数 float : 将一个整数或者字符串变成一个浮点型数据 • 整数的运算永远是精确的,而 ...
- 扩展Unity Inspector
Unity Editor下,可以在不改变原有布局的情况下扩展Inspect的界面. 在继承了Editor的类中,有两种实现方式: using UnityEditor; [CustomEditor(ty ...
- 九九乘法表的python复习
九九开始的复习 这周复习之前的学的知识关于range函数,gormat函数,print的使用总结一下 从一个小例子开始,开始我的回顾吧, 大家都是从那个九九乘法表开始的数学之旅,从一一得一,开始了我们 ...
- Python连接MySQL数据库(pymysql的使用)
本文Python版本3.5.3,mysq版本5.7.23 基本使用 # 导入pymysql模块 import pymysql #连接数据库 conn = pymysql.connect( databa ...
- 【RL系列】马尔可夫决策过程——状态价值评价与动作价值评价
请先阅读上两篇文章: [RL系列]马尔可夫决策过程中状态价值函数的一般形式 [RL系列]马尔可夫决策过程与动态编程 状态价值函数,顾名思义,就是用于状态价值评价(SVE)的.典型的问题有“格子世界(G ...
- Webstorm使用时发生Page 'http://localhost:63340/n…tok/css/bootstrap.css.map' requested without authorization, you can copy URL and open it in browser to trust it.
在使用webstorm编辑器开发时候,点击4处发生以下错误: Page 'http://localhost:63340/n…tok/css/bootstrap.css.map' requested w ...
- 工作小应用:EXCEL查找两列重复数据
工作案例:excel存在A列.B列,需要找出B列没有A列的数据,具体做法如下(以office2007做案例): 1.点击 公式-定义名称 ,选中A列,填写名称“AAA”,选中B列,填写名称“BBB”: ...
- python基础知识-11-函数装饰器
python其他知识目录 1.装饰器学习前热身准备 1.1装饰器简介 1.2装饰器热身分析 ) def func(): pass v1 = v2 = func #将函数名赋予一个变量,就和变量赋值是同 ...