POJ2104————K-th Number(线段树,二分法)
来源:互联网 发布:淘宝优化 编辑:程序博客网 时间:2024/04/29 13:59
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 51227 Accepted: 17511Case 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.
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).
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 31 5 2 6 3 7 42 5 34 4 11 7 3
Sample Output
563
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|1int 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;elselb=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();}}
0 0
- POJ2104————K-th Number(线段树,二分法)
- poj2104——K-th Number(划分树)
- poj2104——K-th Number
- POJ2104 —— K-th number
- K-th Number【POJ2104】——主席树
- poj2104 K-th Number (线段树实现)
- POJ2104 K-th Number 【线段树+二分】
- POJ2104 K-th Number —— 划分树(模板题)
- POJ2104--K-th Number(主席树)
- poj2104 K-th Number(划分树)
- poj2104 K-th Number(主席树)
- POJ2104 K-th Number(主席树)
- POJ2104 K-th Number (主席树)
- (划分树)poj2104 K-th Number
- poj2104 K-th Number (函数式线段树)
- poj2104 K-th Number 离散+可持久化线段树
- 【POJ2104】K-th Number-主席树(可持久化线段树)+离散化
- [poj2104]K-th Number
- 短信的接收流程
- Longest Palindromic Substring
- 总结彻底解决Spring MVC+Mybatis中文乱码问题
- 设计模式_单例模式
- RPC是什么?
- POJ2104————K-th Number(线段树,二分法)
- 电子科技大学第八届ACM趣味程序设计竞赛第二场题解
- 使用AJAX异步提交表单的几种方式
- 小鑫爱运动
- OFFICE--onenote的一个坑
- 笔记-软件方法-上册-业务建模和需求
- Leetcode 之GameofLife
- 用PySide编写数字时钟
- hash_map/unordered_map原理和使用