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. c# 获取MAC IP TCP列表

    转载自baidu:http://hi.baidu.com/jackeyrain/item/ff94efcfd5cf3a3099b498e9 namespace Public { public clas ...

  2. 【STL】string 常用函数

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  3. js获取UserControl (<uc1>)控件ID

    ASPX: <table> <tr> <td> <uc1:uccalendar id="ucXudaxia" runat="se ...

  4. Java file文件的写入和读取及下载

    File文件的写入 一.FileWriter 和BufferedWriter 结合写入文件 FileWriter是字符流写入字符到文件.默认情况下,它会使用新的内容代替文件原有的所有内容,但是,当指定 ...

  5. js 关于字母和数字之间的转换

    方法:字符转ascii码:用charCodeAt();        ascii码转字符:用fromCharCode(); 例子: 字母转成数字: var str='A'; str.charCodeA ...

  6. 月薪10K必备--C#下拉框联动

                   下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗 ...

  7. 生产者与消费者(三)---BlockingQueue

    前面阐述了实现生产者与消费者问题的两种方式:wait() / notify()方法 和 await() / signal()方法,本文继续阐述多线程的经典问题---生产者与消费者的第三种方式:Bloc ...

  8. Java程序实现导出Excel,支持IE低版本

    来博客园两年多了,最近才开通了微博,因为懒所以也一直没有写东西,今天想整理一下自己前段时间遇到的一个导出的问题. 因为项目的需求,要做一部分导出功能.开始的时候用的公司的导出,但是很奇怪有部分模块导出 ...

  9. ACM YTU 1012 u Calculate e

    u Calculate e Problem Description A simple mathematical formula for e is where n is allowed to go to ...

  10. jQuery仿苏宁易购导航

    最近看了些网上的各类导航网站源码,自己学习制作了一个仿苏宁易购的导航栏 jQuery部分代码 $(function(){ $(".CategoryTree>ul>li" ...