The hh-index of an author is the largest hh where he has at least hh papers with citations not less than hh.

Bobo has published nn papers with citations a1,a2,…,ana1,a2,…,an respectively.
One day, he raises qq questions. The ii-th question is described by two integers lili and riri, asking the hh-index of Bobo if has only published papers with citations ali,ali+1,…,ariali,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 nn and qq.
The second line contains nn integers a1,a2,…,ana1,a2,…,an.
The ii-th of last qq lines contains two integers lili and riri.
Output
For each question, print an integer which denotes the answer.

Constraint

  • 1≤n,q≤1051≤n,q≤105
  • 1≤ai≤n1≤ai≤n
  • 1≤li≤ri≤n1≤li≤ri≤n
  • The sum of nn does not exceed 250,000250,000.
  • The sum of qq does not exceed 250,000250,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
    题意:一组长度为n的序列,m次查询。每次查询给出两个数字l和r。询问的是找出最大的数字k,使得l到r之间的数字至少有k个数字大于等于k。
    思路很简单,二分答案,主席树上求区间大于等于k的数字个数。
    很多题解说的是求区间第k大,其实没有那么麻烦,直接离散化之后二分答案k,求大于等于k的个数就行。根据个数缩短区间。
    代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e5+100;
struct node{int l;int r;int sum;
}p[maxx*40];
int a[maxx],b[maxx],root[maxx];
int n,m,tot;
/*--------------主席树--------------*/
inline int build(int l,int r)
{int cur=++tot;p[cur].sum=0;if(l==r) return cur;int mid=l+r>>1;p[cur].l=build(l,mid);p[cur].r=build(mid+1,r);return cur;
}
inline int update(int rot,int pos,int l,int r)
{int cur=++tot;p[cur]=p[rot];p[cur].sum++;if(l==r) return cur;int mid=l+r>>1;if(pos<=mid) p[cur].l=update(p[rot].l,pos,l,mid);else p[cur].r=update(p[rot].r,pos,mid+1,r);return cur;
}
inline int query(int lrot,int rrot,int l,int r,int pos)
{if(l==r) return p[rrot].sum-p[lrot].sum;int mid=l+r>>1;int sum=0;if(pos<=mid) sum=query(p[lrot].l,p[rrot].l,l,mid,pos)+p[p[rrot].r].sum-p[p[lrot].r].sum;else sum=query(p[lrot].r,p[rrot].r,mid+1,r,pos);return sum;
}
int main()
{int l,r;while(~scanf("%d%d",&n,&m)){tot=0;for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];sort(b+1,b+1+n);int len=unique(b+1,b+1+n)-b-1;//离散化root[0]=build(1,len);for(int i=1;i<=n;i++) root[i]=update(root[i-1],lower_bound(b+1,b+1+len,a[i])-b,1,n);while(m--){scanf("%d%d",&l,&r);int L=1,R=n,mid,ans;while(L<=R){mid=L+R>>1;int zz=query(root[l-1],root[r],1,n,lower_bound(b+1,b+1+len,mid)-b);if(zz>=mid){ans=mid;L=mid+1;}else R=mid-1;}printf("%d\n",ans);}}return 0;
}

努力加油a啊,(o)/~

