HDU-6278-Jsut$h$-index(主席树)
链接:
https://vjudge.net/problem/HDU-6278
题意:
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.
思路:
主席树模板,记录每个数字出现的次数,如果某个区间内,数字出现的次数大于等于区间左端点,就往右找,
否则吧右边的所有数累加,往左找,因为右边的数大于左边的数,h是单调的
代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
struct Segment
{
int cnt;
int l, r;
}Seg[MAXN*30];
int Tree_root[MAXN*30];
int a[MAXN];
int n, m;
int tree_cnt;
int Insert(int num, int last, int l, int r)
{
tree_cnt++;
int nownode = tree_cnt;
Seg[tree_cnt].cnt = Seg[last].cnt+1;
int mid = (l+r)/2;
if (l == r)
return nownode;
else if (num <= mid)
{
Seg[nownode].l = Insert(num, Seg[last].l, l, mid);
Seg[nownode].r = Seg[last].r;
}
else
{
Seg[nownode].l = Seg[last].l;
Seg[nownode].r = Insert(num, Seg[last].r, mid+1, r);
}
return nownode;
}
int Query(int x, int y, int l, int r, int num)
{
// cout << l << ' ' << r << endl;
if (l == r)
return l;
int mid = (l+r)/2;
int sum = Seg[Seg[y].r].cnt-Seg[Seg[x].r].cnt;
// cout << sum+num << endl;
if (sum+num <= mid)
{
return Query(Seg[x].l, Seg[y].l, l, mid, num+sum);
}
else
{
return Query(Seg[x].r, Seg[y].r, mid+1, r, num);
}
}
void Init()
{
tree_cnt = 0;
Seg[0].cnt = 0;
Tree_root[0] = 0;
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
for (int i = 1;i <= n;i++)
scanf("%d", &a[i]);
Init();
for (int i = 1;i <= n;i++)
{
int pos = Insert(a[i], Tree_root[i-1], 1, n);
Tree_root[i] = pos;
}
int x, y;
while (m--)
{
scanf("%d %d", &x, &y);
printf("%d\n", Query(Tree_root[x-1], Tree_root[y], 1, n, 0));
}
}
return 0;
}
/*
5 3
1 5 3 2 1
1 3
2 4
1 5
5 1000
4 4 4 4 5
1 5
*/
HDU-6278-Jsut$h$-index(主席树)的更多相关文章
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- hdu 4417 Super Mario (主席树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...
- hdu 4348 To the moon (主席树)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...
- HDU 4348 To the moon 主席树 在线更新
http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...
- HDU 2665 && POJ 2104(主席树)
http://poj.org/problem?id=2104 对权值进行建树(这个时候树的叶子是数组b的有序数列),然后二分查找原数列中每个数在有序数列中的位置(即第几小),对每一个前缀[1,i]建一 ...
- HDU 5919 -- Sequence II (主席树)
题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...
- HDU 5919 Sequence II(主席树)题解
题意:有A1 ~ An组成的数组,给你l r,L = min((l + ans[i - 1]) % n + 1, (r + ans[i - 1]) % n + 1),R = max((l + ans[ ...
- HDU 4417 Super Mario(主席树 区间不超过k的个数)题解
题意:问区间内不超过k的个数 思路:显然主席树,把所有的值离散化一下,然后主席树求一下小于等于k有几个就行.注意,他给你的k不一定包含在数组里,所以问题中的询问一起离散化. 代码: #include& ...
- HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...
随机推荐
- [VBA]删除多余工作表
sub 删除多余工作表() Dim i As Integer Application.DisplayAlerts = False For i = Worksheets.Count To 1 step ...
- Mysql登录报1045错误
MySQL在使用root密码登陆报 1045 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password ...
- Oracle 无备份情况下的恢复--密码文件/参数文件
13.1 恢复密码文件 密码文件(linux 为例)在$ORACLE_HOME/dbs目录下,文件名的前缀是orapw,后接数据库实例名. [oracle@DSI backup]$ cd /u01/a ...
- Ubuntu新建用户以及安装pytorch
环境:Ubuntu18,Python3.6 首先登录服务器 ssh username@xx.xx.xx.xxx #登录一个已有的username 新建用户 sudo adduser username ...
- 操作系统安全 - 提权 - Windows提权 - 汇总
CVE_2019-1388 Date: -- 影响范围: SERVER ====== Windows 2008r2 ** link OPENED AS SYSTEM ** Windows 2012r2 ...
- Linux window 安装tomcat各版本下载地址
1.地址 https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/
- Go语言入门篇-jwt(json web token)权限验证
一.token.cookie.session的区别 1.cookie Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie. 内存Cookie由浏览器维护, ...
- 如何上传大文件到github上
真的是被百度坑怕了,只适合我自己的 如果你之前安装git成功的话,右键会出现两个新选项,分别为Git Gui Here,Git Bash Here,这里我们选择Git Bash Here,进入如下界面 ...
- mybatis一级缓存和二级缓存的使用
在mybatis中,有一级缓存和二级缓存的概念: 一级缓存:一级缓存 Mybatis的一级缓存是指SQLSession,一级缓存的作用域是SQLSession, Mabits默认开启一级缓存.在同一个 ...
- 【转】centos7安装
转自:https://blog.csdn.net/qq_42570879/article/details/82853708 1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:htt ...