UPC:2224 Boring Counting(二分+划分树)

题意:给一串数字,每次查询区间内大于等于A小于等于B的数字个数。

思路:二分+划分树。划分树可以求区间内第k小的数字,配合二分可以求到某个数字在区间内是第几小数字。

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=50005;
typedef long long LL;
struct DivideTree
{
    int arr[maxn],sorted[maxn],dat[20][maxn];
    int toleft[20][maxn];
    void build(int c,int L,int R)
    {
        int mid=(L+R)/2,lsame=mid+1-L,lp=L,rp=mid+1;
        for(int i=L; i<mid; ++i)
            if(sorted[i]<sorted[mid]) lsame--;
        for(int i=L; i<=R; ++i)
        {
            if(i==L)
            {
                toleft[c][i]=0;
            }
            else
            {
                toleft[c][i]=toleft[c][i-1];
            }
            if(dat[c][i]<sorted[mid])
            {
                dat[c+1][lp++]=dat[c][i];
                toleft[c][i]++;
            }
            else if(dat[c][i]>sorted[mid])
                dat[c+1][rp++]=dat[c][i];
            else
            {
                if(lsame)
                {
                    lsame--;
                    toleft[c][i]++;
                    dat[c+1][lp++]=sorted[mid];
                }
                else
                {
                    dat[c+1][rp++]=sorted[mid];
                }
            }
        }
        if(L==R) return ;
        build(c+1,L,mid);
        build(c+1,mid+1,R);
    }
    int query(int c,int L,int R,int ql,int qr,int k)
    {
        if(L==R)
            return dat[c][L];
        int mid=(L+R)/2;
        int la,lb,ra,rb;
        if(L==ql)
            la=0;
        else
            la=toleft[c][ql-1];
        lb=toleft[c][qr];
        ra=ql-L-la;
        rb=qr+1-L-lb;
        int s=lb-la;
        if(k<=s)
            return query(c+1,L,mid,L+la,L+lb-1,k);
        else
            return query(c+1,mid+1,R,mid+1+ra,mid+rb,k-s);
    }
};
DivideTree tree;
int n,q;
int binSearch1(const int &ql,const int &qr,const int &target)
{
    int low=1,high=qr-ql+1;
    int mid=low+(high-low)/2;
    while(low<high)
    {
        if(tree.query(0,1,n,ql,qr,mid)>=target) high=mid;
        else low=mid+1;
        mid=low+(high-low)/2;
    }
    if(tree.query(0,1,n,ql,qr,mid)>=target)
        return mid;
    else
        return -1;
}
int binSearch2(const int &ql,const int &qr,const int &target)
{
    int low=1,high=qr-ql+1;
    int mid=low+(high-low+1)/2;
    while(low<high)
    {
        if(tree.query(0,1,n,ql,qr,mid)<=target) low=mid;
        else high=mid-1;
        mid=low+(high-low+1)/2;
    }
    if(tree.query(0,1,n,ql,qr,mid)<=target)
        return mid;
    else
        return -1;
}
int main()
{
    int T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&q);
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&tree.arr[i]);
            tree.sorted[i]=tree.dat[0][i]=tree.arr[i];
        }
        sort(tree.sorted+1,tree.sorted+n+1);
        tree.build(0,1,n);
        printf("Case #%d:\n",++kase);
        while(q--)
        {
            int l,r,a,b;
            scanf("%d%d%d%d",&l,&r,&a,&b);
            int x,y;
            x=binSearch1(l,r,a);
            y=binSearch2(l,r,b);
            if(x==-1||y==-1) puts("0");
            else printf("%d\n",y-x+1);
        }
    }
    return 0;
}


你可能感兴趣的:(数据结构,二分法,划分树)