HDOJ2665 Kth number --- 划分树求区间第k小数

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2665

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

题意: 有n组样例,每次输入n,m代表n个数(1...n)和m次询问。 然后输入这n个数用a数组存下来。

每次询问给3个数l,r,k,表示询问第a[l...r]中第k小的数。

注: 这题有点坑了,题目说是求第k大数,其实是求k小。。。

题解:单纯的求第k小数,可以用快排和优先队列来做,但是这题要求m次询问,这两种方法明显复杂度大了。

这种题我所知道的可以用划分树和主席树来做,不过由于我太菜不会主席树,所以此题采用划分树来做。

这题基本就是一个裸的划分树模板,划分树的资料网上很多,不会的话花一个多小时的时候差不多能看懂的。写划分树的时候需要注意的是Query函数里面有个大的数组范围L和R,还有个小的查询范围l,r,一定要注意每次递归访问数组的时候不能越过L--R的边界,不然就会像我一样一直Runtime Error。

 

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
// 从1开始计数
int a[22][111111];  // 保存划分树
int sorted[111111]; 
int num[22][111111]; // 记录进入左子树的数目
int n,m;
int l,r,k; // 每次询问的区间和

// 建树
void Build(int l,int r,int depth) {
    if(l == r) {
        return;  // 结束递归
    }
    int mid = (l + r) / 2;    
    int same = 0;// 记录有多少与mid的数相等且会进入左子树的元素
    for(int i = l;i <= mid;i++)
        if(sorted[i] == sorted[mid])
            same++;
    int left = l,right = mid+1;    
    num[depth][l] = 0;
    for(int i = l;i <= r;i++) {                
        if(i != l) {
            num[depth][i] = num[depth][i-1];
        }
        if(a[depth][i] < sorted[mid] || (a[depth][i] == sorted[mid] && same > 0)) {
            num[depth][i]++;
            a[depth+1][left++] = a[depth][i];
            if(a[depth][i] == sorted[mid])
                same--;            
        } else {
            a[depth+1][right++] = a[depth][i];
        }        
    }             
    Build(l,mid,depth+1);
    Build(mid+1,r,depth+1);
}

// 查询区间第k小数
int Query(int l,int r,int L,int R,int k,int depth) {
    if(l == r)
        return a[depth][l];        
    int cnt;
    if(l-1>=L) {    
        cnt = num[depth][r] - num[depth][l-1];// 区间中进入左子树的个数
    }
    else {
        cnt = num[depth][r];// 区间中进入左子树的个数
    }        
    int mid = (L + R) / 2;
    if(k <= cnt) {
        // 进入左子树查询
        int newl;
        if(l-1 >= L)
        	newl = L + (num[depth][l-1]);
        else
			newl = L;	
        int newr = newl + cnt - 1;
        return Query(newl,newr ,L,mid,k,depth+1);
    } else {
        int newr = R - (R-r - (num[depth][R]-num[depth][r]));
        int newl = newr - (r-l+1-cnt) + 1;
        return Query(newl,newr,mid+1,R,k-cnt,depth+1);
    }
}


int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++) {
            scanf("%d",&a[1][i]);
        }
        for(int i = 1;i <= n;i++)
            sortn ed[i] = a[1][i];
        sort(sorted+1,sorted+1+n);
        Build(1,n,1);                
        // m次问
        for(int i = 0;i < m;i++) {
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",Query(l,r,1,n,k,1));
        }
        
    }
    
    return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值