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. .Net程序员学习Linux(一)

    本次知识点:Linux系统的多终端切换,linux下的用户,linux远程访问工具使用,linux下重要的目录,命令的组成,通配符,linux的路径问题,文件操作的综合运用 为什么学习linux? 1 ...

  2. [Mime] 在c#程序中放音乐的帮助类 (转载)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...

  3. mvc在页面上显示PDF

    今天看到需求要在页面上显示pdf,自己整了半天,啥效果都没有,偶尔有效果还各种不兼容,很无语的说.捣鼓了半天,没办法了,去谷歌了下,介绍了各种插件,各种方法,但是都挺繁琐的,本人不是一个很喜欢使用插件 ...

  4. IOS-UIScrollView实现图片分页

    1.设置可以分页 _scrollView.pagingEnabled = YES; 2.添加PageControl UIPageControl *pageControl = [[UIPageContr ...

  5. TCP/IP状态转换图

  6. async: false的应用.

    目的: 手机webview中, js ajax请求, success后, 进行window.open 操作 问题: 在Android, IOS均不能执行window.open 解决办法: 设置ajax ...

  7. JS中关于clientWidth offsetWidth srollWidth等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  8. Jquery 和 Js

     jQuery  [一]  jQuery     1: javaScript和 javaScript库:        (1) javaScript自身存在的3个弊端:复杂的文档对象模型(DOM),不 ...

  9. Ubuntu14.04不支持U盘exfat格式该如何解决

    转: http://www.jb51.net/os/Ubuntu/275158.html exfat是U盘的文件系统,很多系统都支持exfat格式的使用,但Ubuntu系统并不支持exfat格式,要如 ...

  10. Python3.4使用MySql

    如何在Django1.6结合Python3.4版本中使用MySql django默认的mysql连接是Mysqldb,悲催的是此版本只支持到python2.7,oracle官方的mysql-conne ...