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.

题目意思:给一个数组。问一个区间内第K大的数。

解题思路:划分树。

划分树是基于高速排序的,首先将原始数组a[]进行排序sorted[],然后取中位数m,将未排序数组中小于m放在m左边,大于m的放在m右边,并记下原始数列中每一个数左边有多少数小于m,用数组to_left[depth][]表示,这就是建树过程。

重点在于查询过程。设[L,R]为要查询的区间,[l,r]为当前区间,s 表示[L,R]有多少数放到左子树,ss表示[l,L-1]有多少数被放倒左子树。假设s大于等于K,也就是说第K大的数肯定在左子树里。下一步就查询左子树,但这之前先要更新L,R,新的newl=l+ss,
newr=newl+s-1。假设s小于k,也就是说第k大的数在右子树里,下一步查询右子树,也要先更新L,R,dd表示[l,L-1]中有多少数被放到右子树,d表示[L,R]有多少数被放到右子树,那么newl = m+1+dd,newr=m+d+dd, 这样查询逐渐缩小查询区间,直到最后L==R 返回最后结果即可。

给出一个大牛的图片样例。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 100000+100 int val[30][maxn];
int to_left[30][maxn];
int sorted[maxn];
int n; void build(int l,int r,int d,int rt){
if(l==r) return;
int m = (l+r)>>1;
int lsame = m-l+1;
for(int i=l;i<=r;i++){
if(val[d][i]<sorted[m]) lsame--;
}
int lpos=l,rpos=m+1,same=0;
for(int i=l;i<=r;i++){
if(i==l) to_left[d][i]=0;
else to_left[d][i] = to_left[d][i-1];
if(val[d][i]<sorted[m]){
to_left[d][i]++;
val[d+1][lpos++] = val[d][i];
}else if(val[d][i]>sorted[m]){
val[d+1][rpos++] = val[d][i];
}else{
if(same<lsame){
same++;
to_left[d][i]++;
val[d+1][lpos++] = val[d][i];
}else{
val[d+1][rpos++] = val[d][i];
}
}
}
build(l,m,d+1,rt<<1);
build(m+1,r,d+1,rt<<1|1);
} void print(){
printf("###\n");
for(int i=0;i<10;i++){
for(int j=1;j<=n;j++){
cout << val[i][j]<<" ";
}
cout << endl;
}
printf("****\n");
for(int i=0;i<10;i++){
for(int j=1;j<=n;j++){
cout << to_left[i][j]<<" ";
}
cout << endl;
}
} int query(int L,int R,int k,int l,int r,int d,int rt){
if(L==R) return val[d][L];
int s,ss;
if(L==l){
s = to_left[d][R];
ss = 0;
}else{
s = to_left[d][R]-to_left[d][L-1];
ss = to_left[d][L-1];
}
int m = (l+r)>>1;
if(s>=k){
int newl = l+ss;
int newr = newl + s-1;
return query(newl,newr,k,l,m,d+1,rt<<1);
}else{
int bb = (L-1)-l+1-ss;
int b = R-L+1 -s;
int newl = m+1+bb;
int newr = m+bb+b; // 4 5 1 3 2 (2,5,4)
return query(newl,newr,k-s,m+1,r,d+1,rt<<1|1);
}
} int main(){
int m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&val[0][i]);
sorted[i]=val[0][i];
}
sort(sorted+1,sorted+n+1);
build(1,n,0,1);
// print();
while(m--){
int i,j,k;
scanf("%d %d %d",&i,&j,&k);
printf("%d\n",query(i,j,k,1,n,0,1));
}
return 0;
}

poj 2104 K-th Number - 经典划分树的更多相关文章

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

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

  2. poj 2104:K-th Number(划分树,经典题)

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

  3. POJ 2104 K-th Number(划分树)

    Description You are working for Macrohard company in data structures department. After failing your ...

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

                                                                K-th Number Time Limit: 20000MS   Memory ...

  5. hdu 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. poj2104 K-th Number(划分树)

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

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

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

  8. K-th Number 【POJ - 2104】【可持久化线段树】

    题目链接 因为这道题没有删除修改之类的,所以很多人会用离散化之后的线段树来做,但是实际上(可能是我懒得去做离散化这个操作了),然后就是直接写可持久化线段树,区间的长度就是int的从最小到最大的长度,然 ...

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

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

随机推荐

  1. 小学生之Map集合框架的使用

    Map用于保存具有映射关系的数据(key-vlaue).Map的key不允许重复,即同一个Map对象的任何两个key通过equals方法比较总是返回false Map中包含了一个keySet()方法, ...

  2. 30款jQuery常用网页焦点图banner图片切换 下载 (转)

    1.jquery 图片滚动特效制作 slide 图片类似窗帘式图片滚动 查看演示 2.jquery幻灯片插件带滚动条的圆形立体图片旋转滚动 查看演示 3.jQuery图片层叠旋转类似洗牌翻转图片幻灯片 ...

  3. 高放的c++学习笔记之模板与泛型编程

    函数模板 作用 有很多时候参数的类型以及返回值的类型是可变的,我们通过定义模板来让函数能更灵活的运用. 我们设计一个比较函数,如果能比较的两个参数是int型的,两个参数也可能都是string型的,单独 ...

  4. 你好,C++(24)好大一个箱子!5.1.1 函数的声明和定义

    第5章 用函数封装程序功能 在完成功能强大的工资程序V1.0之后,我们信心倍增,开始向C++世界的更深远处探索. 现在,我们可以用各种数据类型定义变量来表达问题中所涉及的各种数据:用操作符连接这些变量 ...

  5. Python3学习之一环境搭建

    Windows 7 Python343下载 PTVS下载 Linux CentOS7 wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3 ...

  6. 《python基础教程》笔记之 异常

    按自己的方式出错 使用raise语句引发一个异常,可以使用一个类(应该是Exception的子类)或者实例参数来作为raise的引发对象.使用类时,程序会自动创建实例,如 >>> r ...

  7. 利用libpcap抓取QQ号码信息

    最近想在QQ登录时把QQ号码信息记录下来,百度了很多都没有找到具体方式,最近用Wireshark分析报文+libpcap库嗅探实现了这个小功能. 通讯背景: QQ客户端在通讯时使用UDP协议,其中数据 ...

  8. 手机页面关于头部固定定位与input出现的问题

    前些天写了一个页面,要求头部导航进行固定定位position:fixed.内容区域有输入框input.在大多数手机上都是显示正常的,可偏在一些低版本的手机却出现问题了. 把我给苦恼的不行. 问题:头部 ...

  9. Android 每隔3s更新一次title

    MainActivity.java public class MainActivity extends Activity { private static int i=0; @Override pro ...

  10. Android 读取和保存文件(手机内置存储器)

    1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...