【poj2104】K-th Number 分块

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

总之就是多次询问给定区间内第K大值…

主席树什么的不会,用分块水水算了…分块+二分即可。

一些细节:
1.二分的返回值一定要确定好,我就是这里WA…
2.手动读入注意负数,不要被坑

第一次打分块,代码难看得很,懂思想就好QAQ

真的不建议抄而只是看一下的代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

int block=0,b_num=0;
int num[200010];
int nums[200010];

struct fenkuai{
    int num[2010],len,l,r;
    fenkuai(){memset(num,0,sizeof(num));len=l=r=0;}
}fk[2010];

inline void build(int n)
{
    b_num=n/((int)( (int)(sqrt(n*log2(n)))+1));
    if(n%b_num) block=n/b_num+1;
    else block=n/b_num;
    for(int i=1;i<=n;i++)
    {
        if(i%block==1) fk[i/block+1].l=i;
        if(i%block==0) fk[i/block].r=i;

        if(i%block) fk[i/block+1].num[++fk[i/block+1].len]=num[i];
        if(i%block==0) fk[i/block].num[++fk[i/block].len]=num[i];
    }
    fk[b_num].r=n;
    for(int i=1;i<=b_num;i++)
    {   
        sort(fk[i].num+1,fk[i].num+1+fk[i].len);
/* printf("fk:%d %d %d\n",fk[i].l,fk[i].r,fk[i].len); for(int j=1;j<=fk[i].len;j++) { printf("%d ",fk[i].num[j]); } puts("");*/
    }
}

inline int find(int i,int d)
{
    int l=0;
    int r=fk[i].len+1,ans=0;
    while(r-l>0)
    {
        int mid=(l+r)/2;
        if(fk[i].num[mid]<=d) ans=mid,l=mid+1;
        else r=mid;
    }
    return ans;
}
inline bool check(int l,int r,int k,int d)
{
    int ans=0;
    for(int i=1;i<=b_num;i++)
    {
        if(l<=fk[i].l&&fk[i].r<=r) 
        {
            ans+=find(i,d);
//          printf("1 ans: %d\n",ans);
        }
        else if(fk[i].l<=l&&r<=fk[i].r)
        {
            for(int j=l;j<=r;j++)
            {
                if(num[j]<=d) ans++;
            }
//          printf("2 ans: %d\n",ans);
        }       
        else if(l>=fk[i].l&&l<=fk[i].r)
        {
            for(int j=l;j<=fk[i].r;j++)
            {
                if(num[j]<=d) ans++;
            }
//          printf("3 ans: %d\n",ans);
        }
        else if(r>=fk[i].l&&r<=fk[i].r)
        {
            for(int j=fk[i].l;j<=r;j++)
            {
                if(num[j]<=d) ans++;
            }           
//          printf("4 ans: %d\n",ans);
        }
    }
//  printf("ans:%d d:%d\n",ans,d);
    return ans<k;
}
int n;
inline int div(int ll,int rr,int k)
{
    int l=0,r=n;
    while(r-l>1)
    {
        int mid=(l+r)/2;
        if(check(ll,rr,k,nums[mid])) l=mid;
        else r=mid;
    }
    return nums[r];
}

inline void scan(int &n)
{
    n=0;
    char a=getchar();
    bool flag=0;
    while(a<'0'||a>'9') 
    {
        if(a=='-') flag=1;
        a=getchar();
    }
    while(a>='0'&&a<='9')
    {
        n*=10;
        n+=a-'0';
        a=getchar();
    }
    if(flag) n*=-1;
}

int main()
{
//  freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);  
    int m;
    scan(n);
    scan(m);
    for(int i=1;i<=n;i++) 
    {
        scan(num[i]);
        nums[i]=num[i];
    }
    sort(nums+1,nums+1+n);
    build(n);
    while(m--)
    {
        int l,r,k;
        scan(l);
        scan(r);
        scan(k);
        printf("%d\n",div(l,r,k));
    }
    return 0;
}


你可能感兴趣的:(二分,分块)