【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 ...
随机推荐
- [改善Java代码]使用构造函数协助描述枚举项
一.分析 一般来说,我们经常使用的枚举项只有一个属性,即排序号,其默认值是从0.1.2... ....但是除了排序号外,枚举还有一个(或多个)属性:枚举描述,它的含义是通过枚举的构造函数,声明每个枚举 ...
- 关于Eclipse中校验输入文件名的源代码
Eclipse中测试文件名的方法. 也没有单独的分操作系统.在Talend时解决一个在文本框中输入名字有Bug的一个问题,这个是Eclipse中解决输入名字,对名字校验的部分源码. public IS ...
- jQuery点击图片弹出大图遮罩层
使用jQuery插件HoverTreeShow弹出遮罩层显示大图 效果体验:http://hovertree.com/texiao/hovertreeshow/ 在开发HoverTreeTop项目的产 ...
- js验证 button 提交
<form class="form-horizontal" role="form" action="member_add" metho ...
- Jersey(1.19.1) - Use of @Context
Previous sections have introduced the use of @Context. The JAX-RS specification presents all the sta ...
- Lombok(1.14.8) - @Log
@Log @Logs,生成一个日志对象. package com.huey.lombok; import lombok.extern.java.Log; @Log public class LogEx ...
- 如何将 select top 4 id from table1 赋值 给 declare @id1 int,@id2 int,@id3 int,@id4 int
declare @id1 int,@id2 int,@id3 int,@id4 int ),) select @sickcode = sickcode,@sfrq =sfrq from tablena ...
- base.AutoScaleMode = AutoScaleMode.Font; 方法“InitializeComponent”内的代码由设计器生成,不应手动修改。请移除任何更改,然后尝试重新打开设计器”。
http://www.taohuiduo.com 反编译后的工程文件用VS2010打开后,在打开窗体时会出现一系列错误提示: 第一种情况: “设计器无法处理第 152 行的代码: base.AutoS ...
- 229. Majority Element II My Submissions Question
Total Accepted: 23103 Total Submissions: 91679 Difficulty: Medium Given an integer array of size n, ...
- 分布式PostGIS系列【1】— 概述
一.Postgresql 分布式技术简介 二.分布式技术相关概念 三.分布式 Postgresql解决方案 四.相关资料与参考文献