[POI2014]Couriers
OJ题号:BZOJ3524、BZOJ2223、洛谷3567
思路:
维护一颗可持久化权值线段树,记录每次加入数字时,不同数字出现的个数。
对于每一个询问$[l,r]$,同时查询以$r$和$l-1$为根的线段树,每次比较两个节点左右字子树的权值和,如果大于$[l,r]$区间的一半就说明这一子区间可能有答案,递归查询即可。
#include<cstdio>
#include<cctype>
#include<cstring>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=,SZ=;
class FotileTree {
private:
int val[SZ],sz,left[SZ],right[SZ];
int newnode() {
return sz++;
}
public:
FotileTree() {
sz=;
memset(val,,sizeof val);
}
int root[N];
int build(const int b,const int e) {
int new_p=newnode();
if(b==e) return new_p;
int mid=(b+e)>>;
left[new_p]=build(b,mid);
right[new_p]=build(mid+,e);
return new_p;
}
int modify(const int p,const int b,const int e,const int x) {
int new_p=newnode();
val[new_p]=val[p]+;
if(b==e) return new_p;
int mid=(b+e)>>;
if(x<=mid) left[new_p]=modify(left[p],b,mid,x),right[new_p]=right[p];
if(x>mid) right[new_p]=modify(right[p],mid+,e,x),left[new_p]=left[p];
return new_p;
}
int query(const int p1,const int p2,const int b,const int e,const int k) {
if(b==e) return b;
int mid=(b+e)>>;
if(val[left[p2]]-val[left[p1]]>k) return query(left[p1],left[p2],b,mid,k);
if(val[right[p2]]-val[right[p1]]>k) return query(right[p1],right[p2],mid+,e,k);
return ;
}
};
FotileTree t;
int main() {
int n=getint(),m=getint();
t.root[]=t.build(,n);
for(int i=;i<=n;i++) t.root[i]=t.modify(t.root[i-],,n,getint());
while(m--) {
int l=getint(),r=getint();
printf("%d\n",t.query(t.root[l-],t.root[r],,n,(r-l+)>>));
}
return ;
}
[POI2014]Couriers的更多相关文章
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- BZOJ 3524: [Poi2014]Couriers
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1905 Solved: 691[Submit][St ...
- 【BZOJ3524/2223】[Poi2014]Couriers 主席树
[BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...
- 3524: [Poi2014]Couriers -- 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组 ...
- [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2436 Solved: 960[Submit][St ...
- 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers
题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...
- Bzoj3524 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...
- BZOJ 3542 [Poi2014]Couriers ——可持久化线段树
[题目分析] 查找区间内出现次数大于一半的数字. 直接用主席树,线段树上维护区间大小,由于要求出现次数大于一半,每到一个节点可以分治下去. 时间复杂度(N+Q)logN [代码] #include & ...
- 【BZOJ3524】 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
- 【bzoj 3524】[Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...
随机推荐
- DFP算法(转载)
转载链接:http://blog.csdn.net/itplus/article/details/21896981 注意:式(2.25)中,蓝色变量之所以是实数可以根据它们的矩阵系数相乘为1*1得到.
- Linux mmc framework1:软件架构
[部分内容来自] http://www.wowotech.net/comm/mmc_framework_arch.html 1. 前言 由eMMC基础技术1:MMC简介中MMC.SD.SDIO的介绍可 ...
- H3C SNMP OID
有两种mib-style [1]老些的设备 cpu 使用率OID: .1.3.6.1.4.1.25506.2.6.1.1.1.1.6.slot 内存使用率OID: .1.3.6.1.4.1.2550 ...
- 转载:2.2.3 配置项的注释《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19628.html 如果有一个配置项暂时需要注释掉,那么可以加"#"注释掉这一行配置.例如: #pid ...
- VMware环境和Window环境进行网络连接的问题
一. 首先贴出本人在网络上找到与VMware网络连接相关的知识点 安装完虚拟机后,默认安装了两个虚拟网卡,VMnet1和VMnet8,其他的未安装(当然也可以手动安装其他的).其中VMnet1是hos ...
- viewpager显示图片的Adapter
package com.ming.chiye.yishanghorse.Adapter; import android.content.Context; import android.graphics ...
- ios如何实现静音模式下声音仍然可以外放
AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSes ...
- 调试Windows Service
调试Windows Service 使用一般的调试方法调试不了Windows Servers,所以参考了一些调试方法 我服务源码中使用了Timer,注意不能使用工具箱内的Timer,用System.T ...
- vmware+CentOs 6.9的安装步骤
一.安装步骤 linux分区 登录用户名和密码 登录用户名和密码后安装成功 二.远程控制Xshell的安装
- MySQL索引底层实现原理
优秀博文: MySQL索引背后的数据结构及算法原理 B树.B-树.B+树.B*树[转],mysql索引 MySQL 和 B 树的那些事 索引的本质 MySQL官方对索引的定义为:索引(Index)是帮 ...