题目链接: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. VS2010安装包制作全过程图解

    项目的第一个版本出来了,要做个安装包,很久没做过已经有些淡忘了,网上差了差资料,写了一个,总结下,可能还不是很完善,仅作参考. 1.首先在打开 VS2010    >新建>项目 2.创建一 ...

  2. ios开发之--字符串局部改变颜色

    改变指定位置字符的颜色,代码如下: NSString *descStr = @"楼主"; NSString *nickStr = [NSString stringWithForma ...

  3. 【遥感影像】Python GDAL 像素与坐标对应

    转:https://blog.csdn.net/theonegis/article/details/50805520 https://blog.csdn.net/wsp_1138886114/arti ...

  4. Selenium 选项卡管理

    什么是选项卡: from selenium import webdriver browser = webdriver.Chrome() browser.get("http://www.bai ...

  5. [Linux] 如何修改 Linux 主机名

    该方法适用于安装了 Linux 系统的Raspberry Pi & Cubieboard. 在终端执行: sudo vi /etc/hosts 你看到的 hosts 文件应该是这样的: 127 ...

  6. MDK972-EK开发板裸调试设置和裸机程序烧写(转)

    硬件平台:MDK972-EK开发板编译调试软件:KEIL uVision4仿真工具:JLINK V7/V8   本例子从串口输出信息,如图:       KEIL uVision4调试设置如图所示: ...

  7. 浏览器解析html全过程详解

    前端文摘:深入解析浏览器的幕后工作原理 关于浏览器解析html全过程详解 输入URL到浏览器接收返回的数据的整个过程 TCP报文格式详解 IP报文格式详解 Linux IO模式及 select.pol ...

  8. 浅析TCP字节流与UDP数据报的区别

    转自http://www.linuxidc.com/Linux/2014-11/109545.htm “TCP是一种流模式的协议,UDP是一种数据报模式的协议”,这句话相信大家对这句话已经耳熟能详~但 ...

  9. webpack4 优化记录

    webpack4.0优化那些事儿 一 缩小文件搜索范围 1 include & exclude 1) action 限制编译范围 2) useage module: { rules: [ { ...

  10. Android开发进阶从小工到专家之性能优化