poj2104 K-th Number 主席树或划分树

Language:
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 36893 Accepted: 11863
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 10 9 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小。

/*********************************************************
  file name: poj2104.cpp
  author : kereo
  create time:  2015年03月31日 星期二 22时58分41秒
*********************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int sigma_size=26;
const int N=100+50;
const int MAXN=100000+50;
const int inf=0x3fffffff;
const double eps=1e-8;
const int mod=1000000000+7;
#define L(x) (x->ch[0])
#define R(x) (x->ch[1])
#define PII pair<int, int>
#define mk(x,y) make_pair((x),(y))
int n,m,cnt,top,num;
int a[MAXN],b[MAXN],st[30*MAXN];
struct node{
    int sz;
    node *ch[2];
}nod[30*MAXN],*root[MAXN],nil,*null;
struct Query{
    int l,r,k;
}q[MAXN];
struct CMT{
    void init(){
        cnt=top=0;
        nil.sz=0; null=&nil;
        L(null)=R(null)=null;
        for(int i=0;i<=n;i++)
            root[i]=null;
    }
    void newnode(node * &x,node *fa){
        if(top)
            x=&nod[st[--top]];
        else
            x=&nod[cnt++];
        if(fa == null){
            x->sz=1; 
            L(x)=R(x)=null;
        }
        else{
            x->sz=fa->sz+1; 
            L(x)=L(fa); R(x)=R(fa);
        }
    }
    void insert(node * &rt,node *fa,int l,int r,int pos){
        newnode(rt,fa);
        if(l == r)
            return ;
        int mid=(l+r)>>1;
        if(pos<=mid)
            insert(L(rt),L(fa),l,mid,pos);
        else
            insert(R(rt),R(fa),mid+1,r,pos);
    }
    int query(node *lq,node *rq,int l,int r,int k){
        if(l == r)
            return b[l];
        int sz=L(rq)->sz-L(lq)->sz;
        int mid=(l+r)>>1;
        if(k<=sz){
            lq=L(lq); rq=L(rq);
            return query(lq,rq,l,mid,k);
        }
        else{
            lq=R(lq); rq=R(rq);
            return query(lq,rq,mid+1,r,k-sz);
        }
    }
}cmt;
int main(){
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]); b[i]=a[i];
        }
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);
        sort(b+1,b+n+1);
        num=unique(b+1,b+n+1)-b-1;
        cmt.init();
        for(int i=1;i<=n;i++){
            int k=lower_bound(b+1,b+num+1,a[i])-b;
            cmt.insert(root[i],root[i-1],1,num,k);
        }
        for(int i=0;i<m;i++)
            printf("%d\n",cmt.query(root[q[i].l-1],root[q[i].r],1,num,q[i].k));
    }
    return 0;
}

解法二:划分树

<pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+100;
int n,m;
int a[MAXN];
struct node
{
    int v[MAXN];
    int num[MAXN];
}tree[25]; // 按层分的。
void build(int rt,int l,int r)
{
    if(l == r) return ;
    int mid=(l+r)>>1,same=mid-l+1;
    if(rt == 0)
    for(int i=l;i<=r;i++)
        if(tree[rt].v[i]<a[mid]) same--;
    int lf=l,rf=mid+1;
    for(int i=l;i<=r;i++){
        if(i == l)
            tree[rt].num[i]=0;
        else
            tree[rt].num[i]=tree[rt].num[i-1];
        if(tree[rt].v[i]<a[mid]){
            tree[rt].num[i]++;
            tree[rt+1].v[lf++]=tree[rt].v[i];
        }
        else if(tree[rt].v[i]>a[mid])
            tree[rt+1].v[rf++]=tree[rt].v[i];
        else{
            if(same){
                same--;
                tree[rt].num[i]++; tree[rt+1].v[lf++]=tree[rt].v[i];
            }
            else
                tree[rt+1].v[rf++]=tree[rt].v[i];
        }
    }
    build(rt+1,l,mid); build(rt+1,mid+1,r);
}
int query(int rt,int a,int b,int k,int l,int r)
{
    if(l == r)
        return tree[rt].v[a];// 边界需要再判断,到达叶子就找到该元素
    int mid=(l+r)>>1;
    int cnt1,cnt2;// cnt1记录[l,a-1]中记入左孩子的元素个数,cnt2记录区间[a,b]进入左孩子的元素个数
    if(a == l)
        cnt1=0;
    else
        cnt1=tree[rt].num[a-1];
    cnt2=tree[rt].num[b]-cnt1;
    if(cnt2>=k){ //说明[a,b]进入左孩子的数>=k
        a=l+cnt1;
        b=l+cnt2+cnt1-1;
        return query(rt+1,a,b,k,l,mid);
    }
    else {
        int x=a-l-cnt1;  //[l,a-1]中分到右孩子的个数
        int y=b-a+1-cnt2;//[a,b]分到右孩子的个数
        a=mid+x+1; b=mid+y+x;
        return query(rt+1,a,b,k-cnt2,mid+1,r);
    }
}
int main()
{
    //freopen("text.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            tree[0].v[i]=a[i];
        }
        sort(a+1,a+n+1);
        build(0,1,n);
        while(m--){
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",query(0,l,r,k,1,n));
        }
    }
    return 0;
}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值