K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 45653   Accepted: 15177
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
转自:http://blog.csdn.net/famousdt/article/details/7064866 
建树的过程比较简单,对于区间[l,r],首先通过对原数组的排序找到这个区间的中位数a[mid],小于a[mid]的数划入它的左子树[l,mid-1],大于它的划入右子树[mid,r]。
同时,对于第i个数a[i],记录在[l,i]区间内有多少数被划入左子树。最后,对它的左子树区间[l,mid-1]和右子树区间[mid,r]递归的继续建树就可以了。
建树的时候要注意,对于被分到同一子树的元素,元素间的相对位置不能改变。

查找的过程中主要问题就是确定将要查找的区间。
  查找深度为dep,在大区间[L ,R]中找小区间[l ,r]中的第k元素。
 
我们的想法是,先判断[l ,r]中第k元素在[L ,R]的哪个子树中,然后找出对应的小区间和k,递归的进行查找,直到小区间的l==r为止。
 
通过之前的记录可以知道,在区间[L,l-1]中有(toleft[dep][l-1]-toleft[dep][L-1])进入左子树,
 
记它为x。


同理区间[L,r]中有(toleft[dep][r]-toleft[dep][L-1])个数进去左子树,记它为y。
 
所以,我们知道区间小区间[l,r]中有(y-x)个数进入左子树。那么如果(y-x)>=k,那么就在左子树中继续查找,否则就在右子树中继续查找。


接着,解决查找的小区间的问题。
  如果接下来要查找的是左子树,那么小区间应该是[L+([L,l-1]区间进入左子树的个数),L+([L,r]区间内进入左子树的个数)-1]。即区间[L+x,L+y-1]。(这里的-1应该是防止边界问题)
    显然,这里k不用变。
  如果接下来要查找的是右子树,那么小区间应该是[mid+([L,l-1]区间中进入右子树的个数),mid+([L,r]区间进入右子树的个数)-1]。
    即区间[mid+(l-L-x),mid+(r-L-y)]。(这里要记得+1)
    显然,这里k要减去区间里已经进入左子树的个数,即k变为k-(y-x)。

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXSIZE = ;
int tree[][MAXSIZE]; ///树最多也就30层
int _sort[MAXSIZE];
int toleft[][MAXSIZE]; void build(int l,int r,int deep)
{
if(l==r) return;
int mid =(l+r)>>;
int same = mid-l+; ///设这个变量的主要原因是判断tree[deep][i]==_sort[mid]时tree[deep][i]往哪边走
for(int i=l; i<=r; i++)///计算放于左子树中与中位数相等的数字个数,
{
// printf("%d ",tree[deep][i]);
if(tree[deep][i]<_sort[mid]) same--;
}
//printf("\n");
int left = l;
int right = mid+;
for(int i=l; i<=r; i++)
{
int flag = ;
if((tree[deep][i]<_sort[mid])||(tree[deep][i]==_sort[mid]&&same>))
{
flag = ;
tree[deep+][left++] = tree[deep][i];
if(tree[deep][i]==_sort[mid]) same--;
}
else
{
tree[deep+][right++] = tree[deep][i];
}
toleft[deep][i] = toleft[deep][i-]+flag;
}
build(l,mid,deep+);
build(mid+,r,deep+);
} int query(int L,int R,int l,int r,int k,int deep)
{
if(l==r) return tree[deep][l];
int x = toleft[deep][l-]-toleft[deep][L-]; ///x代表[L,l-1]区间内进入左子树的数字的量
int y = toleft[deep][r]- toleft[deep][L-]; ///y代表[L,r]区间内进入左子树的数字的量
int cnt = y-x; ///cnt代表[l,r]区间内进入左子树的数字的量
int mid = (L+R)>>;
int rx = l - L - x;///l左边放在右子树中的数字个数
int ry = r - L - y;///到r右边为止位于右子树的数字个数
if(cnt>=k)
{
query(L,mid,L+x,L+y-,k,deep+);
}else{
query(mid+,R,mid+rx+,mid+ry+,k-cnt,deep+);
} }
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{ memset(toleft,,sizeof(toleft));
for(int i=; i<=n; i++)
{
scanf("%d",&_sort[i]);
tree[][i] = _sort[i];
} sort(_sort+,_sort+n+);
build(,n,);
while(m--)
{
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
int t = query(,n,a,b,k,);
printf("%d\n",t);
}
}
return ;
}
 

