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. Linux 使用常见问题

    1. 如何查看软件安装到什么位置 [Ubuntu] 今天安装了Lxc-docker,想看一下文件都安装到哪里了,首先找到这个包的ersion zhouh1@uhome:~$ dpkg -s lxc-d ...

  2. Redis学习笔记(五)散列进阶

    HEXISTS key_name key(检查键key是否存在) HKEYS key_name(获得散列的所有键) HVALS key_name(获得散列的所有值) HINCRBY key_name ...

  3. 详解nginx.conf文件配置项(包括负载均衡)

    http://www.cnblogs.com/hsapphire/archive/2010/04/08/1707109.html #运行用户 user  nobody nobody; #启动进程 wo ...

  4. Android(java)学习笔记185:多媒体之设置全屏的方法

    在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果.其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏. 其 ...

  5. c语言 错误记录

    1.预处理错误 #include <>   //系统内部的 #include ""   // 自定义的 遇到 not find------解决方案:gcc -I 跟查找 ...

  6. OpenCV2:总结篇 工具方法函数

    一.简介 OpenCV提供了一些工具方法函数来辅助完成图像运算 二.时间相关 1.getTickCount()和getTickFrequency() double tTime; tTime = (do ...

  7. P2257 YY的GCD (莫比乌斯反演)

    题意:求\[\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j) = prim]\] 题解:那就开始化式子吧!! \[f(d) = \sum_{i=1}^{n}\sum_{j=1 ...

  8. Linux配置ssh免密登录

    假定有3台机,用户名和IP分别是:C1  192.168.1.101C2  192.168.1.102C3  192.168.1.103 # 登入root用户su # 安装vimapt-get ins ...

  9. 清除oracle归档日志

    清除oracle归档日志 1. 连接oracle报如下错误 ORA-00257: archiver error. Connect internal only, until freed 产生原因:出现O ...

  10. 01matplotlib

    一. 简介 1.概念 matplotlib:最流行的Python底层绘图库,主要做数据可视化图表,名字取材于MATLAB,模仿MATLAB构建. 2.作用 1)能将数据进行可视化,更直观的呈现 2)使 ...