Just $h$-index HDU - 6278(主席树找区间大于等于k的个数)相关推荐

  1. Greedy Sequence(2019南京icpc网络预选赛)主席树求区间小于k的最大值

    题意:给出n个整数,构造s1,s2,s3-sn s1,s2,s3-sns1,s2,s3-sn,si sisi满足五个条件 1.s1[i]=i s1[i]=is1[i]=i 2.对于1<j< ...

  2. 【代码源 Div1 - 108】#464. 数数(主席树,区间比k小的数的个数)HDU4417

    problem solution 主席树查询区间比k小的数的个数 建树之后直接在目标区间的主席树内将 H 作为挡板递归计数. #include<bits/stdc++.h> using n ...

  3. [蓝桥杯][2016年第七届真题]压缩变换(主席树求区间不同数的个数)

    题目描述 小明最近在研究压缩算法. 他知道,压缩的时候如果能够使得数值很小,就能通过熵编码得到较高的压缩比. 然而,要使数值很小是一个挑战. 最近,小明需要压缩一些正整数的序列,这些序列的特点是,后面 ...

  4. 【HDU - 4348】To the moon(主席树,区间更新)

    题干: Background  To The Moon is a independent game released in November 2011, it is a role-playing ad ...

  5. hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)

    题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...

  6. hdu 2665(主席树查询区间k大值)

    先贴我自己写的代码做模板虽然跟原博主没什么两样.(一开始空间开的4*maxn,交到hdu上一直TLE很奇怪) #include<bits/stdc++.h> using namespace ...

  7. Super Mario HDU - 4417(主席树解决区间数字小于k的个数||线段树+离线)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

  8. Sequence II (HDU 5919)(主席树)

    Sequence II 题目大意是有mmm次询问,每次询问一段区间[l,r][l, r][l,r],从左到右,如果这个数是在这个区间第一次出现,则记录下其下标, 我们会得到一个新的数组,要求这个数组的 ...

  9. 2016中国大学生程序设计竞赛(长春)Sequence II HDU - 5919 主席树

    传送门 文章目录 题意: 思路: 题意: 给一个长度为nnn的序列,每次一个询问[l,r][l,r][l,r],求其中数第一次出现的位置的中位数. 思路: 先考虑一下如何求区间内不同数的个数. 因为要 ...

最新文章

  1. Latex 添加多张图片技巧说明
  2. 洛谷P4513 小白逛公园
  3. 【数据结构总结】第二章:线性表
  4. ArcGIS学习路线
  5. 移动硬盘备份linux系统盘,将Ubuntu Linux系统放到你的移动硬盘
  6. 拓端tecdat|R语言对股票风险“溃疡指数”( Ulcer Index)曲面图可视化
  7. wⅰndows办公软件2003,办公软件2003
  8. Oracle日志报03113,OracleORA-03113 ORA-600 [4193]故障处理
  9. 计算机组成原理基本概念,《计算机组成原理》教学中几个基本概念的分析
  10. 海康网络摄像机与电脑交互,有网络和无网络两种方式读取URL视频流,以及无网络情况下配置IP地址
  11. QQ自定义表情巧收藏换了电脑照样用(转)
  12. 3.虚幻4-游戏开始界面的制作
  13. [情感] 历练熟女给老实木讷男孩的恋爱建议
  14. 解决Chrome中打不开Google搜索结果链接
  15. 安卓篇-我自己的第一个安卓程序(附源码链接)
  16. 三阶魔方背后的神奇数学
  17. 如何在html中播放本地视频文件【兼容ie、火狐、谷歌、360浏览器等】
  18. 对 ArabicRSS APK 应用木马样本的分析
  19. Pagination 分页实现跳转首页和尾页
  20. 电脑设置了从睡眠中唤醒需要密码却没生效(已解决)

热门文章

  1. leetcode机器人运动范围Java_【LeetCode】面试题13. 机器人的运动范围
  2. p4: php5ts.dll p5: 5.6.14.0,服务器httpd.exe 应用程序错误
  3. 功能测试常用6种方法_16种常用的数据分析方法聚类分析
  4. 用python画爱心再加一行文字_如何理解python一行代码实现一个爱心字符画?
  5. jdbc 生成建表语句_JDBC数据库连接怎么操作?
  6. python第三方包是什么意思_安装Python和第三方包的方法
  7. python牛顿迭代法求平方根_牛顿迭代法计算平方根(Java,Python实现)
  8. QGraphicsWidget收不到鼠标、键盘消息解决
  9. Android开发之开发工具之Android Studio出现全屏的解决办法
  10. Django模板渲染——(二)