poj 2104(线段树)的更多相关文章

  1. K-th Number Poj - 2104 主席树

    K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...

  2. poj 2886 线段树+反素数

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Acc ...

  3. [poj 2104]主席树+静态区间第k大

    题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...

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

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

  5. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

  6. POJ——3264线段树

    题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...

  7. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

  8. poj 2828 线段树

    http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 而且后来的也能够确定----假设从前往后 ...

  9. POJ - 1177 线段树

    POJ - 1177 扫描线 这道题也算是一道扫描线的经典题目了. 只不过这道题是算周长,非常有意思的一道题.我们已经知道了,一般求面积并,是如何求的,现在我们要把扫描线进行改造一下,使得能算周长. ...

  10. poj 2886 (线段树+反素数打表) Who Gets the Most Candies?

    http://poj.org/problem?id=2886 一群孩子从编号1到n按顺时针的方向围成一个圆,每个孩子手中卡片上有一个数字,首先是编号为k的孩子出去,如果他手上的数字m是正数,那么从他左 ...

随机推荐

  1. Object.defineProperty基本用法

    1. 基本形式 Object.defineProperty(obj,prop,descriptor) 参数说明: obj: 必需,目标对象prop: 必需,需定义或修改属性的名字descriptor: ...

  2. 51nod1199:Money out of Thin Air(线段树)

    按dfs序一个一个加入线段树,可以让任何一颗子树的节点在线段树中连续,于是就可以用线段树维护整棵树了 和树剖的思想是一样的,大概一眼就看出来了,但是写了两个半天(躺 总结:记住以后写完数据结构或者数字 ...

  3. WizTools.org RESTClient 启动方法

    关于 WizTools.org RESTClient的使用 今天分享一个很好用的测试service的工具,很好用 提供两种方法使用这个东东. 第一种方法 通过cmd命令窗口. (1)cd C:\Use ...

  4. 洛谷:P2922 [USACO08DEC]秘密消息(Trie树)

    P2922 [USACO08DEC]秘密消息Secret Message 题目链接:https://www.luogu.org/problemnew/show/P2922 题目描述 贝茜正在领导奶牛们 ...

  5. 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)

    P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...

  6. 阐述ArrayList、Vector、LinkedList的存储性能和特性?

    ArrayList 和Vector他们底层的实现都是一样的,都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内 ...

  7. 【题解】Casting Spells LA 4975 UVa 1470 双倍回文 SDOI 2011 BZOJ 2342 Manacher

    首先要吐槽LRJ,书上给的算法标签是“有难度,需要结合其他数据结构”,学完Manacher才发现几乎一裸题 题目的意思是问原串中有多少个wwRwwR这样的子串,其中wR表示w的反串 比较容易看出来,w ...

  8. POJ 3111 二分

    K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 10507   Accepted: 2709 Case Time ...

  9. Robotframework Web自动化实战课程

    想学习的小伙伴,现在可以报名了!!!7月1日正式开课本期课程主要是web自动化为主,根据平时工作经验整理的一套流程以及使用过程中常见的问题总结.学完后能很快上手,即学即用,课后遇到问题在线解答,远程协 ...

  10. 读取txt内容放入set中

    package pingbi; /** * 将txt文本读入导入到set中 * 问题: * 第一个地方有会多一个 ?--解决问题很简单,但不知道问题的原因 */ import java.io.Buff ...