K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 34935   Accepted: 11134
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
划分树
AC代码:
 #include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 100005
using namespace std;
class TreeNode
{
public:
int left;
int right;
int mid;
};
int ToLeft[][MAX];
int val[][MAX];
TreeNode node[*MAX];
int sorted[MAX];
void BuildTree(int k, int d, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].mid = (l + r) >> ;
int mid = (l + r) >> ;
if(l == r)
return ;
int lsame = mid - l + ;
for(int i = l;i <= r; i ++)
{
if(val[d][i] < sorted[mid])
lsame --;
}
int lpos = l;
int rpos = mid + ;
for(int i = l;i <= r;i ++)
{
if(i == l)
ToLeft[d][i] == ;
else
ToLeft[d][i] = ToLeft[d][i-];
if(val[d][i] < sorted[mid])
{
ToLeft[d][i] ++;
val[d+][lpos++] = val[d][i];
}
else if(val[d][i] > sorted[mid])
val[d+][rpos++] = val[d][i];
else
{
if(lsame)
{
ToLeft[d][i] ++;
val[d+][lpos++] = val[d][i];
lsame --;
}
else
val[d+][rpos++] = val[d][i];
}
}
BuildTree(k << , d + , l, mid);
BuildTree(k << |, d+, mid + , r);
} int Query(int l, int r, int k, int d, int idx)
{
if(l == r)
return val[d][l];
int s;
int ss;
if(node[idx].left == l)
{
s = ToLeft[d][r];
ss = ;
}
else
{
s = ToLeft[d][r] - ToLeft[d][l-];
ss = ToLeft[d][l-];
}
if(s >= k)
{
int newl = node[idx].left + ss;
int newr = node[idx].left + ss + s - ;
return Query(newl, newr, k, d + , idx << );
}
else
{
int bb = l - node[idx].left - ss;
int b = r- l - s + ;
int newl = node[idx].mid + bb + ;
int newr = node[idx].mid + bb + b;
return Query(newl, newr, k - s, d + , idx << |);
}
} int main(int argc, char const *argv[])
{
int n, m;
int l, r, k;
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
for(int i = ;i <= n;i ++)
{
scanf("%d", &val[][i]);
sorted[i] = val[][i];
}
sort(sorted+, sorted+n+);
BuildTree(, , , n);
for(int i = ;i < m;i ++)
{
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", Query(l, r, k, , ));
}
}
return ;
}
 

POJ --2104的更多相关文章

  1. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

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

  2. K-th Number POJ - 2104

    K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...

  3. POJ 2104 K-th Number【整体二分 + 树状数组】

    本来只是想学一下CDQ,还是先把整体二分搞懂一点. 这题窝几个月前分别用划分树,树套树,主席树和挑战上介绍的分桶法实现了一发(然而现在都忘得差不多了) 最快的是划分树,其次是主席树,然后是树套树,还有 ...

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

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

  5. poj 2104 K-th Number 主席树+超级详细解释

    poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...

  6. poj 2104 K-th Number(主席树,详细有用)

    poj 2104 K-th Number(主席树) 主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了. 查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区 ...

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

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

  8. 主席树 【权值线段树】 && 例题K-th Number POJ - 2104

    一.主席树与权值线段树区别 主席树是由许多权值线段树构成,单独的权值线段树只能解决寻找整个区间第k大/小值问题(什么叫整个区间,比如你对区间[1,8]建立一颗对应权值线段树,那么你不能询问区间[2,5 ...

  9. HDU 2665 && POJ 2104(主席树)

    http://poj.org/problem?id=2104 对权值进行建树(这个时候树的叶子是数组b的有序数列),然后二分查找原数列中每个数在有序数列中的位置(即第几小),对每一个前缀[1,i]建一 ...

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

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

随机推荐

  1. H TML5 之 (1) 初识HTML5

    新特性 HTML5 中的一些有趣的新特性: 用于绘画的 canvas 元素 用于媒介回放的 video 和 audio 元素 对本地离线存储的更好的支持 新的特殊内容元素,比如 article.foo ...

  2. (转)PHP获取今天、昨天、明天的日期

    <?php echo "今天:".date("Y-m-d")."<br>"; echo "昨天:".d ...

  3. Redis介绍

    Redis的介绍 Remote Dictionary Server(Redis)是一个基于 key-value 键值对的持久化数据库存储系统.支持多种数据结构,包括 string (字符串).list ...

  4. .NET设计模式(8):适配器模式(Adapter Pattern)

    ):适配器模式(Adapter Pattern)    适配器模式(Adapter Pattern) --.NET设计模式系列之八 Terrylee,2006年2月 概述 在软件系统中,由于应用环境的 ...

  5. android下面res目录

    1. 相关文件夹介绍      在Android项目文件夹里面,主要的资源文件是放在res文件夹里面的.assets文件夹是存放不进行编译加工的原生文件,即该文件夹里面的文件不会像xml,java文件 ...

  6. 案例:latch: cache buffers chains event tuning

    前两天对oracle数据库(single instance)进行了迁移升级从10.2.0.4 升到11.2.0.3,有一个项目迁完后第二天,cpu负载升到了130更高(16cpus). 向用户询问后使 ...

  7. Windows服务安装方法

    操作系统:Win8.1 安装方法:在命令行窗口中输入:InstallUtil service.exe 出错原因:需要以管理员身份启动命令行.

  8. 为什么每个浏览器都有Mozilla

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ...

  9. iOS支付总结

    内容大纲: 一.常见的支付方案简介 二.第三方支付SDK 三.苹果官方支付方案 四.Web支付方案 正文: 一.常见的支付方案简介 在微信支付中 微信支付的网址是: https://pay.weixi ...

  10. C#程序中:如何修改xml文件中的节点(数据)

    要想在web等程序中实现动态的数据内容给新(如网页中的Flash),不会更新xml文件中的节点(数据)是远远不够的,今天在这里说一个简单的xml文件的更新,方法比较基础,很适合初学者看的,保证一看就懂 ...