码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2104 K-th Number[主席树]

时间:2016-12-12 01:56:40      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:rip   art   struct   ssi   let   values   namespace   tput   span   

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

document:
1.http://blog.csdn.net/metalseed/article/details/8045038
2.http://www.cnblogs.com/oyking/p/3230296.html
 
解决区间极值查询问题
主席树就是对每个前缀做了一个线段树
这样相当于处理了前缀和
提取一段区间,就是T[j]-T[i-1]
每棵线段树都按照[1,n]建树(保证链可以共用)
离散化后按照序列的顺序建树
T[i+1]和T[i]只有一条链不同,其他的可以共用
 
插入操作就是只把新链上的节点新建,其他的都用上一课树的
查询操作就是一个kth的过程,只不过要用两棵树的差值
 
PS:两种建树写法,貌似用引用比较快
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<0||c>9){if(c==-)f=-1; c=getchar();}
    while(c>=0&&c<=9){x=x*10+c-0; c=getchar();}
    return x*f;
}
int n,m,mp[N],l,r,k;
struct data{
    int v,id;
    bool operator <(const data &r)const{return v<r.v;}
}a[N];
struct node{
    int lc,rc,size;
}t[N*20];
int cnt=0,root[N];
void insert(int num,int &x,int l,int r){//printf("ins %d %d %d\n",l,r,x);
    cnt++;
    t[cnt]=t[x];x=cnt;
    ++t[x].size;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(num<=mid) insert(num,t[x].lc,l,mid);
    else insert(num,t[x].rc,mid+1,r);
}
int ins(int num,int pre,int l,int r){
    int x=++cnt;
    t[x]=t[pre]; ++t[x].size;
    if(l==r) return x;
    int mid=(l+r)>>1;
    if(num<=mid) t[x].lc=ins(num,t[x].lc,l,mid);
    else t[x].rc=ins(num,t[x].rc,mid+1,r);
    return x;
}
int query(int i,int j,int l,int r,int k){
    if(l==r) return l;
    int ls=t[t[j].lc].size-t[t[i].lc].size;
    int mid=(l+r)>>1;
    if(k<=ls) return query(t[i].lc,t[j].lc,l,mid,k);
    else return query(t[i].rc,t[j].rc,mid+1,r,k-ls);
}

int main(){
    n=read();m=read();
    for(int i=1;i<=n;i++) a[i].v=read(),a[i].id=i;
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++) mp[a[i].id]=i;
    
    for(int i=1;i<=n;i++){
        //root[i]=ins(mp[i],root[i-1],1,n);
        root[i]=root[i-1];
        insert(mp[i],root[i],1,n);
    }
    while(m--){
        l=read();r=read();k=read();
        printf("%d\n",a[query(root[l-1],root[r],1,n,k)].v);
        
    }
}

 

 
 

POJ2104 K-th Number[主席树]

标签:rip   art   struct   ssi   let   values   namespace   tput   span   

原文地址:http://www.cnblogs.com/candy99/p/6160704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!