题目

Source

http://poj.org/problem?id=2104

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 10 9 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

分析

题目是给一个序列,多次询问序列区间中第k小的元素。

经典主席树入门题。。相当于序列各个前缀建一棵权值线段树,区间上的信息就是两个前缀和的差分了。。复习复习,已经忘了。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111111 int root[MAXN],tree[MAXN*20],lch[MAXN*20],rch[MAXN*20],x,y,N;
void update(int i,int j,int a,int &b){
b=++N;
if(i==j){
tree[b]=tree[a]+1;
return;
}
lch[b]=lch[a];
rch[b]=rch[a];
int mid=i+j>>1;
if(x<=mid) update(i,mid,lch[a],lch[b]);
else update(mid+1,j,rch[a],rch[b]);
tree[b]=tree[lch[b]]+tree[rch[b]];
}
int query(int i,int j,int l,int r,int k){
if(i==j) return i;
int mid=i+j>>1;
int lcnt=tree[lch[r]]-tree[lch[l]];
if(k<=lcnt) return query(i,mid,lch[l],lch[r],k);
return query(mid+1,j,rch[l],rch[r],k-lcnt);
} int a[MAXN],b[MAXN];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; ++i){
scanf("%d",&a[i]);
} memcpy(b,a,sizeof(b));
sort(b+1,b+1+n);
int bn=unique(b+1,b+1+n)-b-1; for(int i=1; i<=n; ++i){
x=lower_bound(b+1,b+1+n,a[i])-b;
update(1,bn,root[i-1],root[i]);
} int l,r,k;
while(m--){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",b[query(1,bn,root[l-1],root[r],k)]);
}
return 0;
}

POJ2104 K-th Number(主席树)的更多相关文章

  1. 【poj2104】K-th Number 主席树

    题目描述 You are working for Macrohard company in data structures department. After failing your previou ...

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

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

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

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

  4. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  5. poj2104 K-th Number区间第k小值 主席树

    原来主席树就是可持久化线段树啊,刚知道,,, 作为一道裸题,还是必A的,然而一开始偷懒不写离散化跪了N多遍,后来在缪大的帮助下发现了这个问题,遂A之 ——又是这种破问题,实在不想说自己了 把n个数看成 ...

  6. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  7. POJ2104 K-th Number[主席树]【学习笔记】

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

  8. [poj2104] K-th Number (主席树)

    主席树 Description You are working for Macrohard company in data structures department. After failing y ...

  9. 主席树:POJ2104 K-th Number (主席树模板题)

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

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

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

随机推荐

  1. <section> 标签

    最近正在学习html5,刚接触html5,感觉有点不适应,因为有一些标签改变了,特别是div, section article这三个标签,查了一些资料,也试着用html5和css3布局网页,稍微有点头 ...

  2. 使用php+swoole对client数据实时更新(下)

    上一篇提到了swoole的基本使用,现在通过几行基本的语句来实现比较复杂的逻辑操作: 先说一下业务场景.我们目前的大多数应用都是以服务端+接口+客户端的方式去协调工作的,这样的好处在于不论是处在何种终 ...

  3. vs写python扩展资料收集

    总结: 1.创建dll工程: 2.增加包含头文件路径 :将python路径下的include加入到包含头文件路径:在工程属性页 C/C++/附加包含目新增<Python>\include目 ...

  4. windows 平台 php_Imagick 拓展遇到的那些坑!

    我的php环境是使用了phpstudy 下载地址:http://www.phpstudy.net/a.php/211.html 最终并未解决问题 持续更新~ 1.首先到官网上 http://www.i ...

  5. 关于开放式CNC系统实时软件控制系统的一些简单分析

    随笔: 黄杰U201310771 首先,我们一起了解或复习两个概念:开放式CNC和开放式CNC体系结构.按照工EEE规定:一个开放式的系统必须具备不同应用程序能很好地运行于不同供应商提供的不同平台之上 ...

  6. Activity系列讲解---三大基本状态与七大生命周期函数

    简介:四大组件之一,在应用中一个Activity可以用来表示一个界面,可以理解为用户可视化界面,一个android应用必须通过Activity来运行和启动. 1.三大基本状态与七大生命周期函数 2.代 ...

  7. 【krpano】krpano xml资源解密(破解)软件说明与下载(v1.4)

    欢迎加入qq群551278936讨论krpano技术以及获取最新软件.   该软件已经不再维护,现在已经被KRPano资源分析工具取代,详情参见 http://www.cnblogs.com/reac ...

  8. java并行计算Fork和Join的使用

    Java在JDK7之后加入了并行计算的框架Fork/Join,可以解决我们系统中大数据计算的性能问题.Fork/Join采用的是分治法,Fork是将一个大任务拆分成若干个子任务,子任务分别去计算,而J ...

  9. LruCache算法原理及实现

    LruCache算法原理及实现 LruCache算法原理 LRU为Least Recently Used的缩写,意思也就是近期最少使用算法.LruCache将LinkedHashMap的顺序设置为LR ...

  10. 终于遇到app不兼容,你遇到了么?

    题记: 如果支付宝和QQ不兼容,要二选一,你会怎么选择? 首先了解一下背景: 笔者最近发现,微众银行的app升级到1.7.4, 而患有轻度强迫症的人是迫不及待的点了升级. 第一次,居然安装包安装不成功 ...