链接:https://ac.nowcoder.com/acm/problem/52893
来源:牛客网
题目描述
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,…,ana_1, a_2, \dots, a_na1,a2,…,an respectively.
One day, he raises q questions. The i-th question is described by two integers lil_ili and rir_iri, asking the h-index of Bobo if has *only* published papers with citations ali,ali+1,…,aria_{l_i}, a_{l_i + 1}, \dots, a_{r_i}ali,ali+1,…,ari.
输入描述:
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,…,ana_1, a_2, \dots, a_na1,a2,…,an. The i-th of last q lines contains two integers lil_ili and rir_iri.
输出描述:
For each question, print an integer which denotes the answer.
示例1
输入
复制5 3 1 5 3 2 1 1 3 2 4 1 5 5 1 1 2 3 4 5 1 5
5 3 1 5 3 2 1 1 3 2 4 1 5 5 1 1 2 3 4 5 1 5
输出
复制2 2 2 3
2 2 2 3
备注:
* 1≤n,q≤1051 \leq n, q \leq 10^51≤n,q≤105 * 1≤ai≤n1 \leq a_i \leq n1≤ai≤n * 1≤li≤ri≤n1 \leq l_i \leq r_i \leq n1≤li≤ri≤n * The sum of n does not exceed 250,000. * The sum of q does not exceed 250,000.
题意:对于每组测试,求一个最大的数h,使n个数的[l,r]区间内>=h的数的个数>=h
思路:h使n个数的[l,r]区间内>=h的数的个数>=h并且h要取最大的 ,显然,等价于求[l,r]第h大的数;
而主席树可以求区间第k小,做一下转换,[l,r]第h大的数即为第(r-l+1)-h+1小的数,然而有q次询问,因此限制了时间,答案和n个数的[l,r]区间内>=h的数的个数有关,因此答案的可能的所有取值为1 ~ 区间长度(r-l+1) 的范围,在这个范围内主席树上二分答案,就是每次将二分得到的答案带入主席树查询,看查询出来的数是否>=当前答案即可。
二分答案的时间复杂度为:log(r-l+1),二分时每次用主席树的查询时间为logn,一共q次询问,所以总的时间复杂度:
O(log(r-l+1)*logn*q)
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=1e5+5;
int n,m,a[maxn];
vector<int> v;
typedef struct Node
{
int l,r,sum;
}no;
no hjt[maxn*40];
int cnt,root[maxn];
inline int getid(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
void init()
{
memset(root,0,sizeof root);
v.clear();
cnt=0;
//for(int i=1;i<=maxn*40;i++) hjt[i].l=hjt[i].r=hjt[i].sum=0;
}
void insert(int l,int r,int pre,int &now,int p)
{
hjt[++cnt]=hjt[pre];
now=cnt;
hjt[now].sum++;
if(l==r) return;
int m=(l+r)>>1;
if(p<=m) insert(l,m,hjt[pre].l,hjt[now].l,p);
else insert(m+1,r,hjt[pre].r,hjt[now].r,p);
}
int query(int l,int r,int L,int R,int k)//找区间第k小的数
{
if(l==r) return l;
int m=(l+r)>>1;
int t=hjt[hjt[R].l].sum-hjt[hjt[L].l].sum;
if(k<=t) return query(l,m,hjt[L].l,hjt[R].l,k);
else return query(m+1,r,hjt[L].r,hjt[R].r,k-t);
}
int main()
{
while(~scanf("%d%d",&n,&m)){
init();
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(std::unique(v.begin(),v.end()),v.end());
for(int i=1;i<=n;i++)
{
insert(1,n,root[i-1],root[i],getid(a[i]));
}
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
int len=r-l+1;
int L=1,R=len,res=0;//答案一定在区间长度范围内
while(L<=R){
int mid=L+R>>1;//二分答案
int rk=len-mid+1;//第mid大就是第R-mid+1小
int id=query(1,n,root[l-1],root[r],rk)-1;
if(v[id]>=mid){//如果区间中第mid大的数比当前答案还要大,即:>=mid的数的个数>=mid个,因为题目要求一个res,使[l,r]中>=res的数的个数>=res个,说明当前mid满足条件,可以再往大找
res=mid;
L=mid+1;
}
else R=mid-1;
}
printf("%d\n",res);
}
}
return 0;
}