K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 44537   Accepted: 14781
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

题目大意:给你一段序列,求给定区间[u,v]中第k小的值

代码:

 #include<cstdio>
#include<iostream>
#include<algorithm>
#define maxn 1000100 using namespace std; int n,m,sz;
int hash[maxn],a[maxn],root[maxn],ls[maxn*],rs[maxn*],sum[maxn*]; void updata(int l,int r,int x,int &y,int v)
{
y=++sz;
sum[y]=sum[x]+;
if (l==r) return;
ls[y]=ls[x];
rs[y]=rs[x];
int mid=(l+r)>>;
if (v<=mid) updata(l,mid,ls[x],ls[y],v);
else updata(mid+,r,rs[x],rs[y],v);
} int que(int L,int R,int K)
{
int l=,r=n,mid,x,y;
x=root[L-];
y=root[R];
while (l!=r)
{
mid=(l+r)>>;
if (K<=sum[ls[y]]-sum[ls[x]])
{
x=ls[x];
y=ls[y];
r=mid;
}
else
{
K-=(sum[ls[y]]-sum[ls[x]]);
x=rs[x];
y=rs[y];
l=mid+;
}
}
return l;
} int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
scanf("%d",&a[i]),hash[i]=a[i];
sort(hash+,hash+n+);
for (int i=;i<=n;i++)
{
int w=lower_bound(hash+,hash+n+,a[i])-hash;
updata(,n,root[i-],root[i],w);
}
for (int i=;i<=m;i++)
{
int l,r,v;
scanf("%d%d%d",&l,&r,&v);
printf("%d\n",hash[que(l,r,v)]);
}
return ;
}

C++之路进阶——poj2104(K-th Number)的更多相关文章

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

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

  2. 【POJ2104】K-th Number

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABToAAAJ2CAIAAADwi6oDAAAgAElEQVR4nOy9a5Pj1nnvi0/Q71Llj3

  3. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...

  4. C++之路进阶codevs1269(匈牙利游戏)

    1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description ...

  5. c++之路进阶——hdu3507(Print Article)

    参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种 ...

  6. C++之路进阶——hdu2222(Keywords Search)

    /*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...

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

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

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

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

  9. C++之路进阶——HDU1880(魔咒词典)

    ---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 3 ...

随机推荐

  1. 【转】C#中HttpWebRequest的用法详解

    本文实例讲述了C#中HttpWebRequest的用法.分享给大家供大家参考.具体如下: HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来 ...

  2. 【转】Eclipse Class Decompiler——Java反编译插件

    闲暇之余,写了一个Eclipse下的Java反编译插件:Eclipse Class Decompiler,整合了目前最好的2个Java反编译工具Jad和JD-Core,并且和Eclipse Class ...

  3. css中textarea去掉边框和选中后的蓝色边框问题的解决方法

    我们在设计网页的输入框时,有时会遇到需要把textarea的边框去掉的问题,经过测试,下面的代码是可以的. textarea{ border: solid 0px; outline:none; }

  4. C++内存问题大集合(指针问题,以及字符串拷贝问题,确实挺危险的)

    作者:rendao.org,版权声明,转载必须征得同意. 内存越界,变量被篡改 memset时长度参数超出了数组长度,但memset当时并不会报错,而是操作了不应该操作的内存,导致变量被无端篡改 还可 ...

  5. C++ 简易时间类

    .h file #ifndef LIBFRAME_DATETIME_H_ #define LIBFRAME_DATETIME_H_ #include <stdint.h> #include ...

  6. iOS开发教程之:iPhone开发环境搭建

    安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...

  7. iOS NSURLSession 下载

    周五的时候,有个新的需求,要下载脚本,帮助玩家自动打怪,应该也是挂机的意思吧! 组长让我设计界面,让汤老师设计数据等.我觉得数据的挑战性更大一点,然后就接过来了. 自己还没有形成互联网思维,所以弄了一 ...

  8. thinkphp 加载静态框架frameset frame 浏览器显示空白

    我觉得静态框架这个东西非常奇怪,可能是因为没有研究透它. 我的情况是这样的,我之前做过的一个面向对象没有基于thinkPHP,的项目中用同一套后台静态框架没有问题,但用thinkphp后台的index ...

  9. 【转】TableLayout(表格布局)

    转自:http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864536.html TableLayout(表格布局) 表格布局模型以行列的形式管 ...

  10. @SuppressWarnings

    http://www.cnblogs.com/fsjohnhuang/p/4040785.html 一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“ ...