POJ2104————K-th Number(线段树,二分法)
2016-12-04 13:00
459 查看
K-th Number
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
Sample Output
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
给你一个数列和m次查询,对于每个查询,求出给定区间第K大的数字
在一个区间内,如果一个数是第k大,那么肯定有k-1个数小于等于该数。
比如我们要查询x是不是区间内第k大的数,我们只需要知道区间内小于等于x的数的个数,这样就可以对数列进行二分确定答案
线段树表示每个区间的有序序列,那么当我们要查询x的时候,用二分搜索可以快速查询出区间小于x的数的个数
整个算法复杂度为nlogn+mlogn^3
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN=100010;
const int INF = 1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int a[MAXN];
vector <int> dat[MAXN<<2];
void pushup(int rt){
int l=rt<<1,r=rt<<1|1;
int i=0,j=0;
while(i<dat[l].size()||j<dat[r].size()){
//printf("%d %d\n",dat[l].size(),dat[r].size());
if(j==dat[r].size()){
while(i<dat[l].size()){
dat[rt].push_back(dat[l][i++]);
}
break;
}
else if(i==dat[l].size()){
while(j<dat[r].size()){
dat[rt].push_back(dat[r][j++]);
}
break;
}
else if(dat[l][i]<dat[r][j]){
dat[rt].push_back(dat[l][i]);
i++;
}
else{
dat[rt].push_back(dat[r][j]);
j++;
}
}
//merge(dat[l].begin(),dat[l].end(),dat[r].begin(),dat[r].end(),dat[rt].begin());
}
void build(int l,int r,int rt){
if(l==r){
dat[rt].push_back(a[l]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int query(int L,int R,int x,int l,int r,int rt){
if(L<=l&&r<=R){
return (upper_bound(dat[rt].begin(),dat[rt].end(),x))-dat[rt].begin();
}
int m=(l+r)>>1;
int res=0;
if(L<=m){
res+=query(L,R,x,lson);
}
if(R>m){
res+=query(L,R,x,rson);
}
return res;
}
int nums[MAXN];
int n,m;
void solve(){
for(int i=0;i<n;i++)
nums[i]=a[i];
sort(nums,nums+n);
build(0,n-1,1);
//for(int i=0;i<dat[1].size();i++)
// cout<<dat[1][i]<<" ";
//cout<<endl;
for(int i=0;i<m;i++){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
l--;
r--;
int lb=-1,ub=n-1;
while(ub-lb>1){
int m=(lb+ub)>>1;
int c=query(l,r,nums[m],0,n-1,1);
if(c>=k)
ub=m;
else
lb=m;
}
printf("%d\n",nums[ub]);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;i++)
scanf("%d",a+i);
for(int i=0;i<MAXN*4;i++)
dat[i].clear();
solve();
}
}
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 51227 | Accepted: 17511 | |
Case Time Limit: 2000MS |
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.
给你一个数列和m次查询,对于每个查询,求出给定区间第K大的数字
在一个区间内,如果一个数是第k大,那么肯定有k-1个数小于等于该数。
比如我们要查询x是不是区间内第k大的数,我们只需要知道区间内小于等于x的数的个数,这样就可以对数列进行二分确定答案
线段树表示每个区间的有序序列,那么当我们要查询x的时候,用二分搜索可以快速查询出区间小于x的数的个数
整个算法复杂度为nlogn+mlogn^3
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN=100010;
const int INF = 1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int a[MAXN];
vector <int> dat[MAXN<<2];
void pushup(int rt){
int l=rt<<1,r=rt<<1|1;
int i=0,j=0;
while(i<dat[l].size()||j<dat[r].size()){
//printf("%d %d\n",dat[l].size(),dat[r].size());
if(j==dat[r].size()){
while(i<dat[l].size()){
dat[rt].push_back(dat[l][i++]);
}
break;
}
else if(i==dat[l].size()){
while(j<dat[r].size()){
dat[rt].push_back(dat[r][j++]);
}
break;
}
else if(dat[l][i]<dat[r][j]){
dat[rt].push_back(dat[l][i]);
i++;
}
else{
dat[rt].push_back(dat[r][j]);
j++;
}
}
//merge(dat[l].begin(),dat[l].end(),dat[r].begin(),dat[r].end(),dat[rt].begin());
}
void build(int l,int r,int rt){
if(l==r){
dat[rt].push_back(a[l]);
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int query(int L,int R,int x,int l,int r,int rt){
if(L<=l&&r<=R){
return (upper_bound(dat[rt].begin(),dat[rt].end(),x))-dat[rt].begin();
}
int m=(l+r)>>1;
int res=0;
if(L<=m){
res+=query(L,R,x,lson);
}
if(R>m){
res+=query(L,R,x,rson);
}
return res;
}
int nums[MAXN];
int n,m;
void solve(){
for(int i=0;i<n;i++)
nums[i]=a[i];
sort(nums,nums+n);
build(0,n-1,1);
//for(int i=0;i<dat[1].size();i++)
// cout<<dat[1][i]<<" ";
//cout<<endl;
for(int i=0;i<m;i++){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
l--;
r--;
int lb=-1,ub=n-1;
while(ub-lb>1){
int m=(lb+ub)>>1;
int c=query(l,r,nums[m],0,n-1,1);
if(c>=k)
ub=m;
else
lb=m;
}
printf("%d\n",nums[ub]);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;i++)
scanf("%d",a+i);
for(int i=0;i<MAXN*4;i++)
dat[i].clear();
solve();
}
}
相关文章推荐
- 用线段树求区间第K大(POJ 2104 K-th Number)
- POJ 2104 K-th Number 区间第K大,可持久化线段树
- poj2104 K-th Number (函数式线段树)
- POJ 2104 K-th Number (可持久化线段树)
- [POJ]2104 K-th Number 主席树&线段树合并&整体二分
- poj-2104 K-th Number[主席树/函数式线段树/可持久化线段树]
- poj 2104 K-th Number(线段树)
- poj 2104 K-th Number(可持久线段树)
- POJ 2104 K-th number 主席树 函数式线段树
- poj 2104 K-th Number(可持久化线段树)
- POJ-2104-K-th Number(函数式线段树)
- 主席树(可持久化线段树)讲解 [POJ 2104] K-th Number
- POJ2104 K-th number 函数式线段树
- 【原】 POJ 2104 K-th Number 线段树 划分树 合并树 解题报告
- POJ 2104 K-th Number 线段树
- POJ_2104_K-th Number_线段树(归并树)
- POJ-2104 K-th Number(线段树[归并树]-区间第k大)
- POJ 2104 K-th Number(分桶,线段树,主席树)
- poj 2104 K-th Number 函数式线段树
- POJ 2104 K-th Number (可持久化线段树)