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 ...
随机推荐
- 浅谈MVC Razor基本语法
首先说下MVC,mvc已经不用ViewState来保留网页的状态,所以大部分依赖ViewState的功能都将无法使用, 比如gridview的分页与排序,page trace等利用viewstate记 ...
- 接口测试中抓包工具Charles的使用
在被测接口并没有明确的接口文档给出时,我们需要借助抓包工具来帮助测试,利用抓包工具我们几乎可以获得接口文档中能给你的一切.常见的抓包工具有Charles和Fiddler, Fiddler只能用在Win ...
- 利用shell连接服务器
#应用 连接timesten 数据库 host = Linux(ip, 'user', 'pwd') # 传入Ip,用户名,密码host.connect() #主机开启cdsql = host.sen ...
- WPF中的颜色转换
HEX16色转Bursh: Brush brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFF ...
- git pull fatal: refusing to merge unrelated histories
1.首先我github有个远程仓库,然后我本地有个仓库 本地仓库我新添加了一个文件,然后我去关联(git remote add origin git@github.com:qshilary/gitte ...
- Unity扩展编辑器一
将Test1脚本挂在摄像机上 如图展示 下面我们需要在代码中动态的编辑它,请在你的Project视图中创建一个Editor文件夹,把MyEditor放进Editor文件夹中 在OnInsp ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- c++三大概念要分清--重载,隐藏(重定义),覆盖(重写)
重载,隐藏(重定义),覆盖(重写)—这几个名词看着好像很像,不过其实一样都不一样!! 综述: 说明:覆盖中的访问修饰符可以不同是指可以不用显示地用virtual:当访问修饰符改为const或者stat ...
- vue 子组件传值给父组件
子组件通过this.$emit("event",[args,....]),传值给父组件 HTML部分: <div id="app"> <tmp ...
- python打印对象的所有可操作内容
print('\n'.join(['%s:%s' % item for item in 对象.__dict__.items()]))