分桶方法和平方分割

分桶法:是把一排物品或者平面分成桶,每个桶分别维护自己内部的信息,以达到高效计算的目的方法。

平方分割:是把排成一列的n个元素每√n个分在一个桶内进行维护的方法的统称。

平方分割和线段树

一般地,如果线段树和平方分割都能实现某个功能,多数情况下线段树会比平方分割快,但是因为平方分割在实现上比线段树简单,所以如果运行时间限制不是太紧时,也可以考虑使用平方分割。

 

例题(题目连接

K-th Number

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

因为查询m的个数很大,朴素的求法无法在规定时间内出解,因此应该选取合理的方法维护数据来做高效地查询。

如果x是第k个数,那么一定有

在区间中不超过x的数不少于k个

在区间中小于x的数有不到k个

因此如果可以快速求出区间里不超过x的个数,就可以通过对x进行二分搜索来求出第k个数是多少。

用平方分割解决,首先把数列每b个一组分到各个桶里,每一个桶内保存有排序后的数列,这样要求某个区间中不超过x的数的个数就可以这样求得。

对于完全包含在区间内的桶,用二分搜索法解决。

对于所在的桶不完全包含在区间内的元素,逐个检查。

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;

const int B=1000,MAX_N=100005,MAX_M=5000;
int N,M,A[MAX_N],I[MAX_M],J[MAX_M],K[MAX_M];
int nums[MAX_N];    //对A排序后的结果
vector<int> bucket[102];    //每一个桶排序后的结果

int main(){
    scanf("%d%d",&N,&M);
    for(int i=0;i<N;i++)
        scanf("%d",&A[i]);
    for(int i=0;i<M;i++)
        scanf("%d%d%d",&I[i],&J[i],&K[i]);
    for(int i=0;i<N;i++){
        bucket[i/B].push_back(A[i]);
        nums[i]=A[i];
    }
    sort(nums,nums+N);
    //虽然每B个一组剩下的部分所在的桶没有排序,但是不会产生影响
    for(int i=0;i<N/B;i++)
        sort(bucket[i].begin(),bucket[i].end());
    for(int i=0;i<M;i++){
        //求[l,r]区间里的第k个数
        int l=I[i]-1,r=J[i],k=K[i];
        int lb=-1,ub=N-1;
        while(ub-lb>1){
            int md=(lb+ub)/2;
            int x=nums[md];
            int tl=l,tr=r,c=0;
            //区间两段多出来的部分
            while(tl<tr&&tl%B!=0) if(A[tl++]<=x)c++;
            while(tl<tr&&tr%B!=0) if(A[--tr]<=x)c++;
            //对每一个桶进行计算
            while(tl<tr){
                int b=tl/B;
                c+=upper_bound(bucket[b].begin(),bucket[b].end(),x)-bucket[b].begin();
                tl+=B;
            }
            if(c>=k) ub=md;
            else lb=md;
        }
        printf("%d\n",nums[ub]);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值