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

题意:给出一列数组和几组查询,对于每个查询(i,j,k),要求输出a(i)~a(j)的升序排列的第k大数。

思路:挑战者上的题目,上面采用的是平方分割法,总体来讲,线段树的复杂度肯定是比平方分割的复杂度更小,但平方分割的实现更加简单易懂。

但是敲了一遍书上的代码,不知道是不是手残,敲完以后总有个别数据对不上答案(心累,调了一个小时不知所以然)。

给出AC代码

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std; #define MAXN 100010
#define MAXM 5010
#define B 1000 //桶的大小 //输入
int n,m;
int a[MAXN];
int L[MAXM],R[MAXM],K[MAXM]; int num[MAXN]; //对A排序后的结果
vector<int> bucket[MAXN/B]; //每个桶排序后的结果 void solve()
{
for(int i=0;i<n;i++)
{
bucket[i/B].push_back(a[i]);
num[i]=a[i];
}
sort(num,num+n);
for(int i=0;i<n/B;i++)
sort(bucket[i].begin(),bucket[i].end());
for(int i=0;i<m;i++)
{
//求[L,R]区间的第K个数
int l=L[i]-1,r=R[i],k=K[i];
int left=-1,right=n-1,mid;
while(right-left>1)
{
mid=(left+right)/2;
int x=num[mid];
int tl=l,tr=r,c=0; //区间两端多出的部分
while(tl<tr&&tl%B!=0){
if(a[tl++]<=x) c++;
}
while(tl<tr&&tr%B!=0){
if(a[--tr]<=x) c++;
} //对每个桶进行计算
while(tl<tr)
{
int b=tl/B;
c+=upper_bound(bucket[b].begin(),bucket[b].end(),x)-bucket[b].begin();
tl+=B;
} if(c>=k)
right=mid;
else
left=mid;
}
printf("%d\n",num[right]);
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d%d%d",&L[i],&R[i],&K[i]);
solve();
return 0;
}

平方分割poj2104K-th Number的更多相关文章

  1. POJ2104 K-th Number 静态区间第k最值 平方分割

    干掉这道题的那一刻,我只想说:我终于**的AC了!!! 最终内存1344K,耗时10282ms,比起归并树.划分树以及其他各种黑科技,这个成绩并不算光彩⊙﹏⊙ 但至少,从最初的无数次TLE到最终的AC ...

  2. ZOJ2112 Dynamic Rankings 动态区间第K最值 平方分割

    有了上一题的经验(POJ的静态区间第K最值)再解决这道题就轻松多了 空间5256KB,时间3330ms,如果把动态开点的平衡树换成数组模拟的话应该会更快 之所以选择了平方分割而不是树套树,不仅是所谓趁 ...

  3. POJ2104 (平方分割)二分查找理解。

    题意:任意区间求第k大数 思路: 预处理:利用平方分割(分桶法)把区间切割成B = sqrt(n)大小的一块块,然后每个各自排序. 二分第k大数x,接着就需要求[l,r]区间中x的排名,与k比较,将两 ...

  4. 静态区间第k大(分桶法和平方分割)

    POJ 2104为例 思想: <挑战程序设计竞赛>中介绍的方法. 分桶法:把一排物品或者平面分成桶,每个桶分别维护自己内部的信息,已达到高效计算的目的. 设一共有n个数,每b个分到一个桶里 ...

  5. Big String 块状数组(或者说平方分割)

    Big String 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 如果直接模拟的话, ...

  6. POj 2104 K-th Number (分桶法+线段树)

    题目链接 Description You are working for Macrohard company in data structures department. After failing ...

  7. POJ2104 K-th Number(归并树)

    平方分割一直TLE,最后用归并树处理过了,使用STL会比较慢. #include<cstdio> #include<iostream> #include<cstdlib& ...

  8. BZOJ 2440 中山市选2011 全然平方数 二分答案+容斥原理+莫比乌斯反演

    题目大意:求第k个无平方因子数是多少(无视原题干.1也是全然平方数那岂不是一个数也送不出去了? 无平方因子数(square-free number),即质因数分解之后全部质因数的次数都为1的数 首先二 ...

  9. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

随机推荐

  1. 使用XML定义组件样式

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content& ...

  2. python 安装依赖几个问题---HttpScan

    https://blog.csdn.net/chenggong2dm/article/details/61923420 https://www.cnblogs.com/caochuangui/p/59 ...

  3. 欧拉 路径&&回路

    不管 欧拉回路  还是  欧拉路径  无向图或者有向图(删除方向后)要联通 欧拉路径存在的判定条件 1   无向图   度数为奇数的点最多有两个 2   有向图   最多只能有两个点的入度不等于出度  ...

  4. TCP/IP学习笔记(4)------ICMP,ping,traceroute

    IMCP协议介绍 当传送IP数据包发生错误--比如主机不可达,路由不可达等等,ICMP协议将会把错误信息封包,然后传送回给主机.给主机一个处理错误的机会,这 也就是为什么说建立在IP层以上的协议是可能 ...

  5. TCP/IP学习笔记(3)----IP,ARP,RARP协议

    把这三个协议放到一起学习是因为这三个协议处于同一层(网络层协议),ARP协议用来找到目标主机的Ethernet网卡Mac地址,IP则承载要发送的消息.数据链路层可以从ARP得到数据的传送信息,而从IP ...

  6. Network-POJ3694(最小公共祖先LCA+Tarjin)

    http://poj.org/problem?id=3694 这一题  为什么要找最小祖先呢 当两个节点连到一块的时候  找最小公共节点就相当于找强连通分支 再找最小公共节点的过程中直到找到  这个过 ...

  7. spring-kafka

    spring-kafka 使用spring-kafka的小伙伴,看过来. 说明 因为spring-kafka封装的比较厉害,可能跟你实际使用起来有很大的差别. 一个简单的消费例子 在spring-bo ...

  8. 洛谷 P1081 开车旅行(70)

    P1081 开车旅行 题目描述 小AA 和小BB 决定利用假期外出旅行,他们将想去的城市从 11到 NN 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ii的海 ...

  9. spring mvc随便接收list<objeect>参数

    在后台设定一个类,PersonList类: public class PersonList {private List<User> user; public List<User> ...

  10. JAVA OO之 类、对象、包

    类 类是具有相同属性和行为的一组对象的集合. public class HomeWork1 { public static void main(String[] args) { MyPhone mp= ...