1. 程式人生 > >POJ2104————K-th Number(線段樹,二分法)

POJ2104————K-th Number(線段樹,二分法)

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

給你一個數列和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();
	}
}