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. VCS filelist 文件格式

    VCS在运行仿真一般都会加仿真参数 –f filelist,filelist 是包含其他的仿真参数和整个工程的文件列表.具体格式如下: //file list format, just for exa ...

  2. FPGA编程技巧系列之按键边沿检测

    抖动的产生: 通常的按键所用开关为机械弹性开关,当机械触点断开.闭合时,由于机械触点的弹性作用,一个按键开关在闭合时不会马上稳定地接通,在断开时也不会一下子断开.因而在闭合及断开的瞬间均伴随有一连串的 ...

  3. leetcode_268.missing number

    给定一个数组nums,其中包含0--n中的n个数,找到数组中没有出现的那个数. 解法一:cyclic swapping algorithm class Solution { public: int m ...

  4. Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simplified)

    http://www.msdn.hk/html/2014/1404.html Windows Server 2012 R2 with Update (x64) - DVD (Chinese-Simpl ...

  5. postman使用--发送请求

    概述 上节讲了下接口的基础,从现在来学习怎么测接口.当然,测试接口有很多的工具,比如postman,jmeter等等,或者用代码测试,如果是做接口自动化我当然会选python,如果是调试接口,我特别喜 ...

  6. linux 安装nginx 集成emq

    1:下载nginx-1.12.2.tar.gz http://nginx.org/en/download.html 2:解压 tar -zxvf  nginx-1.12.2.tar.gz 3:进行co ...

  7. 清除SQL Server 2008记住的数据库地址、登录名和密码

    在服务器上登录过数据库信息,并且选择了记住了密码,由于服务器数据库很多人在使用,有必要删除信息 定位到fileC:\Users\%username%\AppData\Roaming\Microsoft ...

  8. Android UI: LinearLayout中layout_weight 属性的使用规则

    首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...

  9. 122. Best Time to Buy and Sell Stock II@python

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. 用Multisim实现彩灯循环控制器

    2019/06/06 !转载请注明出处 1.设计任务目的与要求 1.1 展示器件 10路彩灯分别用10个发光二极管L0.L1…..L9模拟,发光二极管L0.L1…..L9从左到右排列. 1.2 要求显 ...