hdu 6278 Just h-index —— 主席树

Problem Description
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

Source
CCPC2018-湖南全国邀请赛-重现赛(感谢湘潭大学)

裸的主席树。。。只需要在查询的时候和查询有多少小于等于k的个数的题目反一下就好了

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int sum[maxn*40],ls[maxn*40],rs[maxn*40],rt[maxn],cnt;
void update(int l,int r,int root,int last,int val)
{
    ls[root]=ls[last];
    rs[root]=rs[last];
    sum[root]=sum[last]+1;
    if(l==r)
        return ;
    int mid=l+r>>1;
    if(mid>=val)
        update(l,mid,ls[root]=++cnt,ls[last],val);
    else
        update(mid+1,r,rs[root]=++cnt,rs[last],val);

}
int query(int l,int r,int root,int last,int val)
{
    if(l==r)
        return sum[root]-sum[last];
    int mid=r+l>>1;
    int ans=0;
    int all=sum[rs[root]]-sum[rs[last]];
    if(mid>=val)
        ans+=all+query(l,mid,ls[root],ls[last],val);
    else
        ans+=query(mid+1,r,rs[root],rs[last],val);
    return ans;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(sum,0,sizeof(sum));
        cnt=0;
        int val;
        for(int i=1;i<=n;i++)
            scanf("%d",&val),update(1,n,rt[i]=++cnt,rt[i-1],val);
        int l,r;
        while(m--)
        {
            scanf("%d%d",&l,&r);
            int low=1,high=n;
            int ans;
            while(high>=low)
            {
                int mid=low+high>>1;
                if(query(1,n,rt[r],rt[l-1],mid)>=mid)
                    ans=mid,low=mid+1;
                else
                    high=mid-1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值