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

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment. 
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?" 
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000). 
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given. 
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion
 
原题大意:给一组数。对于每个查询区间,找出第k小的数。
解题思路:划分树或主席树的裸题,POJ还有一个类似的题目 POJ 2761.
              这两道题的代码一样都可以过。
              所谓划分树,类似于在已经知道区间中位数的情况下的快排,比中位数小的都放左边,大的分右边,相等的看看左边有没有空,有空放左边,没空放右边,相对位置不变,并记录每次的左右子树划分情况。
              如果所求第k小的树,每次我们看看有多少数在左边,就可以知道该查左区间还是右区间。对查询区间做了处理后递归下去即可得出唯一的数值。
#include<stdio.h>
#include<algorithm>
using namespace std;
int tree[20][110001],sorted[110001],left[20][110001];
void build(int dep,int l,int r)
{
int mid=(l+r)>>1,i,midnum=mid-l+1,sonl=l,sonr=mid+1;
for(i=l;i<=r;++i) if(sorted[i]<sorted[mid]) --midnum; //寻找能放入左子树中值的个数
for(i=l;i<=r;++i)
{
if(i==l) left[dep][i]=0; else left[dep][i]=left[dep][i-1]; //对不同的区间,进行左
//树个数初始化
if(tree[dep][i]==sorted[mid])
{
if(midnum)
{
--midnum;
++left[dep][i];
tree[dep+1][sonl++]=tree[dep][i]; //统计进入左子树的个数,并更新//下一层树
} else tree[dep+1][sonr++]=tree[dep][i];
} else
if(tree[dep][i]<sorted[mid]) //比中间值小,入左子树
{
++left[dep][i];
tree[dep+1][sonl++]=tree[dep][i];
} else tree[dep+1][sonr++]=tree[dep][i]; //比中间值小,入右子树
}
if(l==r) return; //如果是叶子结点,返回
build(dep+1,l,mid); //递归左右子树
build(dep+1,mid+1,r);
}
int query(int dep,int l,int r,int ql,int qr,int k)
{
int l_ql,ql_qr,mid=(l+r)>>1;
if(l==r) return tree[dep][l]; //如果找到值,返回
if(l==ql) //恰好是所求左区间为递归左区间
{ //
l_ql=0;
ql_qr=left[dep][qr];
} else
{
l_ql=left[dep][ql-1]; //l到ql-1的入左区间数
ql_qr=left[dep][qr]-l_ql; //ql到qr的左区间数
}
if(k<=ql_qr) return query(dep+1,l,mid,l+l_ql,l+l_ql+ql_qr-1,k); else //递归下一区间
return query(dep+1,mid+1,r,mid+1+ql-l_ql-l,mid+1+qr-ql_qr-l_ql-l,k-ql_qr);
//右区间有点乱,ql-l-l_ql即l到ql-1中入右区间的个数依次类推
}
int main()
{
int n,m,i,ql,qr,qk;
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)
{
scanf("%d",&tree[0][i]);
sorted[i]=tree[0][i];
}
sort(sorted+1,sorted+n+1);
build(0,1,n);
while(m--)
{
scanf("%d%d%d",&ql,&qr,&qk);
printf("%d\n",query(0,1,n,ql,qr,qk));
}
return 0;
}

  

[划分树] POJ 2104 K-th Number的更多相关文章

  1. HDU 4417 (划分树+区间小于k统计)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...

  2. 划分树 静态第k大

    划分树是保存了快速排序的过程的树,可以用来求静态第k小的数 如果,划分树可以看做是线段树,它的左孩子保存了mid-L+1 个 小于等于 a[mid] 的数字,  右孩子保存了 R-mid个大于等于a[ ...

  3. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  4. 【POJ 2104】 K-th Number 主席树模板题

    达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...

  5. POJ 2104:K-th Number(整体二分)

    http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二 ...

  6. POJ 2104:K-th Number 整体二分

    感觉整体二分是个很有趣的东西. 在别人的博客上看到一句话 对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了 树套树写了一天还是WA着,调得焦头烂额,所以决定学cd ...

  7. 主席树----POJ 2104(主席树裸题)(转)

    首先来介绍一下我们需求:给你n个数,多次问你某个区间内的第k小是哪个数 主席树: 主席树的全名应该是 函数式版本的线段树.加上附带的一堆 technology.. ..总之由于原名字太长了,而且 “主 ...

  8. POJ 2104 K-th Number(区间第k大数)(平方切割,归并树,划分树)

    题目链接: http://poj.org/problem? id=2104 解题思路: 由于查询的个数m非常大.朴素的求法无法在规定时间内求解. 因此应该选用合理的方式维护数据来做到高效地查询. 假设 ...

  9. K-th Number POJ - 2104 划分树

    K-th Number You are working for Macrohard company in data structures department. After failing your ...

随机推荐

  1. Trading

    http://v.youku.com/v_show/id_XMTA0OTcxMjgw.html?from=y1.2-1-87.3.8-1.1-1-1-7

  2. Socket之TCP连接_TcpNoDelay

    摘自: http://jerrypeng.me/2013/08/mythical-40ms-delay-and-tcp-nodelay/

  3. JavascriptExecutor

    Why we use it?To enhance the capabilities of the existing scripts by performing javascript injection ...

  4. Unity仪表盘显示UGUI制作小心得

    最近在做设备仪表参数参数显示,由于模型摆放位置经常修改,加之要求不能在模型的下面添加东西,显示界面的位置也不得不跟着修改,一来二去就烦了,想了解决办法,现在总结如下: 1.仍然在模型下面新建Panel ...

  5. 物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)

    物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)   2015年11月14日|    by: nbboy|    Category: 系统设计, 缓存设计, 高性能系统 摘要 ...

  6. 剑指offer四:链表中倒数第k个结点

    输入一个链表,输出该链表中倒数第K个结点 public class ListNode { int val; ListNode next = null; ListNode(int val) { this ...

  7. spark的standlone模式安装和application 提交

    spark的standlone模式安装 安装一个standlone模式的spark集群,这里是最基本的安装,并测试一下如何进行任务提交. require:提前安装好jdk 1.7.0_80 :scal ...

  8. 深入理解IOC模式及Unity框架

    研究了下,有几篇博客确实已经说得很清楚了 1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html  这篇博客是通过一个 ...

  9. jquery了解

    jQuery 库 - 特性 jQuery 是一个 JavaScript 库. jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操 ...

  10. 服务器上index.jsp变空

    早上,前五分钟3台分别浏览了3台服务器,都是正常的,一会后台说其中一台打开页面是空白的,发现这台服务器上的index.jsp文件变成了空白.用其他服务器上的index.jsp文件覆盖,蹦出了另一个错误 ...