题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)

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

题意:

一个作者它的“h-index”指的是:有一个最大的正整数h,且满足他有至少h篇论文的引用量不低于h。

现在给出n篇论文的引用量,m个区间[L,R]的查询,询问假设他只发了[L,R]这个区间内的这些论文,则他的h-index为多少。

题解:

当初比赛的时候不会主席树,也不会莫队,只能拿着普通的线段树在那里硬刚,结果十分凄惨。

现在会了分块版的莫队算法,回来补题了。

考虑num[c]表示引用量为c的文章数,那么用树状数组维护就可以 $O\left( {\log _2 N} \right)$ 的进行单点修改、区间查询,正好符合题目要求;

所以我们每次区间转移进行 $O\left( {\log _2 N} \right)$ 的进行单点修改,

然后转移结束之后,当前区间的答案(h_index)就可以通过 二分查找 + $O\left( {\log _2 N} \right)$ 的区间查询 得到。

时间复杂度大概在 $O\left( {N \cdot \sqrt N \cdot \left( {\log _2 N} \right)^2 } \right)$,从理论上讲应该可以过。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+;
const int MAXM=1e5+; int n,m;
int citation[MAXN];
int h_idx[MAXM];
struct Query
{
int block;
int id;
int l,r;
bool operator <(const Query &oth) const
{
if(block==oth.block) return r<oth.r;
return block<oth.block;
}
}query[MAXM]; struct _BIT{
int N,C[MAXN];
int lowbit(int x){return x&(-x);}
void init(int n)//初始化共有n个点
{
N=n;
for(int i=;i<=N;i++) C[i]=;
}
void add(int pos,int val)//在pos点加上val
{
while(pos<=N)
{
C[pos]+=val;
pos+=lowbit(pos);
}
}
int sum(int pos)//查询1~pos点的和
{
int ret=;
while(pos>)
{
ret+=C[pos];
pos-=lowbit(pos);
}
return ret;
}
}BIT; int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int len=sqrt(n);
for(int i=;i<=n;i++) scanf("%d",&citation[i]); for(int i=;i<=m;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
query[i].block=query[i].l/len;
query[i].id=i;
}
sort(query+,query+m+); BIT.init(n);
int pl=;
int pr=;
int cnt=;
for(int i=;i<=m;i++)
{
if(pr<query[i].r)
{
for(int j=pr+;j<=query[i].r;j++)
{
BIT.add(citation[j],);
cnt++;
}
}
if(pr>query[i].r)
{
for(int j=pr;j>query[i].r;j--)
{
BIT.add(citation[j],-);
cnt--;
}
}
if(pl<query[i].l)
{
for(int j=pl;j<query[i].l;j++)
{
BIT.add(citation[j],-);
cnt--;
}
}
if(pl>query[i].l)
{
for(int j=pl-;j>=query[i].l;j--)
{
BIT.add(citation[j],);
cnt++;
}
}
pl=query[i].l;
pr=query[i].r; int l=,r=n,mid;
while(l<r)
{
mid=(l+r+)/;
if(cnt-BIT.sum(mid-)<mid) r=mid-;
else l=mid;
} h_idx[query[i].id]=r;
} for(int i=;i<=m;i++) printf("%d\n",h_idx[i]);
}
}

HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]的更多相关文章

  1. 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...

  2. HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)

    链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...

  3. BZOJ3289 Mato的文件管理(莫队算法+树状数组)

    题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...

  4. 【BZOJ3289】Mato的文件管理 莫队算法+树状数组

    [BZOJ3289]Mato的文件管理 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是 ...

  5. BZOJ3289【莫队算法+树状数组+离散化】

    思路: 区间逆序数即是交换次数. 逆序数,可以用树状数组吧. 怎么处理区间变换的时候求逆序数啊.. 这里分成左边的增/删,右边的增/删 因为是按时序插入, 所以左边增,增一个数,计算:ans+=sun ...

  6. BZOJ 3289:Mato的文件管理(莫队算法+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3289 题意:…… 思路:求交换次数即求逆序对数.确定了这个之后,先离散化数组.然后在后面插入元素的话 ...

  7. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  8. 【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3289 很裸的莫队... 离线了区间然后分块排序后,询问时搞搞就行了. 本题中,如果知道$[l, r] ...

  9. BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...

随机推荐

  1. Spring中可以复用的工具类&特性记录

    Spring 里有用工具类: GenericTypeResolver 解析泛型类型.核心逻辑还是调用 ResolvableTypeResolvableType 解析泛型类型 BeanWrapper 利 ...

  2. NYOJ 116 士兵杀敌 (线段树,区间和)

    题目链接:NYOJ 116 士兵杀敌 士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的 ...

  3. javascript的replace方法的高级应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Jsoup(二)-- Jsoup查找DOM元素

    一.Jsoup查找DOM元素的方法 getElementById(String id) 根据id 来查询DOM getElementsByTag(String tagName) 根据tag 名称来查询 ...

  5. windows 命令行下 简单好用的查看端口占用情况的方法

    在windows命令行窗口下执行: C:\>netstat -aon|findstr "4444" TCP 127.0.0.1:4444 0.0.0.0:0 LISTENIN ...

  6. apk反编译看包名什么的

    首先默认你是装了java环境的 到google code里面下载apktool1.5.2.tar.bz2和apktool-install-windows-r05-ibot.tar.bz2(地址:htt ...

  7. U盘安装centos7:不能载入到安装界面

    在用U盘安装centos7时,我们需要修改镜像位置: 选择第一项:Install CentOS 7 ,按 e(也有可能是tab键)键进入编辑界面. 将 vmlinuz initrd=initrd.im ...

  8. SQL - ROW_NUMBER,Rank 添加序号列

    百度的时候查到的博客: http://blog.csdn.net/xsfqh/article/details/6663895-------------------------------------- ...

  9. 虚拟机可以ping同宿主机,宿主机ping不通虚拟机

    虚拟机里能ping同本机,而本机却ping不通虚拟机,或者虚拟机不能ping通本机,可能有如下原因: 如果是桥接模式,那么可能性1:虚拟机防火墙禁ping,请关闭虚拟机防火墙重试:root 状态下se ...

  10. 【大数据系列】hadoop脚本分析

    一.start-all.sh hadoop安装目录/home/hadoop/hadoop-2.8.0/ libexec/hadoop-config.sh     ---设置变量 sbin/start- ...