Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4585    Accepted Submission(s): 1461

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
 
Source
 
Recommend
zty   |   We have carefully selected several similar problems for you:  

pid=2660" target="_blank">2660 2662 2667 2663 2661




一開始用归并树 超时了, 仅仅好去学划分树= = 妈蛋,看了好久才理解。



划分树建树的原理和归并树有点类似。仅仅只是划分树採用的是高速排序的方式,先把原数列排序,建树的时候以中间大的数为基数。将小的扔左边,大的扔右边,有一些小细节,如有多个数与基数同样的时候,须要一个暂时变量记录一下,用seg[dep][i] 记录i到左边区间有 多少个数是小于基数的,查询的时候,推断区间 还有就

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10; struct node{
int lson,rson;
int mid(){
return (lson+rson)>>1;
}
}tree[maxn*4]; int seg[25][maxn];
int lftnum[25][maxn];
int num[maxn];
int n,m,sta,ed; void build(int L,int R,int rt,int dep){
tree[rt].lson = L;
tree[rt].rson = R;
if(L==R) return;
int mid = tree[rt].mid(),key = num[mid],scnt = mid-L+1;//左边中间值的个数
for(int i = L; i <= R ; i++){
if(seg[dep][i] < num[mid]){
scnt--;
}
}
int lp = L,rp = mid+1;
for(int i = L; i <= R; i++){
if(i==L){
lftnum[dep][i] = 0;
}else{
lftnum[dep][i] = lftnum[dep][i-1];
}
if(seg[dep][i] < key){
lftnum[dep][i]++;
seg[dep+1][lp++] = seg[dep][i];
}
else if(seg[dep][i] > key){
seg[dep+1][rp++] = seg[dep][i];
}
else{
if(scnt>0){
scnt--;
lftnum[dep][i]++;
seg[dep+1][lp++] = seg[dep][i];
}else{
seg[dep+1][rp++] = seg[dep][i];
}
}
}
build(L,mid,rt<<1,dep+1);
build(mid+1,R,rt<<1|1,dep+1);
}
int query(int L,int R,int rt,int dep,int k){
if(tree[rt].lson==tree[rt].rson){
return seg[dep][L];
}
int cnt,act;
if(L==tree[rt].lson){
cnt = lftnum[dep][R];
act = 0;
}else{
cnt = lftnum[dep][R] - lftnum[dep][L-1];
act = lftnum[dep][L-1];
}
int mid = tree[rt].mid();
if(cnt >= k){
L = tree[rt].lson + act;
R = tree[rt].lson + act + cnt-1;
return query(L,R,rt<<1,dep+1,k);
}else{
int a = L-tree[rt].lson-act;
int b = R-L-cnt+1;
L = mid + a + 1;
R = mid + a + b;
return query(L,R,rt<<1|1,dep+1,k-cnt);
} }
int main(){
int ncase;
cin >> ncase;
while(ncase--){
cin >> n >> m;
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
seg[0][i] = num[i];
}
sort(num+1,num+n+1);
build(1,n,1,0);
while(m--){
int k;
scanf("%d%d%d",&sta,&ed,&k);
printf("%d\n",query(sta,ed,1,0,k));
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

hdu2665-Kth number的更多相关文章

  1. HDU2665 Kth number 【合并树】

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

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

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

  3. HDU2665 kth number 线段树做法

    题意:求区间第k小 思路: 线段树 每个节点上保存 当前区间已经排序好的序列 (归并一下就好了嘛 复杂度 O(l)的) 这样建树的时空复杂度都是 O(nlogn)的 对于 每次询问 二分一个答案 在树 ...

  4. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  5. POJ2104 K-th Number(主席树)

    题目 Source http://poj.org/problem?id=2104 Description You are working for Macrohard company in data s ...

  6. POJ2104 K-th Number[主席树]【学习笔记】

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51440   Accepted: 17594 Ca ...

  7. poj[2104]K-th Number

    Description You are working for Macrohard company in data structures department. After failing your ...

  8. [划分树] POJ 2104 K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51732   Accepted: 17722 Ca ...

  9. poj 2104 K-th Number(可持久线段树)

    K-th Number 持久化:http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 结构:http://www.docin.c ...

  10. [POJ2104]K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 34048   Accepted: 10810 Ca ...

随机推荐

  1. 在VS2012中使用GitHub

    注册GitHub账号(DeanZhouLin) https://github.com/ 向GitHub中添加一个仓库(Test) *创建完成后,记录该仓库的地址:https://github.com/ ...

  2. U6Linux的文件权限与目录配置

    1.ll查看文件信息:[权限][连接][所有者][用户组][文件容量][修改日期][文件名] 2.第一个字符代表文件的属性:若为[d]则是目录.若为[-]则是文件.若为[l]则为连接. 3.chgrp ...

  3. Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的

    你是否发现用Texturepacker在打包压缩资源文件之后. 把原稿文件弄丢了,可是又要添加新的小png的时候,却无从下手了,本文就是博主在遇到这个问题后百度了非常多方法,可惜仅仅有plist格式的 ...

  4. 【JS】依据表格ID进行排序(附凝视)

    分享一个前端做的依据表格ID进行排序的方法哈,贴码例如以下: HTML: <input type="button" id="btn1" value=&qu ...

  5. 用bytecode来看try-catch-finally和return

    之前看过一篇关于return和finally运行顺序的文章.仅在Java的语言层面做了分析.事实上我倒认为直接看bytecode可能来的更清晰一点. 近期一直在看Java虚拟机规范.发现直接分析byt ...

  6. SO_REUSEADDR 套接字选项应用

    在网络上的SO_REUSEADDR套接字选项是用来解决地址问题重用了大量的信息.但仅仅停留在文字的表达.并没有实例,非常easy误导谁刚开始学习,和不解,此处不再赘述. 的使用该选项,以及须要注意的问 ...

  7. crashRpt用法

    从官网上下载crashRpt的源代码,按说明编译出对应的lib和dll 1 在编译crashRpt的时候,在环境变量中设置  crashrptDir=D:\work\AIW\WebMicaps\Web ...

  8. python学习笔记之十:文件和素材

    这里将介绍函数和对象--文件和流,让你在程序调用期间存储数据,并且可以处理来自其他程序的数据. 一. 打开文件 1.1 open函数 open函数用来打开文件,语法如下:open(name,[.mod ...

  9. Android正在使用Handler实现信息发布机制(一)

    上一篇文章,我们谈到了电话Handler的sendMessage方法,最后,我们将进入一个电话 sendMessageAtTime方法,例如下列: public boolean sendMessage ...

  10. Spring 事情具体详尽的解释

    一.Spring事务管理  1. Spring事务管理机制 三个核心部分  1) PlatformTransactionManager  平台的事务管理器  commit 提交事务.rollback ...