poj 2104 K-th Number

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

Northeastern Europe 2004, Northern Subregion
思路:主席树模板,求第k大的数。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100001
using namespace std;
int n,m,tot,cnt,num[MAXN],hash[MAXN],root[MAXN];
int sum[MAXN*20],lchild[MAXN*20],rchild[MAXN*20];
void HASH(){
    sort(hash+1,hash+1+n);
    tot=unique(hash+1,hash+1+n)-(hash+1);//求去重后元素的个数 
    for(int i=1;i<=n;i++)
        num[i]=lower_bound(hash+1,hash+tot+1,num[i])-hash;
}
void build(int x,int &y,int l,int r,int k){
    y=++cnt;
    sum[y]=sum[x]+1;
    if(l==r)    return ;
    int mid=(l+r)/2;
    if(k<=mid){
        rchild[y]=rchild[x];
        build(lchild[x],lchild[y],l,mid,k);
    }
    else{
        lchild[y]=lchild[x];
        build(rchild[x],rchild[y],mid+1,r,k);
    }
}
int query(int x,int y,int l,int r,int k){
    if(l==r)    return l;
    int mid=(l+r)/2;
    int tmp=sum[lchild[y]]-sum[lchild[x]];
    if(tmp>=k)    return query(lchild[x],lchild[y],l,mid,k);
    else return query(rchild[x],rchild[y],mid+1,r,k-tmp);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&num[i]);
        hash[i]=num[i];
    }
    HASH();
    for(int i=1;i<=n;i++)
        build(root[i-1],root[i],1,tot,num[i]);
    for(int i=1;i<=m;i++){
        int x,y,k;
        scanf("%d%d%d",&x,&y,&k);
        printf("%d\n",hash[query(root[x-1],root[y],1,tot,k)]);
    }
}

 

posted @ 2017-09-06 21:52  一蓑烟雨任生平  阅读(119)  评论(0编辑  收藏  举报