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. ASP.NET Web API FilterAttribute假想

    偶然的测试发现API FilterAttribute没用引用只会初始化一次 比如: 如果是 Global Action Filter, 则全局只会初始化一次 针对于不同的Controller级别的Ac ...

  2. Spring中@Value的使用

  3. (转)Spring如何装配各种集合类型的属性

    http://blog.csdn.net/yerenyuan_pku/article/details/52858499 在前面我们已经会注入基本类型对象和其他bean,现在我们就来学习如何注入各种集合 ...

  4. pagehelper 分页

    分页jar包: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pa ...

  5. easyui实现增删改查

    陈旧的开发模式 美工(ui工程师:出一个项目模型) java工程师:将原有的html转成jsp,动态展示数据 缺点: 客户需要调节前端的展示效果 解决:由美工去重新排版,重新选色. 前后端分离: 前端 ...

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

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

  7. git 超时 时长 设置?

    [Pipeline] { (Checkout) [Pipeline] checkout > git.exe rev-parse --is-inside-work-tree # timeout=1 ...

  8. Java后端技术微信交流群!工作、学习、技术、资源等!期待你的加入!

    <Java后端技术>专注Java相关技术:SSM.Spring全家桶.微服务.MySQL.MyCat.集群.分布式.中间件.Linux.网络.多线程,偶尔讲点运维Jenkins.Nexus ...

  9. vue项目中设置跨域

    config->index.js 'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io/ ...

  10. python 学习总结3

    Python蟒蛇绘制 一.实现程序如下 import turtle turtle.setup (650, 350, 200, 200)#turtle的绘图窗体turtle.setup(width, h ...