【POJ2104】kth num
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.
题目大意
求区间[l,r]中第k小的值
主席树裸题,我现在也只能做裸题
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int N=;
int a[N],b[N],ls[N],rs[N],sum[N],root[N];
int t,sz,n,m;
void build(int l,int r,int x,int &y,int v){
y=++sz;
ls[y]=ls[x]; rs[y]=rs[x];
sum[y]=sum[x]+;
if (l==r) return;
int mid=(l+r)>>;
if (v>mid) build(mid+,r,rs[x],rs[y],v);
else build(l,mid,ls[x],ls[y],v);
} int query(int L,int R,int w){
int l=,r=t,mid=(l+r)>>;
int x=root[L-],y=root[R];
while (l!=r){
if (sum[ls[y]]-sum[ls[x]]>=w){r=mid;x=ls[x];y=ls[y];mid=(l+r)>>;}
else {w-=sum[ls[y]];w+=sum[ls[x]];l=mid+;x=rs[x];y=rs[y];mid=(l+r)>>;}
}
return l;
} int main(){
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}
sort(b+,b+n+);
t=unique(b+,b+n+)-b-;
for (int i=;i<=n;i++){
int w=lower_bound(b+,b+t+,a[i])-b;
build(,t,root[i-],root[i],w);
}
int l,r,w;
for (int i=;i<=m;i++){
scanf("%d%d%d",&l,&r,&w);
printf("%d\n",b[query(l,r,w)]);
}
}
【POJ2104】kth num的更多相关文章
- 【poj2104】K-th Number 主席树
题目描述 You are working for Macrohard company in data structures department. After failing your previou ...
- 【POJ2104】K-th Number
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABToAAAJ2CAIAAADwi6oDAAAgAElEQVR4nOy9a5Pj1nnvi0/Q71Llj3
- 【POJ2104】K-th Number(主席树)
题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- 【题解】kth异或和/魔改版线性基
[题解]魔改版线性基 魔改版线性基解决此类问题. 联系线性空间的性质,我们直接可以构造出这样的基: \[ 100000 \\ 010000 \\ 000010 \\ 000001 \] 使得每个基的最 ...
- 【POJ2104】【整体二分+树状数组】区间第k大
Description You are working for Macrohard company in data structures department. After failing your ...
- 【HDOJ6621】K-th Closest Distance(主席树,二分)
题意:给定一个长为n的序列,有m次强制在线的询问,每次询问位置[L,R]中abs(a[i]-p)第k小的值 n,m<=1e5,a[i]<=1e6,p<=1e6,k<=169 思 ...
- 【bzoj2104】 K-th Number
http://poj.org/problem?id=2104 (题目链接) 题意 求区间第k大数. Solution1 主席树裸题. 主席树当时我学是学的要死,那个时候不晓得百度出什么bug了,搜个主 ...
- 【leetcode】Kth Largest Element in an Array (middle)☆
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- LeetCode 80
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates": What if duplicat ...
- 在CentOS6.4中安装配置LAMP环境的详细步骤
本文详细介绍了CentOS6.4系统中安装LAMP服务并对其进行配置的过程,即安装Apache+PHP+Mysql,参照了网上大神的设置,其他Linux发行系统可以参考~ 在本文中部分命令操作需要ro ...
- Linux - 硬链接与软链接
在 Linux 的文件系统中,磁盘块分成了 3 个部分.一部分称为数据区,用来存放文件内容.另一部分称为 inode 表,用来存放文件属性.第三部分称为超级块,用来存放文件系统本身的属性.文件的内容和 ...
- 在VS2010中使用附加进程的方式调试IIS中的页面
h3{background:#333333; } 准备篇-配置IIS环境 在发布网站之前,需要安装iis环境! 之后点击确定即可! 发布网站至IIS-附加到进程调试 1. 用VS2010将 ...
- Ajax-数据格式-xml,json
xml demo testDataXml <%@ page language="java" contentType="text/html; charset=UTF- ...
- IP地址理解_IP地址=网络地址+主机地址,但是具体前面多少是网络地址看题目说明
题目: 属于网络112.10.200.0/21的地址是() 112.10.206.0 112.10.217.0 112.10.224.0 112.10.198.0 分析解答: 总结: 首先,明白后 ...
- SCSS是什么
SCSS即是SASS的新语法,是Sassy CSS的简写,是CSS3语法的超集,也就是说所有有效的CSS3样式也同样适合于SASS. SASS是CSS3的一个扩展,增加了规则嵌套.变量.混合.选择器继 ...
- iOS 非ARC基本内存管理系列 3-循环retain和@class
1.@class 使用场景:对于循环依赖关系来说,比方A类引用B类,同时B类也引用A类: 可以看出Person和Card互相引用,此时如果使用#import编译报错!因此当使用@class在两个类中相 ...
- JS如何获取iframe内html的body值
default页面: <html> <head> <script type="text/javascript"> window.onload=f ...
- 布局—column(属性)
column-width:||用来定义多列中每列的宽度,用像素表示column-count:||用来定义多列中的列数,用数字来表示1-10columns: 200px 2||列宽和列数column-g ...