Just h-index

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 438    Accepted Submission(s): 203

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

补:主席树  2018湘潭C http://acm.hdu.edu.cn/showproblem.php?pid=6278

代码

#include<bits/stdc++.h>
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1E9+; const int maxn = ;
const int LOG = ; struct node
{
int l,r;
int sum;
}Node[maxn*LOG];
int root[maxn],node_cnt;
int numbers[maxn],num_cnt;
int a[maxn]; void build(int l,int r,int &rt)
{
rt=++node_cnt;
Node[rt].l=Node[rt].r=Node[rt].sum=;
if(l==r)
return ;
int m=(l+r)>>;
build(l,m,Node[rt].l);
build(m+,r,Node[rt].r);
}
void update(int v,int l,int r,int &rt,int pre)
{
rt=++node_cnt;
Node[rt]=Node[pre];
++Node[rt].sum;
if(l==r)
return ;
int m=(l+r)>>;
if(v<=m)
update(v,l,m,Node[rt].l,Node[pre].l);
else
update(v,m+,r,Node[rt].r,Node[pre].r);
}
int query(int k,int l,int r,int r1,int r2)
{
if(l==r)
return r;
int lnum=Node[Node[r2].l].sum-Node[Node[r1].l].sum;
int m=(l+r)>>;
if(k<=lnum)
return query(k,l,m,Node[r1].l,Node[r2].l);
else
return query(k-lnum,m+,r,Node[r1].r,Node[r2].r);
} int main()
{
// ios::sync_with_stdio(false);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
// cin>>a[i];
numbers[i]=a[i];
}
sort(numbers+,numbers+n+);
num_cnt=unique(numbers+,numbers+n+)-numbers-;
node_cnt=;
root[]=;
build(,num_cnt,root[]);
for(int i=;i<=n;i++)
{
int pos = lower_bound(numbers+,numbers+num_cnt+,a[i])-numbers;
update(pos,,num_cnt,root[i],root[i-]);
}
while(m--)
{
int L,R,K;
cin>>L>>R;
int l=,r=R-L+;
int ans=;
//int q=query(K,1,num_cnt,root[L-1],root[R]);
//cout<<numbers[q]<<endl;
while(l<=r)
{
int mid=(l+r)/;
int q=query(R-L+-mid,,num_cnt,root[L-],root[R]);
//cout<<"mid="<<mid<<" val="<<numbers[q]<<endl;
if(numbers[q]>=mid)
{
ans=max(mid,ans);
l=mid+;
}
else
r=mid-; }
printf("%d\n",ans);
}
}
return ;
}

HDU 6278 主席树(区间第k大)+二分的更多相关文章

  1. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

  2. 主席树区间第K大

    主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...

  3. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  4. HDU 4417 (划分树+区间小于k统计)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...

  5. HDU 4348 主席树区间更新

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. HDU 3078 (LCA+树链第K大)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题目大意:定点修改.查询树中任意一条树链上,第K大值. 解题思路: 先用离线Tarjan把每个 ...

  7. zoj2112 主席树动态第k大 (主席树&&树状数组)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  8. 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解

    题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...

  9. zoj2112 主席树动态第k大 ( 参考资料链接)

    参考链接: http://blog.csdn.net/acm_cxlove/article/details/8565309 http://www.cnblogs.com/Rlemon/archive/ ...

随机推荐

  1. Android学习笔记(十七) BroadcastReceiver

    1.接收广播 创建一个类,继承BroadcastReceiver,复写其中的onReceive()方法 在AndroidManifest.xml文件中注册该BroadcastReceiver 设置完成 ...

  2. android开发工具eclipse的安装与配置

    l开发主要应用Eclipse 3.7版本. l辅助工具为jdk.Androidsdk Android环境搭建   –1.1.JDK安装 –1.2.Eclipse安装 –1.3.Android SDK安 ...

  3. docker新手入门(基本命令以及介绍)

    Docker 的核心内容 镜像 (Image) 容器 (Container) 仓库 (Repository) Registry 用来保存用户构建的镜像 docker的开始使用: 1. docker  ...

  4. 关于MessageBox返回值

    风格设置MB_OK. 此时无论点击确定还是点击X,都返回IDOK.风格设置MB_OKCANCEL,点击确认返回IDOK,点击取消和X都返回IDCANCEL.风格设置MB_YESNO,点击是返回IDYE ...

  5. python数据类型常用内置函数之字符串

    1.strip, lstrip, rstrip x = ' jiahuifeng ' print(x.strip(' ')) print(x.lstrip(' ')) print(x.rstrip(' ...

  6. bzoj3272 Zgg吃东西

    题目描述: bz 题解: 线段树模拟费用流. 想法和种树有点类似. 每次取区间内权值和最大的一段,然后整体乘$-1$,代表再次选中时会去掉之前的影响. 线段树维护一堆东西…… 小白逛公园双倍快乐.乘$ ...

  7. FWT板子

    板子: #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> u ...

  8. Python面向对象之异常

    异常的概念 程序在运行时,如果python解释器遇见一个错误,就会停止程序的运行,并且提示一些错误信息,这就是异常: 程序遇见错误停止执行并且提示错误信息,这个动作我们称之为抛出(raise)异常: ...

  9. uwsgs loading shared libraries: libicui18n.so.58 异常处理

    背景 想使用 ningx + uwsgi + flask 搭建 python 应用环境 Python使用的是anaconda3(pyhton 3.6) 依赖包安装完毕,但是执行 uwsgi 的时候出现 ...

  10. 嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av15123607/?from=search&seid=10211084839195730432#page=25 中的42-45讲 {字典}

    #coding=gbk#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av15123607/?from=search&seid=1021108 ...