HDU 6278 主席树+二分

http://acm.hdu.edu.cn/showproblem.php?pid=6278

The h-index of an author is the largest h where he has at least h papers with citations not less than h.

Bobo has published n papers with citations a1,a2,…,an respectively.
One day, he raises q questions. The i-th question is described by two integers li and ri, asking the h-index of Bobo if has *only* published papers with citations ali,ali+1,…,ari

.

Input

The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains two integers n

and q.
The second line contains n integers a1,a2,…,an.
The i-th of last q lines contains two integers li and ri

.

Output

For each question, print an integer which denotes the answer.

## Constraint

* 1≤n,q≤105

 

* 1≤ai≤n
* 1≤li≤ri≤n
* The sum of n does not exceed 250,000.
* The sum of q does not exceed 250,000

.

Sample Input

5 3
1 5 3 2 1
1 3
2 4
1 5
5 1
1 2 3 4 5
1 5

Sample Output

2
2
2
3

题目大意:给出n个数,对于每个询问l、r,输出最大的x,使得在[l,r]这个区间内满足,有x个数的值大于等于x。

思路:主席树,然后二分找x。对于区间[l,r],我们找到这个区间内第mid大的数v,那么>=v的数还有r-mid+1个,有以下三种情况:(1)v=r-mid+1,此时v即为最优解;(2)v>r-mid+1,那么贡献为min(v,r-mid+1),缩小上界;(3)v<r-mid+1的情况和(2)差不多。因为本题给定1<=ai<=n,所以离不离散化其实都可以。实测本题中离散化的效率要高一点。

离散化做法:

#include<bits/stdc++.h>
#define maxn 2000010
using namespace std;

struct node
{
    int ls,rs,sum;
}tree[maxn];

int n,nn,m,tot;
int rt[maxn],a[maxn],b[maxn];

void insert(int i,int &x,int l,int r)
{
    tree[++tot]=tree[x];
    x=tot;
    ++tree[x].sum;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    if(i<=mid)
        insert(i,tree[x].ls,l,mid);
    else
        insert(i,tree[x].rs,mid+1,r);
}

int query(int i,int j,int k,int l,int r)
{
    if(l==r)
        return l;
    int dis=tree[tree[j].ls].sum-tree[tree[i].ls].sum;
    int mid=(l+r)>>1;
    if(k<=dis)
        return query(tree[i].ls,tree[j].ls,k,l,mid);
    else
        return query(tree[i].rs,tree[j].rs,k-dis,mid+1,r);
}

int main()
{
    rt[0]=0;
    tree[0].ls=tree[0].rs=tree[0].sum=0;
    while(~scanf("%d %d",&n,&m))
    {
        tot=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        sort(b+1,b+1+n);
        nn=unique(b+1,b+1+n)-b-1;
        for(int i=1;i<=n;i++)
            a[i]=lower_bound(b+1,b+1+n,a[i])-b;
        for(int i=1;i<=n;i++)
        {
            rt[i]=rt[i-1];
            insert(a[i],rt[i],1,nn);
        }
        int x,y,l,r,mid,temp,len,ans;
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&x,&y);
            l=1,len=r=y-x+1;
            ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                temp=b[query(rt[x-1],rt[y],mid,1,nn)];
                ans=max(ans,min(temp,len-mid+1));
                if(temp==len-mid+1)
                    break;
                else if(temp>len-mid+1)
                    r=mid-1;
                else
                    l=mid+1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

不离散化做法:

#include<bits/stdc++.h>
#define maxn 2000010
using namespace std;

struct node
{
    int ls,rs,sum;
}tree[maxn];

int n,nn,m,tot;
int rt[maxn],a[maxn],b[maxn];

void insert(int i,int &x,int l,int r)
{
    tree[++tot]=tree[x];
    x=tot;
    ++tree[x].sum;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    if(i<=mid)
        insert(i,tree[x].ls,l,mid);
    else
        insert(i,tree[x].rs,mid+1,r);
}

int query(int i,int j,int k,int l,int r)
{
    if(l==r)
        return l;
    int dis=tree[tree[j].ls].sum-tree[tree[i].ls].sum;
    int mid=(l+r)>>1;
    if(k<=dis)
        return query(tree[i].ls,tree[j].ls,k,l,mid);
    else
        return query(tree[i].rs,tree[j].rs,k-dis,mid+1,r);
}

int main()
{
    rt[0]=0;
    tree[0].ls=tree[0].rs=tree[0].sum=0;
    while(~scanf("%d %d",&n,&m))
    {
        tot=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
        {
            rt[i]=rt[i-1];
            insert(a[i],rt[i],1,n);
        }
        int x,y,l,r,mid,temp,len,ans;
        for(int i=0;i<m;i++)
        {
            scanf("%d %d",&x,&y);
            mid=(x+y)>>1;
            l=1,len=r=y-x+1;
            ans=0;
            while(l<=r)
            {
                mid=(l+r)>>1;
                temp=query(rt[x-1],rt[y],mid,1,n);
                ans=max(ans,min(temp,len-mid+1));
                if(temp==len-mid+1)
                    break;
                else if(temp>len-mid+1)
                    r=mid-1;
                else
                    l=mid+1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值