[POI2014] KUR-Couriers (主席树)
[POI2014]KUR-Couriers
题目描述
Byteasar works for the BAJ company, which sells computer games.
The BAJ company cooperates with many courier companies that deliver the games sold by the BAJ company to its customers.
Byteasar is inspecting the cooperation of the BAJ company with the couriers.
He has a log of successive packages with the courier company that made the delivery specified for each package.
He wants to make sure that no courier company had an unfair advantage over the others.
If a given courier company delivered more than half of all packages sent in some period of time, we say that it dominated in that period.
Byteasar wants to find out which courier companies dominated in certain periods of time, if any.
Help Byteasar out!
Write a program that determines a dominating courier company or that there was none.
给一个数列,每次询问一个区间内有没有一个数出现次数超过一半,没有这种数输出0
输入输出格式
输入格式:
The first line of the standard input contains two integers, and
(
), separated by a single space, that are the number of packages shipped by the BAJ company and the number of time periods for which the dominating courier is to be determined, respectively.
The courier companies are numbered from to (at most)
.
The second line of input contains integers,
(
), separated by single spaces;
is the number of the courier company that delivered the
-th package (in shipment chronology).
The lines that follow specify the time period queries, one per line.
Each query is specified by two integers, and
(
), separated by a single space.
These mean that the courier company dominating in the period between the shipments of the -th and the
-th package, including those, is to be determined.
In tests worth of total score, the condition
holds, and in tests worth
of total score
.
输出格式:
The answers to successive queries should be printed to the standard output, one per line.
(Thus a total of lines should be printed.) Each line should hold a single integer: the number of the courier company that dominated in the corresponding time period, or
if there was no such company.
输入输出样例
输入样例#1:
7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
输出样例#1:
1
0
3
0
4
说明
给一个数列,每次询问一个区间内有没有一个数出现次数超过一半
Solution
前置技能:
主席树
主席树学习传送门
对原序列建主席树,对于一段区间,我们先尝试左子树是否可以满足条件,再查右子树,如果都不能就返回0
其实如果会主席树,这道题挺显然的
Code
#include<bits/stdc++.h>
#define in(i) (i=read())
#define il extern inline
#define rg register
#define mid ((l+r)>>1)
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define lol long long
using namespace std;
const lol N=1e6+10;
lol read() {
lol ans=0, f=1; char i=getchar();
while (i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
while (i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+(i^48), i=getchar();
return ans*f;
}
int n,m,tot,num;
int a[N],b[N],rt[N<<6];
struct Chair_Tree {
int l,r,v;
}t[N<<6];
void build(int &u,int l,int r) {
u=++tot;
if(l==r) {t[u].v=a[l]; return;}
build(t[u].l,l,mid);
build(t[u].r,mid+1,r);
}
void insert(int &u,int l,int r,int pre,int p) {
t[u=++tot]=t[pre]; t[u].v++;
if(l==r) return;
if(p<=mid) insert(t[u].l,l,mid,t[pre].l,p);
else insert(t[u].r,mid+1,r,t[pre].r,p);
}
int query(int x,int y,int l,int r,int k) {
if(l==r) return b[l];
int ln=t[t[y].l].v-t[t[x].l].v;
int rn=t[t[y].r].v-t[t[x].r].v;
if(k<=ln) return query(t[x].l,t[y].l,l,mid,k);
if(k<=rn) return query(t[x].r,t[y].r,mid+1,r,k);
return 0;
}
int main()
{
in(n), in(m);
for (int i=1;i<=n;i++) in(a[i]),b[i]=a[i];
sort(b+1,b+1+n); num=unique(b+1,b+1+n)-b-1;
for (int i=1;i<=n;i++) {
int p=lower_bound(b+1,b+1+num,a[i])-b;
insert(rt[i],1,num,rt[i-1],p);
}
for (int i=1,l,r,k;i<=m;i++) {
in(l),in(r), k=(r-l+1)/2+1;
printf("%d\n",query(rt[l-1],rt[r],1,num,k));
}
}
[POI2014] KUR-Couriers (主席树)的更多相关文章
- BZOJ 3524: [Poi2014]Couriers [主席树]
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1892 Solved: 683[Submit][St ...
- 【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. 输入 第一行 ...
- BZOJ3524: [Poi2014]Couriers(主席树)
题意 题目链接 Sol 严格众数只会出现一次,那么建出主席树,维护子树siz,直接在树上二分即可 #include<bits/stdc++.h> #define LL long long ...
- 【bzoj3524】[Poi2014]Couriers 主席树
题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...
- BZOJ 3524 Couriers | 主席树
BZOJ 3524 Couriers 题意 求一个区间内出现超过区间长度的一半的数,如果没有则输出0. 题解 我可能太菜了吧--这道题愣是没想出来-- 维护权值主席树,记录每个数都出现过多少次: 查询 ...
- 2018.09.14 洛谷P3567 [POI2014]KUR-Couriers(主席树)
传送门 简单主席树啊. 但听说有随机算法可以秒掉%%%(本蒟蒻并不会) 直接维护值域内所有数的出现次数之和. 当这个值不大于区间总长度的一半时显然不存在合法的数. 这样在主席树上二分查值就行了. 代码 ...
- [POI2014]KUR-Couriers BZOJ3524 主席树
给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. Input 第一行两 ...
- 3524: [Poi2014]Couriers -- 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组 ...
随机推荐
- 3星|麦肯锡合伙人《从1到N》:PPT讲稿,图表不错,讲解不够深入
从1到N:企业数字化生存指南 两位作者是麦肯锡合伙人.全书插图比较多,图做的还比较有水平.但是相关文字不够深入,我读后的感觉是:图表不是两位执笔者做的,他们对细节不清楚,对图表涉及到的行业也缺乏深入的 ...
- 基础的Servlet
1.认识Servlet 今天接触了Servlet,我就写了这篇Servlet的文章.首先,我们了解一下Servlet是什么: 这是百度百科的解释,我个人理解是可以用来前后端交互的一个东西,例如可以实现 ...
- [转]Zookeeper系列(一)
一.ZooKeeper的背景 1.1 认识ZooKeeper ZooKeeper---译名为“动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而 ...
- mac react-native从零开始android真机测试
1. 安装android相关jdk,(https://blog.csdn.net/vvv_110/article/details/72897142) 2. 手机和mac使用usb连接, 手机开发者设置 ...
- http状态码 以及请求响应头相关
1xx消息[编辑] 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...
- Android 中的广播机制
Android 中的广播机制 Android 中的广播,按照广播响应范围,可以分为应用内广播和全局广播.按照广播的接收方式,可以分为标准广播和有序广播. 广播的分类 响应范围 应用内广播:此类广播只能 ...
- Spring 依赖的Jar包简介
Spring 依赖的Jar包简介 Spring的依赖关系 依赖关系分组 JAR文件 说 明 ant ant.jar, ant-junit.jar, ant-launcher.jar Spring采用A ...
- Alpha冲刺——第八天
Alpha第八天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- alpha冲刺(事后诸葛亮)
[设想和目标] Q1:我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? "小葵日记"是为了解决18-30岁年轻用户在记录生活时希望得到一美体验友好 ...
- lintcode-477-被围绕的区域
477-被围绕的区域 给一个二维的矩阵,包含 'X' 和 'O', 找到所有被 'X' 围绕的区域,并用 'X' 填充满. 样例 给出二维矩阵: X X X X X O O X X X O X X O ...