2018湘潭邀请赛C题(主席树+二分)
题目地址:https://www.icpc.camp/contests/6CP5W4knRaIRgU
比赛的时候知道这题是用主席树+二分,可是当时没有学主席树,就连有模板都不敢套,因为代码实在是太长了。
题意:给你一些数字,要求你某些区间中找到一个h-index。
每次查找h-index复杂度不能超过O(n)
h-index的定义是:有最少h个数不小于h,找到最大的h。
分析:假如查询的区间长度为n,那么ans一定是1-n。用二分查找找到一个最大的n即可
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
struct Tree
{
int L,R,num;
}tree[maxn*20];
int root[maxn];
int cnt;
void updata(int x,int &rt,int a,int b)
{
tree[cnt++]=tree[rt];
rt=cnt-1;
tree[rt].num++;
if(a==b)return;
int mid=(a+b)/2;
if(x>=mid+1)
updata(x,tree[rt].R,mid+1,b);
else
updata(x,tree[rt].L,a,mid);
}
int quer(int a,int b,int k,int s,int o)
{
if(s==o)return s;
int d=tree[tree[b].L].num-tree[tree[a].L].num;
int mid=(s+o)/2;
if(k<=d)
return quer(tree[a].L,tree[b].L,k,s,mid);
else
return quer(tree[a].R,tree[b].R,k-d,mid+1,o);
}
int main()
{
int n,m;
while(cin>>n>>m)
{
cnt=1;
for(int i=1;i<=n;i++)
{
root[i]=root[i-1];
int num;
scanf("%d",&num);
updata(num,root[i],1,n);
}
for(int i=1;i<=m;i++)
{
int l,r;
scanf("%d %d",&l,&r);
int b=r-l+1,a=1;
int mid=(a+b)/2;
while(a!=b)
{
if(quer(root[l-1],root[r],r-l+1-mid,1,n)>=mid+1)
a=mid+1;
else
b=mid;
mid=(a+b)/2;
}
printf("%d\n",a);
}
}
return 0;
}
2018湘潭邀请赛C题(主席树+二分)的更多相关文章
- HDU 6280 From Tree to Graph(2018 湘潭邀请 E题,树的返祖边)
其实打返祖边就相当于$x$到祖先这一段点(不包括两端)答案都要减$1$. 然后每个点最多减$1$次$1$. #include <bits/stdc++.h> using namespace ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- HDU - 4866 主席树 二分
题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...
- BZOJ.1926.[SDOI2010]粟粟的书架(前缀和 主席树 二分)
题目链接 题意: 在给定矩形区域内找出最少的数,满足和>=k.输出数的个数.两种数据范围. 0~50 注意到(真没注意到...)P[i,j]<=1000,我们可以利用前缀和预处理. num ...
- POJ 6621: K-th Closest Distance(主席树 + 二分)
K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)
HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- BZOJ 1926: [Sdoi2010]粟粟的书架(主席树,二分答案)
BZOJ 1926: [Sdoi2010]粟粟的书架(主席树,二分答案) 题意 : 给你一个长为\(R\)宽为\(C\)的矩阵,第\(i\)行\(j\)列的数为\(P_{i,j}\). 有\(m\)次 ...
- bzoj 1926: [Sdoi2010]粟粟的书架 (主席树+二分)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1926 题面; 1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Se ...
随机推荐
- JQuery实现全选、全不选和反选功能
看重点就行了,最后面给一个网页完整的代码 代码实例: function selectStu() { $.ajax("StuList",{ type:"post" ...
- 安装Jenkins getting started卡住
前言 jenkins版本:2.32.3 操作系统:windows 卡住信息 如果在安装jenkins时卡在getting startted的界面,如下所示 解决方法 1.打开 运行 输入 servi ...
- vbs脚本,电脑说出指定内容
新建文件, 输入一下脚本, 然后保存文件为 .vbs 文件, 双击脚本, 电脑就能听到电脑发出 你好 的声音 CreateObject("SAPI.SpVoice").Speak& ...
- B - Moo Volume
Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are ...
- RocketMQ实现事务消息
在RocketMQ4.3.0版本后,开放了事务消息这一特性,对于分布式事务而言,最常说的还是二阶段提交协议,那么RocketMQ的事务消息又是怎么一回事呢,这里主要带着以下几个问题来探究一下Rocke ...
- ELMO模型(Deep contextualized word representation)
1 概述 word embedding 是现在自然语言处理中最常用的 word representation 的方法,常用的word embedding 是word2vec的方法,然而word2vec ...
- WPF ListView点击删除某一行并获取绑定数据
最近在开发WPF程序时遇到一个问题,在gridview中希望实现在每一行最后添加一个删除的按钮,但是发现点击每行的button时只会触发button的点击事件,并没有选中这一行,此时调用list.Se ...
- 史上最简单的springboot国际化多语言切换实现方案
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 前提: 在resources目录下建立 messages_en_US.properti ...
- 004_Python之all()\any()
http://www.jianshu.com/p/65b6b4a62071 一.待验证整理
- .Net使用Redis详解之ServiceStack.Redis(七) 转载https://www.cnblogs.com/knowledgesea/p/5032101.html
.Net使用Redis详解之ServiceStack.Redis(七) 序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现 ...