Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 59058   Accepted: 20529
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

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion
主席树板子题
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = ;
int ls[N*],rs[N*],sum[N*];
int n,m,a[N],hash[N],cnt,root[N];
int x,y,k;
void lsh()
{
sort(hash+,hash+n+);
cnt=unique(hash+,hash+n+)-(hash+);
for(int i=;i<=n;i++)a[i]=lower_bound(hash+,hash+cnt+,a[i])-hash;
}
int tot=;
void build(int l,int r,int &rt,int pre,int w)
{
rt=++tot;
sum[rt]=sum[pre]+;
if(l==r)return;
int mid=(l+r)>>;
if(w<=mid)rs[rt]=rs[pre],build(l,mid,ls[rt],ls[pre],w);
else ls[rt]=ls[pre],build(mid+,r,rs[rt],rs[pre],w);
}
int query(int l,int r,int x,int y,int k)
{
if(l==r)return l;
int mid=l+r>>,tmp=sum[ls[y]]-sum[ls[x]];
if(tmp>=k)return query(l,mid,ls[x],ls[y],k);
else return query(mid+,r,rs[x],rs[y],k-tmp);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",a+i),hash[i]=a[i];
lsh();
// printf("%d\n",cnt);for(int i=1;i<=cnt;i++) printf("%d ",hash[i]);
for(int i=;i<=n;i++) build(,cnt,root[i],root[i-],a[i]);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",hash[query(,cnt,root[x-],root[y],k)]);
}
return ;
}

poj 2104 K-th Number(主席树的更多相关文章

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

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

  2. 静态区间第k大(主席树)

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

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

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

  4. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  5. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

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

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

  7. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  8. SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]

    题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...

  9. poj 2104 K-th Number(主席树 视频)

    K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...

  10. Poj 2104 K-th Number(主席树&&整体二分)

    K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...

随机推荐

  1. python列表中的深浅copy

    列表中的赋值和平常的赋值是不一样的,看下面的代码: In [1]: a = 1 In [2]: b = a In [3]: a Out[3]: 1 In [4]: b Out[4]: 1 In [5] ...

  2. (转)webView清除缓存

    NSURLCache * cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; [cache setDiskCa ...

  3. GPIO程序在PC上的模拟学习

    #include <stdio.h> #include <malloc.h> #include <memory.h> typedef struct gpio { i ...

  4. POJ:1753-Flip Game(二进制+bfs)

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Description Fl ...

  5. ReportViewer部分使用总结

    最近winform上使用ReportViewer做报表,因为之前没弄过,所以遇到了很多问题,现在总结一下. 一.运行环境 .net环境:4.0 开发工具:vs2010 二.开发步骤 第一步,在winf ...

  6. Python虚拟机中的一般表达式(三)

    其他一般表达式 在前两章:Python虚拟机中的一般表达式(一).Python虚拟机中的一般表达式(二)中,我们介绍了Python虚拟机是怎样执行创建一个整数值对象.字符串对象.字典对象和列表对象.现 ...

  7. luogu2865 [USACO06NOV]路障Roadblocks 次短路

    注意:如果是这么个写法,堆数组要开成n+m的. 为什么呢?设想一下从1到2有m条长度递减的路,这岂不是要入队m次-- #include <algorithm> #include <i ...

  8. 正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不

    //默认为: Database.SetInitializer<testContext>(null);//这里报错, 检查原因:catch(Exception ex) 错误提示: 基础连接未 ...

  9. J2ee项目 编译依赖顺序

    这儿有个帖子, 最后一个回复是:  “我把我项目的libraries的"Order and Export"中的JRE与J2EE顺序换了一个问题解决”. 帖子地址: http://b ...

  10. NOJ——1624死胡同(胡搞模拟)

    [1624] 死胡同 时间限制: 1000 ms 内存限制: 65535 K 问题描述 一个死胡同由排成一列的 n 个格子组成,编号从 1 到 n .实验室的“猪猪”一开始在1号格子,开始向前走,每步 ...