【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 ...
随机推荐
- Codevs 3729==洛谷P1941 飞扬的小鸟
P1941 飞扬的小鸟 456通过 2.4K提交 题目提供者该用户不存在 标签动态规划2014NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录 题目描述 Flappy Bird 是一 ...
- (转)MSMQ续
原文作者:虔诚者 点此传送至原文 在上一篇我简单介绍了MSMQ的相关概念,本篇将以代码说明 Message Message是MSMQ的数据存储单元,我们的用户数据一般也被填充在Message的b ...
- 小技巧之a标签自动解析URL
我们可能都知道javascript中的window.location对象用来获取当前页面的地址URL,并把浏览器重定向到新的页面.它有protocol.hostname.host.port.searc ...
- Linux 命令 - free: 显示系统的内存信息
命令格式 free [-b | -k | -m] [-o] [-s delay ] [-t] [-l] [-V] 命令参数 -b 显示内存的单位为 Byte. -k 显示内存的单位为 KB. -m 显 ...
- PHP学习笔记--入门篇
PHP学习笔记--入门篇 一.Echo语句 1.格式 echo是PHP中的输出语句,可以把字符串输出(字符串用双引号括起来) 如下代码 <?php echo "Hello world! ...
- Xcode-项目模板修改
项目模板就是创建工程的时候选择的某一个条目, Xcode会根据选择的条目生成固定格式的项目 例如想创建一个命令行项目就选择Command Line Tool 如何修改项目模板 1.应用程序中,找到Xc ...
- 使用python发送Email
import smtplib from email.mime.text import MIMEText def SendEmail(): email = "" #设置收件地址 ma ...
- KindEditor配置步骤
KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome.Safari.Opera等主流浏览器. KindEditor ...
- Java WebService简单实例
一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 1.创建[Web Service Project],命名为[TheService ...
- [GeekBand] C++11~14
一.关键字decltype 由对象得到对象的数据类型,例如 Complex a(1, 2); decltype(a) b(3, 4); declare type是让编译器去找到 ...