[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 (主席树)的更多相关文章

  1. BZOJ 3524: [Poi2014]Couriers [主席树]

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1892  Solved: 683[Submit][St ...

  2. 【BZOJ3524/2223】[Poi2014]Couriers 主席树

    [BZOJ3524][Poi2014]Couriers Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大 ...

  3. [BZOJ2223][BZOJ3524][Poi2014]Couriers 主席树

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2436  Solved: 960[Submit][St ...

  4. BZOJ3524[Poi2014]Couriers——主席树

    题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...

  5. BZOJ3524: [Poi2014]Couriers(主席树)

    题意 题目链接 Sol 严格众数只会出现一次,那么建出主席树,维护子树siz,直接在树上二分即可 #include<bits/stdc++.h> #define LL long long ...

  6. 【bzoj3524】[Poi2014]Couriers 主席树

    题目描述 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入 第一行 ...

  7. BZOJ 3524 Couriers | 主席树

    BZOJ 3524 Couriers 题意 求一个区间内出现超过区间长度的一半的数,如果没有则输出0. 题解 我可能太菜了吧--这道题愣是没想出来-- 维护权值主席树,记录每个数都出现过多少次: 查询 ...

  8. 2018.09.14 洛谷P3567 [POI2014]KUR-Couriers(主席树)

    传送门 简单主席树啊. 但听说有随机算法可以秒掉%%%(本蒟蒻并不会) 直接维护值域内所有数的出现次数之和. 当这个值不大于区间总长度的一半时显然不存在合法的数. 这样在主席树上二分查值就行了. 代码 ...

  9. [POI2014]KUR-Couriers BZOJ3524 主席树

    给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. Input 第一行两 ...

  10. 3524: [Poi2014]Couriers -- 主席树

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组 ...

随机推荐

  1. leetcode-优美的排列

    假设有从 1 到 N 的 N 个整数,如果从这 N 个数字中成功构造出一个数组,使得数组的第 i 位 (1 <= i <= N) 满足如下两个条件中的一个,我们就称这个数组为一个优美的排列 ...

  2. [leetcode]三数之和

    三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  3. while read读取文本内容

    读取文件给 while 循环 方式一: exec <FILE while read line do cmd done 方式二: cat FILE_PATH |while read line do ...

  4. 性能度量RMSE

    回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...

  5. KETTLE元数据表

    表名 说明 R_CLUSTER R_CLUSTER_SLAVE R_CONDITION R_DATABASE 数据库连接信息 R_DATABASE_ATTRIBUTE 数据库属性 R_DATABASE ...

  6. CF刷刷水题找自信 2

    CF 1114A  Got Any Grapes(葡萄)? 题目意思:给三个人分葡萄,三个人对葡萄的颜色有一些要求,问所准备的三种颜色的葡萄能否满足三人的要求. 解题意思:直接按条件判断即可. #in ...

  7. erlang驱动使用mysql-otp

    Magnus Ahltorp的Mysql Driver里面介绍emysql的缺陷: 1. 隔离不够好, 2.不能伸缩 mysql-otp使用1个进程1个mysql连接,隔离得很好.推荐使用. mysq ...

  8. 设计模式--Restful笔记(一)

    一.REST基础概念 首先REST是 Representational State Transfer 的缩写,如果一个架构符合REST原则,它就是RESTful架构. 在REST中的一切都被认为是一种 ...

  9. QUdpSocket-Qt使用Udp通讯实现服务端和客户端

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QUdpSocket-Qt使用Udp通讯实现服务端和客户端     本文地址:https:// ...

  10. IIS部署时未能加载程序集"System.Web.Http.WebHost"解决方法

    问题如下 服务器没有安装MVC,下载以下dll覆盖到bin目录下,即可免安装运行程序. 下载地址:https://pan.baidu.com/s/1mhCo5mS