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的更多相关文章

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

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

  2. 【POJ2104】K-th Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABToAAAJ2CAIAAADwi6oDAAAgAElEQVR4nOy9a5Pj1nnvi0/Q71Llj3

  3. 【POJ2104】K-th Number(主席树)

    题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...

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

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

  5. 【题解】kth异或和/魔改版线性基

    [题解]魔改版线性基 魔改版线性基解决此类问题. 联系线性空间的性质,我们直接可以构造出这样的基: \[ 100000 \\ 010000 \\ 000010 \\ 000001 \] 使得每个基的最 ...

  6. 【POJ2104】【整体二分+树状数组】区间第k大

    Description You are working for Macrohard company in data structures department. After failing your ...

  7. 【HDOJ6621】K-th Closest Distance(主席树,二分)

    题意:给定一个长为n的序列,有m次强制在线的询问,每次询问位置[L,R]中abs(a[i]-p)第k小的值 n,m<=1e5,a[i]<=1e6,p<=1e6,k<=169 思 ...

  8. 【bzoj2104】 K-th Number

    http://poj.org/problem?id=2104 (题目链接) 题意 求区间第k大数. Solution1 主席树裸题. 主席树当时我学是学的要死,那个时候不晓得百度出什么bug了,搜个主 ...

  9. 【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 ...

随机推荐

  1. oracle checkpoint 详解

    Oracle checkpoint详解 topcheckpoint扫盲 top什么是checkpoint 在数据库系统中,写日志和写数据文件是数据库中IO消耗最大的两种操作,在这两种操作中写数据文件属 ...

  2. [转]每次打开IE9就会跳出管理加载项的窗口

    当您打开IE9就跳出管理加载项的窗口,这一般是由于使用优化软件对系统进行优化清理后导致了注册表的错误.上次我们就为大家介绍了关于IE9启动后自动弹出管理加载项窗口的问题,这两个问题非常类似,引发问题的 ...

  3. JavaScript基础笔记二

    一.函数返回值1.什么是函数返回值    函数的执行结果2. 可以没有return // 没有return或者return后面为空则会返回undefined3.一个函数应该只返回一种类型的值 二.可变 ...

  4. OSI七层模型:TCP/IP && HTTP && WebSocket && MQTT

    OSI七层模型分为 物理层:  建立.维护.断开物理连接 处理bit流 数据链路层,将比特组合成字节进而组合成帧,用MAC地址访问介质,错误发现但不能纠正 处理数据帧 Frame 网络层,进行逻辑地址 ...

  5. MyBatis(3.2.3) - Mapped statements: The INSERT statement, Autogenerated keys

    We can use the useGeneratedKeys and keyProperty attributes to let the database generate the auto_inc ...

  6. Linux 命令 - watch: 反复执行命令,全屏显示输出

    watch 命令周期性地执行命令,全屏显示输出.可以通过 watch 命令反复执行某一程序来监视它的输出变化. 命令格式 watch [-dhvt] [-n <seconds>] [--d ...

  7. MongoDB - Introduction to MongoDB, BSON Types

    BSON is a binary serialization format used to store documents and make remote procedure calls in Mon ...

  8. Could not delete folder on Win7

    I had the same problem on Win 7 and this worked for me in command prompt. Solution 1: RD/S pathname ...

  9. php中preg_match用户名正则实例

    例子,字母.数字和汉字  代码如下 复制代码 if(preg_match("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|]|[|/|\|"||/& ...

  10. 【leetcode】5. Longest Palindromic Substring

    题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...