主席树【bzoj3524(p3567)】[POI2014]Couriers
Description
给一个长度为n的序列a。1≤a[i]≤n。 m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2。如果存在,输出这个数,否则输出0。
Input
第一行两个数n,m。
第二行n个数,a[i]。
接下来m行,每行两个数l,r,表示询问[l,r]这个区间。
Output
m行,每行对应一个答案。
主席树板子题
话说为啥是个板子题,我也没看出来
查询时需要找出出现次数为\((r-l+1)/2\)的.
主席树基本操作,离散化是必须的.
然后建树操作与之前操作相同.
难点在于查询时候如何做.
最基本的查询,是判断当前根的左节点的\(sum\)与查询的大小的关系.
如果左边的\(sum\)大于等于\((r-l+1)/2\)则,此数在左侧.
然后如何判断在右侧?
用当前根总共的\(sum\)减去左边的\(sum\),即为右边的\(sum\),判断是否大于\((r-l+1)/2\)即可.
否则\(return 0\)
代码
#include<cstdio>
#include<cctype>
#include<algorithm>
#define N 500008
#define R register
using namespace std;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,m,new_n=1;
int a[N],b[N],cnt;
int root[N*20],sum[N*20],lson[N*20],rson[N*20];
void build(int lastroot,int &nowroot,int l,int r,int pos)
{
nowroot=++cnt;
sum[nowroot]=sum[lastroot]+1;
lson[nowroot]=lson[lastroot];
rson[nowroot]=rson[lastroot];
if(l==r)return;
int mid=(l+r)>>1;
if(pos<=mid)build(lson[lastroot],lson[nowroot],l,mid,pos);
else build(rson[lastroot],rson[nowroot],mid+1,r,pos);
}
int query(int lastroot,int nowroot,int l,int r,int pos)
{
if(l==r)return l;
int tmp=sum[lson[nowroot]]-sum[lson[lastroot]];
int mid=(l+r)>>1;
if(pos<tmp)return query(lson[lastroot],lson[nowroot],l,mid,pos);
if(sum[nowroot]-sum[lastroot]-tmp>pos)return query(rson[lastroot],rson[nowroot],mid+1,r,pos);
return 0;
}
int main()
{
in(n),in(m);
for(R int i=1;i<=n;i++)in(a[i]),b[i]=a[i];
sort(b+1,b+n+1);
for(R int i=2;i<=n;i++)if(b[i]!=b[new_n])b[++new_n]=b[i];
for(R int i=1;i<=n;i++)
build(root[i-1],root[i],1,new_n,lower_bound(b+1,b+new_n+1,a[i])-b);
for(R int i=1,l,r;i<=m;i++)
{
in(l),in(r);
int pos=query(root[l-1],root[r],1,new_n,(r-l+1)>>1);
printf("%d\n",b[pos]);
}
}
主席树【bzoj3524(p3567)】[POI2014]Couriers的更多相关文章
- 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers
题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...
- [bzoj3524==bzoj2223][Poi2014]Couriers/[Coci 2009]PATULJCI——主席树+权值线段树
题目大意 给定一个大小为n,每个数的大小均在[1,c]之间的数列,你需要回答m个询问,其中第i个询问形如\((l_i, r_i)\),你需要回答是否存在一个数使得它在区间\([l_i,r_i]\)中出 ...
- 【bzoj3524】[Poi2014]Couriers 主席树
题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...
- 【BZOJ3524】 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
- BZOJ3524:[POI2014]Couriers
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- bzoj3524/2223 [Poi2014]Couriers
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3524 http://www.lydsy.com/JudgeOnline/problem.ph ...
- 【BZOJ3524/2223】[Poi2014]Couriers 主席树
[BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...
- [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2436 Solved: 960[Submit][St ...
- BZOJ3524[Poi2014]Couriers——主席树
题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...
随机推荐
- [洛谷P1120]小木棍 [数据加强版]
题目大意:有一些同样长的木棍,被切割成几段(长$\leqslant$50).给出每段小木棍的长度,找出原始木棍的最小可能长度. 题解:dfs C++ Code: #include<cstdio& ...
- 工具——SVN常用命令
SVN一般都是团队合作做一个项目所需用到的,为了是版本的统一 ;1. Check out——从服务器端取得代码 把服务器资料库里存放的某个项目代码取出来,放到本地主机中,这个动作叫做“check ...
- Angular Cookie 读写
var app = angular.module('Mywind',['ui.router']) app.controller('Myautumn',function($scope,$http,$fi ...
- 判断当前系统当前浏览器是否安装启用 Adobe Flash Player,检查在chrome中的状态
一.判断当前所在系统 let sUserAgent = navigator.userAgent;let isWin = (navigator.platform == "Win32" ...
- ubuntu使用su切换root用户提示“认证失败”
在虚拟机上安装了ubuntu,安装时提示设置密码,也设置了,但是在终端操作时,遇到权限不够的问题,于是就想到就是要切换root用户,获取最高权限. 当我使用 su 切换到root用户时,提示我输入密码 ...
- [CVPR2017]Online Video Object Segmentation via Convolutional Trident Network
基于三端卷积网络的在线视频目标分割 针对半监督视频目标分割任务,作者采取了和MaskTrace类似的思路,以optical flow为主. 本文亮点在于: 1. 使用共享backbone,三输出的自编 ...
- UVa11988 Broken Keyboard (a.k.a. Beiju Text)
题目复制太麻烦了,甩个链接 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693 直接模拟光标操作时间复杂度较高,所以用链 ...
- 将setter方法与itemClick: 进行类比
https://www.evernote.com/shard/s227/sh/a0c3afa3-8792-4756-8594-d2387a7f57ad/b561ff665af9ad401c8e ...
- autoKeras Windows 的入门测试
在测试中分析一下ide的效果,在pycharm中测试的时候老师提示内存溢出,而且跑autoKeras的cnn时确实消耗很大空间.但是同样的电脑,换了vscode进行测试的时候没有问题.我也不知道什么回 ...
- 数据安全之MD5、SHA-1、CRC32区别
crc32 — 计算一个字符串的 crc32 多项式 生成 string 参数的 32 位循环冗余校验码多项式……:这句话从英文翻译过来的,不正确,准确的说应该是这么理解: 以32位循环冗余校验多项式 ...