Boring Counting(主席树+二分)

题目描述

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
思路

我们知道主席树可以求区间第k小的值,我可以二分k的值,然后判断利用第k小来判断出A,B分别是第几小。然后答案就是两者的差值。

代码
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<stack>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f
#define P pair<int,int>
#define ls p<<1
#define rs p<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps=1e-5;
const double pai=acos(-1.0);
const int N=5e4+10;
const int maxn=1e6+10;
//const int mod=1000000007;
int root[N],a[N],b[N],cnt;
int sum[N<<5],lc[N<<5],rc[N<<5];
void build(int p,int l,int r)
{
    p=++cnt,sum[p]=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(lc[p],l,mid);
    build(rc[p],mid+1,r);
}
int update(int p,int l,int r,int pos)
{
    int rt=++cnt;
    lc[rt]=lc[p],rc[rt]=rc[p],sum[rt]=sum[p]+1;
    if(l<r)
    {
        int mid=(l+r)>>1;
        if(pos<=mid) lc[rt]=update(lc[p],l,mid,pos);
        else rc[rt]=update(rc[p],mid+1,r,pos);
    }
    return rt;
}
int query(int u,int v,int l,int r,int k)
{
    if(l==r) return l;
    int mid=(l+r)>>1,x=sum[lc[v]]-sum[lc[u]];
    if(k<=x) return query(lc[u],lc[v],l,mid,k);
    else return query(rc[u],rc[v],mid+1,r,k-x);
}
int main( )
{
    int t,tot=0;
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;//一定要记得清零,要不一直RE/(ㄒoㄒ)/~~
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
        sort(b+1,b+n+1);
        int len=unique(b+1,b+n+1)-b-1;
        build(root[0],1,len);
        for(int i=1;i<=n;i++)
        {
            int pos=lower_bound(b+1,b+len+1,a[i])-b;
            root[i]=update(root[i-1],1,len,pos);
        }
        printf("Case #%d:\n",++tot);
        while(m--)
        {
            int x,y,A,B,ans1,ans2;
            scanf("%d%d%d%d",&x,&y,&A,&B);
            int l=1,r=y-x+1,mid;
            while(l<=r)
            {
                mid=(l+r)>>1;
                int pos=query(root[x-1],root[y],1,len,mid);
                if(b[pos]>=A) 
                {
                    r=mid-1;
                }
                else l=mid+1;
            }
            ans1=l;
            l=1,r=y-x+1;
            while(l<=r)
            { 
                mid=(l+r)>>1;
                int pos=query(root[x-1],root[y],1,len,mid);
                if(b[pos]<=B)
                {
                    l=mid+1;
                }
                else r=mid-1;
            }       
            ans2=l;
            printf("%d\n",ans2-ans1);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值