Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12984    Accepted Submission(s): 3962

Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=; int T[N],num[N],san[N];
int ls[N*],rs[N*],sum[N*];
int tot,n,m;
void Update(int last,int p,int l,int r,int &rt){
rt=++tot;
ls[rt]=ls[last];
rs[rt]=rs[last];
sum[rt]=sum[last]+;
if(l==r) return;
int m=(l+r)>>;
if(p<=m) Update(ls[last],p,l,m,ls[rt]);
else Update(rs[last],p,m+,r,rs[rt]);
}
int Query(int ss,int tt,int l,int r,int k){
if(l==r) return l;
int m=(l+r)>>;
int cnt=sum[ls[tt]]-sum[ls[ss]];
if(k<=cnt) return Query(ls[ss],ls[tt],l,m,k);
else return Query(rs[ss],rs[tt],m+,r,k-cnt);
}
void work(){
int l,r,k;
for(int i=;i<=n;++i) {
scanf("%d",num+i);
san[i]=num[i];
}
tot=;
sort(san+,san+n+);
int cnt=unique(san+,san+n+)-san-;for(int i=;i<=n;++i)
num[i]=lower_bound(san+,san+cnt+,num[i])-san;
for(int i=;i<=n;++i) Update(T[i-],num[i],,cnt,T[i]);
while(m--) {
scanf("%d%d%d",&l,&r,&k);
int id=Query(T[l-],T[r],,cnt,k);
printf("%d\n",san[id]);
}
}
int main(){
int T;
for(scanf("%d",&T);T--;){
scanf("%d%d",&n,&m);
work();
}
}
 

hdu2665可持久化线段树,求区间第K大的更多相关文章

  1. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  2. 主席树(可持久化线段树) 静态第k大

    可持久化数据结构介绍 可持久化数据结构是保存数据结构修改的每一个历史版本,新版本与旧版本相比,修改了某个区域,但是大多数的区域是没有改变的, 所以可以将新版本相对于旧版本未修改的区域指向旧版本的该区域 ...

  3. HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)

    题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...

  4. K-th Number 线段树的区间第K大

    http://poj.org/problem?id=2104 由于这题的时间限制不紧,所以用线段树水一水. 每个节点保存的是一个数组. 就是对应区间排好序的数组. 建树的时间复杂度需要nlogn 然后 ...

  5. [poj2104]kth-number(归并树求区间第k大)

    复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...

  6. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  7. xdoj-1324 (区间离散化-线段树求区间最值)

    思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i]  覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...

  8. hdu 1754 I Hate It (线段树求区间最值)

    HDU1754 I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  9. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

  10. 线段树维护区间前k小

    线段树维护区间前k小 $ solution: $ 觉得超级钢琴太麻烦?在这里线段树提供一条龙服务 . 咳咳,开始讲正题!这道题我们有一个和超级钢琴复杂度一样 $ ~O(~\sum x\times lo ...

随机推荐

  1. Mac文件上传下载到服务器指定命令

    下载文件夹 scp -r  远程登录服务器用户名@远程服务器ip地址:/下载文件夹的目录     『空格』    本地目录 下载文件 scp   远程登录服务器用户名@远程服务器ip地址:/下载文件的 ...

  2. 【Linux网络基础】TCP/IP协议簇的详细介绍(三次握手四次断开,11种状态)

    一.TCP/IP协议簇(DoD参考模型) 用于简化OSI层次,以及相关的标准. 传输控制协议(tcp/ip)簇是相关国防部DoD所创建的,主要用来确保数据的完整性以及在毁灭性战争中维持通信 是由一组不 ...

  3. mac OS vi/vim 使用教程

    vi/vim 的使用 基本上 vi/vim 共分为三种模式 分别是 命令模式(Command mode) 输入模式(Insert mode) 底线命令模式(Last line mode) 命令模式: ...

  4. Highcharts的自适应DOM或者DIV,JS方法实现

    那我们就按照官网的一分钟极速入门代码来说 // 图表配置 var options = { chart: { type: 'bar' //指定图表的类型,默认是折线图(line) }, title: { ...

  5. Math.Round和四舍五入

    Math.Round方法并不是像想象中的四舍五入, 可以从下面的输出结果看出来: Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); // ...

  6. RF(页面断言)

    一.RF中断言方式 title should be(断言title与预期指定的title内容相等) Open Browser https://www.baidu.com/ gc Title Shoul ...

  7. dos命令下安装pip报错 不是内部命令

    在dos命令下: pip install requests 遇到这种情况一般是Python的环境变量没有设置好 解决方案一:设置环境变量 C:\Python\scripts   如图 是否有pytho ...

  8. Python安装第三方模块出错 No module named setuptools

    在安装 zabbix-alerta 第三方模块时候报错 python setup.py install 此时需要安装 setuptools 模块, 这里用自动化脚本安装 wget https://bo ...

  9. 设计模式(Java语言)- 原型模式

    原型模式(Prototype Pattern)也有人将原型模式称为克隆模式,是属于创造型设计模式,用于创建重复的对象,提供了一种创建对象的最佳方式.原型模式需要实现Cloneable接口,来实现对象的 ...

  10. Android | 带你零代码实现安卓扫码功能

    目录 小序 背景介绍 前期准备 开始搬运 结语 小序   这是一篇纯新手教学,本人之前没有任何安卓开发经验(尴尬),本文也不涉及任何代码就可以使用一个扫码demo,华为scankit真是新手的福音-- ...