hdu 2665 Kth number

本文探讨了如何高效地查询区间内的第k大数,并通过划分树(Merge Tree)实现算法优化,适用于大规模数据集。文章详细介绍了算法原理、实现细节以及性能分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3329    Accepted Submission(s): 1107


Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 

Input
The first line is the number of the test cases. 
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere. 
The second line contains n integers, describe the sequence. 
Each of following m lines contains three integers s, t, k. 
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
 

Output
For each test case, output m lines. Each line contains the kth big number.
 

Sample Input
1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2
 

Sample Output
2
 


分析:询问区间[s,t]内第k大的数

划分树:

 

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100001;
template<class T>
inline bool scan_d(T &ret){
    char c;bool sgn;
    if(c=getchar(),c==EOF)return 0;
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    sgn=(c=='-');
    ret=sgn?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9')ret=ret*10+(c-'0');
    if(sgn)ret=-ret;
    return 1;
}
int arr[N],sorted[N],val[18][N],toleft[18][N];
void build(int l,int r,int d){
    if(l==r)return;
    int m=(l+r)>>1;
    int same=m-l+1,i=l,j=m+1,k;
    for(k=l;k<=r;k++)if(val[d][k]<sorted[m])same--;
    for(k=l;k<=r;k++){
        if(val[d][k]<sorted[m])val[d+1][i++]=val[d][k];
        else if(val[d][k]==sorted[m]&&same>0){
            val[d+1][i++]=val[d][k];
            same--;
        }else val[d+1][j++]=val[d][k];
        toleft[d][k]=toleft[d][l-1]+i-l;
    }
    build(l,m,d+1);
    build(m+1,r,d+1);
}
int query(int L,int R,int l,int r,int d,int k){
    if(l==r)return val[d][l];
    int m=(L+R)>>1;
    int cnt=toleft[d][r]-toleft[d][l-1];
    if(cnt>=k){
        int nl=L+toleft[d][l-1]-toleft[d][L-1];
        int nr=nl+cnt-1;
        return query(L,m,nl,nr,d+1,k);
    }else{
        int nr=r+toleft[d][R]-toleft[d][r];
        int nl=nr-(r-l-cnt);
        return query(m+1,R,nl,nr,d+1,k-cnt);
    }
}
int main(){
    int T,n,m,s,t,k,i;
    scanf("%d",&T);
    while(T--){
        scan_d(n),scan_d(m);
        for(i=1;i<=n;i++){
            scan_d(val[0][i]);
            sorted[i]=val[0][i];
        }
        sort(sorted+1,sorted+n+1);
        build(1,n,0);
        while(m--){
            scan_d(s),scan_d(t),scan_d(k);
            printf("%d\n",query(1,n,s,t,0,k));
        }
    }
    return 0;
}


归并树(但超时)

#include<cstdio>
const int N=100002;
struct MergeTree{
    int l,r,m;
}a[N<<2];
int arr[N],val[18][N];
void build(int l,int r,int d,int rt){
    a[rt].l=l,a[rt].r=r,a[rt].m=(l+r)>>1;
    if(l==r){val[d][l]=arr[l];return ;}
    int m=a[rt].m;
    build(l,m,d+1,rt<<1);
    build(m+1,r,d+1,rt<<1|1);
    int i=l,j=m+1,k=l;
    while(i<=m&&j<=r){
        if(val[d+1][i]<val[d+1][j])val[d][k++]=val[d+1][i++];
        else val[d][k++]=val[d+1][j++];
    }
    while(i<=m)val[d][k++]=val[d+1][i++];
    while(j<=r)val[d][k++]=val[d+1][j++];
    
}
int L,R;
int lowb(int key,int d,int rt){
    if(a[rt].l==a[rt].r){
        return key<=val[d][a[rt].m]?a[rt].m:a[rt].m+1;
    }
    if(key<=val[d][a[rt].m])return lowb(key,d,rt<<1);
    else return lowb(key,d,rt<<1|1);
}
int find(int key,int d,int rt){
    if(L<=a[rt].l&&R>=a[rt].r)return lowb(key,d,rt)-a[rt].l;
    int ans=0;
    if(L<=a[rt].m)ans+=find(key,d+1,rt<<1);
    if(R>a[rt].m)ans+=find(key,d+1,rt<<1|1);
    return ans;
}
int query(int l,int r,int k){
    int m,tmp;
    while(l<r){
        m=(l+r+1)>>1;
        tmp=find(val[0][m],0,1);
        if(tmp<k)l=m;
        else r=m-1;
    }
    return l;
}
int main(){
    int T,n,m,i,k;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)scanf("%d",arr+i);
        build(1,n,0,1);
        while(m--){
            scanf("%d%d%d",&L,&R,&k);
            printf("%d\n",val[0][query(1,n,k)]);
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值