1. K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 34048   Accepted: 10810
Case Time Limit: 2000MS

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

Source

Northeastern Europe 2004, Northern Subregion
一句话题意:题目意思很简单,多次询问区间k小值
分析:没什么好分析的,模板题,原本想用划分树做,然后去贴吧问……结果被喷“时代的默泪”,ORZ……然后默默的去看主席树了
(主席树资料网上很多,大家百度一下)
这里说下主要思想,就是对1~n每个位置建立一颗线段树,表示从开头到此位置每个数字在各个区间出现次数(当然要先离散……),当然这样空间不行,但是可以发现所有线段树形状一样只是sum域不同,且第i颗和第i-1颗相比,只是在第i-1颗的基础上插入了第i个点(即只修改了第i-1颗线段树的一条从上到下的路径,其他都没改变),因此只要建立一个主线段树,没变的指一条边过去就可以了。
然后询问[a,b]可以发现线段树可以减,即[a,b]的线段树相当于[1,b]线段树(对应第b颗线段树)-[1,a-1]线段树(对应第a-1颗),然后判断下左子树大小和k的关系就行了
code:
 #include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct wjmzbmr
{
int l,r,ls,rs,sum;
}f[maxn*];
int tot,root[maxn+],q,b[maxn+],sortb[maxn+];
int build(int l,int r)
{
int k=++tot;
f[k]={l,r,,,};
if(l==r) return tot;
int mid=(l+r)>>;
f[k].ls=build(l,mid);
f[k].rs=build(mid+,r);
return k;
}
int change(int o,int x,int v)
{
int k=++tot;
f[k]=f[o];f[k].sum+=v;
if(f[o].l==x&&f[o].r==x) return k;
int mid=(f[o].l+f[o].r)>>;
if(x<=mid) f[k].ls=change(f[o].ls,x,v);else f[k].rs=change(f[o].rs,x,v);
return k;
}
int ask(int a,int b,int k)
{
if(f[b].l==f[b].r) return f[b].l;
int mid=f[f[b].ls].sum-f[f[a].ls].sum;
if(k<=mid) return ask(f[a].ls,f[b].ls,k);else return ask(f[a].rs,f[b].rs,k-mid);
}
int main()
{
freopen("ce.in","r",stdin);
freopen("ce.out","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;++i) scanf("%d",&b[i]),sortb[i]=b[i];
sort(sortb+,sortb++n);
q=;tot=;
for(int i=;i<=n;++i) if(sortb[q]!=sortb[i]) sortb[++q]=sortb[i];
root[]=build(,q);
for(int i=;i<=n;++i)
{
int p=lower_bound(sortb+,sortb+n+,b[i])-sortb;
root[i]=change(root[i-],p,);
}
for(int i=;i<=m;++i)
{
int a,b,k;
scanf("%d%d%d",&a,&b,&k);
printf("%d\n",sortb[ask(root[a-],root[b],k)]);
}
}
return ;
}
 

[POJ2104]K-th Number的更多相关文章

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

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

  2. 【POJ2104】K-th Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABToAAAJ2CAIAAADwi6oDAAAgAElEQVR4nOy9a5Pj1nnvi0/Q71Llj3

  3. C++之路进阶——poj2104(K-th Number)

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

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

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

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

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

  6. poj2104:K-th Number

    思路:可持久化线段树,利用权值线段树,把建树过程看成插入,插入第i个元素就在第i-1棵树的基础上新建结点然后得到第i棵树,那么询问区间[l,r]就是第r棵树上的信息对应减去第l-1棵树上的信息,然后再 ...

  7. POJ2104:K-th Number——题解

    http://poj.org/problem?id=2104 题目大意:求区间第k小. —————————————————————————— 主席树板子题. ……我看了半天现在还是一知半解的状态所以应 ...

  8. poj2104 K大数 划分树

    题意:给定一个数列,求一个区间的第K大数 模板题, 其中的newl, newr 有点不明白. #include <iostream> #include <algorithm> ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  10. 整体二分初识--POJ2104:K-th Number

    n<=100000个数有m<=5000个询问,每次问区间第k大. 方法一:主席树!…… 方法二:整体二分. 整体二分一次性计算半个值域对一个区间的询问的贡献,然后根据“这半边的贡献在某个询 ...

随机推荐

  1. Mycat实现读写分离,主备热切换

    实验环境:ubutu server 14 Master IP:172.16.34.212 Slave IP:172.16.34.34.156 Mycat server IP:172.16.34.219 ...

  2. /etc/fstab 参数详解及如何设置开机自动挂载

    某些时候当Linux系统下划分了新的分区后,需要将这些分区设置为开机自动挂载,否则,Linux是无法使用新建的分区的. /etc/fstab 文件负责配置Linux开机时自动挂载的分区. Window ...

  3. 1---------java调用NLPIR(ICTCLAS2016)实现分词功能

    备注:win7 64位系统,netbeans编程 NLPIR分词系统,前身是2000年发布的ICTCLAS,2009年更为现名.张华平博士打造. 实现步骤: 1.在Netbeans中,文件→新建项目→ ...

  4. eclipse 提示错误**cannot be resolved to a type

    这是某个对象不能识别为类型,比如你写了个类,名字叫Hello,如果你调用它的时候不小心写成hello,或者helo,那么就会报这样的错误,很容易改正的,只要你细心一点

  5. gdb 定位 oops call trace

    [    1.454380] BUG: unable to handle kernel NULL pointer dereference at 00000000000005d0[    1.47402 ...

  6. 留念 C语言第一课简单的计算器制作

    留念 C语言第一课简单的计算器制作 学C语言这么久了.  /* 留念 C语言第一课简单的计算器制作 */   #include<stdio.h>  #include<stdlib.h ...

  7. 报表开发工具中开放的部分图表js接口列表

    1.. 描述 报表开发工具FineReport 8.0版本中开放了部分图表js接口,在具体应用的过程中很多人都不知道这些接口到底有什么作用,该怎么应用,所以根据我自己的应用在下面归纳了这些新开放js接 ...

  8. Windows 10 Threshold 2 升级记录

    昨天(11月17日)升级到Windows 10 Threshold 2版本.我的使用的设备是Surface Pro 3,4G内存,128G硬盘. Threshold 2是作为一个Windows系统更新 ...

  9. 网络之OSI七层模型

    1.  物理层:设备之间的比特流的传输,物理接口,电气特性等 2.  数据链路层:成帧,用MAC地址访问媒介,错误检测与修正 3.  网络层:提供逻辑地址(IP),选路 4.  传输层:可靠与不可靠的 ...

  10. poj1001 Exponentiation 大数的幂

    Description Problems involving the computation of exact values of very large magnitude and precision ...