sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

Boring Counting

Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

    In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each query, please tell us among [L, R], how many Pi is not less than A and not greater than B( L<= i <= R). In other words, your task is to count the number of Pi (L <= i <= R,  A <= Pi <= B).

输入

     In the first line there is an integer T (1 < T <= 50), indicates the number of test cases. 
     For each case, the first line contains two numbers N and M (1 <= N, M <= 50000), the size of sequence P, the number of queries. The second line contains N numbers Pi(1 <= Pi <= 10^9), the number sequence P. Then there are M lines, each line contains four number L, R, A, B(1 <= L, R <= n, 1 <= A, B <= 10^9)

输出

    For each case, at first output a line ‘Case #c:’, c is the case number start from 1. Then for each query output a line contains the answer.

示例输入

1
13 5
6 9 5 2 3 6 8 7 3 2 5 1 4
1 13 1 10
1 13 3 6
3 6 3 6
2 8 2 8
1 9 1 9

示例输出

Case #1:
13
7
3
6
9

提示

 

来源

 2013年山东省第四届ACM大学生程序设计竞赛

示例程序


大体题意:
给你n个数,和一个指定区间,让你求在这个区间内   数字满足  大于等于A  小于等于B的个数!

思路:
这个题写的很痛苦。依旧借鉴了学长们的博客~
先说下整体思路:
建好划分树后,在这个区间上进行二分查找,二分查找的是第K大的级数!
比如说  区间是[2,5],这之间总共有四个数字。
所以我要二分查找1,2,3,4等级的数字!

感觉最别扭的就是二分查找了!
先找出第一个小于等于B的数字位置!mid1
在找出第一个大于等于A的数字位置!mid2
输出mid1-mid2+1就是答案!

需要注意的是二分查找返回的值可以为-1.表示未找到
这样直接判断存在不存在在输出,否则会乱掉!

注:自己的代码已改正!!!!
#include<stdio.h>
#include<algorithm>
using namespace std;
const int M = 100000 + 5;
int tree[20][M],sorted[M];
int toLeft[20][M];
int nl,nr,A,B;
int n,cnt=0,m;
void build(int level,int left,int right){
    if(left==right)return ;
    int mid=(left+right)>>1;
    int i;
    int suppose;
    suppose=mid-left+1;
    for(i=left;i<=right;i++){
        if(tree[level][i]<sorted[mid]){
            suppose--;
        }
    }
    int lpos=left,rpos=mid+1;
    for(i=left;i<=right;i++){
        if(i==left){
            toLeft[level][i]=0;
        }else{
            toLeft[level][i]=toLeft[level][i-1];
        }
        if(tree[level][i]<sorted[mid]){
            toLeft[level][i]++;
            tree[level+1][lpos++]=tree[level][i];
        }else if(tree[level][i]>sorted[mid]){
            tree[level+1][rpos++]=tree[level][i];
        }else{
            if(suppose!=0){
                suppose--;
                toLeft[level][i]++;
                tree[level+1][lpos++]=tree[level][i];
            }else{
                tree[level+1][rpos++]=tree[level][i];
            }
        }
    }
    build(level+1,left,mid);
    build(level+1,mid+1,right);
}
int query(int level,int left,int right,int qleft,int qright,int k){
    if( qleft==qright)
        return tree[level][qleft];
    int s;
    int ss;
    int mid=(left+right)>>1;
    if(left==qleft){
        s=0;
        ss=toLeft[level][qright];
    }else{
        s=toLeft[level][qleft-1];
        ss=toLeft[level][qright]-s;
    }
    int newl,newr;
    if(k<=ss){
        newl=left+s;
        newr=left+s+ss-1;
        return query(level+1,left,mid,newl,newr,k);
    }else{
        newl=mid-left+1+qleft-s;
        newr=mid-left+1+qright-s-ss;
        return query(level+1,mid+1,right,newl, newr,k - ss);
    }
}
int findup(int l,int r){
    int key=-1;
    while(l <= r){
        int mid = l+(r-l)/2;
        int ans = query(0,1,n,nl,nr,mid);
        if (ans <= B)l = mid+1,key = mid;
        else r = mid-1;
    }
    return key;
}
int findlow(int l,int r){
    int key = -1;
    while(l <= r){
        int mid = l + (r-l)/2;
        int ans = query(0,1,n,nl,nr,mid);
        if (ans >= A)r = mid-1,key = mid;
        else l = mid+1;
    }
    return key;
}

int main(){

    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for (int i = 1; i <= n; ++i){
            scanf("%d",&tree[0][i]);
            sorted[i] = tree[0][i];
        }
        sort(sorted+1,sorted+1+n);
        build(0,1,n);
        printf("Case #%d:\n",++cnt);
        while(m--){
            scanf("%d%d%d%d",&nl,&nr,&A,&B);
            int ll = findlow(1,nr-nl+1);
            int rr = findup(1,nr-nl+1);
            if (ll < 0 || rr < 0 || A > B || ll > rr)printf("%d\n",0);
            else printf("%d\n",rr-ll+1);
        }
    }
    return 0;
}




你可能感兴趣的:(sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分))