洛谷题目链接:[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.

给一个数列,每次询问一个区间内有没有一个数出现次数超过一半

输入输出格式

输入格式:

The first line of the standard input contains two integers, \(n\) and \(m\) (\(1 \leq n, m \leq 50000\)), 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 1 to (at most) \(n\).

The second line of input contains \(n\) integers, \(p_1,p_2,...,p_3,p_n\) (\(1 \leq p_i \leq n\)), separated by single spaces; \(p_i\) is the number of the courier company that delivered the \(i\)-th package (in shipment chronology).

The \(m\) lines that follow specify the time period queries, one per line.

Each query is specified by two integers, \(a\) and \(b\) (\(1 \leq a \leq b \leq n\)), separated by a single space.

These mean that the courier company dominating in the period between the shipments of the \(a\)-th and the \(b\)-th package, including those, is to be determined.

In tests worth 65% of total score, the condition \(n, m \leq 50000\) holds, and in tests worth 30% of total score \(n, m \leq 5000\)

输出格式:

The answers to successive queries should be printed to the standard output, one per line.

(Thus a total of \(m\) 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 0 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

说明

给一个数列,每次询问一个区间内有没有一个数出现次数超过一半


一句话题意: 给出一个数列,每次询问一个区间内有没有一个数出现次数超过一半(严格大于).


题解: 既然是静态查询区间的第几小有多少个,所以可以用主席树来维护一下区间中的每个元素个数,然后在查询的时候就判断左子树/右子树是否可以满足条件,如果都不能满足条件,就在查询的过程中返回0.其余都是基本操作.

#include<bits/stdc++.h>
using namespace std;
const int N=500000+5; int n, m, w[N], s[N], rk[N], root[N], size, cnt = 0; struct president_tree{
int ls, rs, cnt;
}t[N*20]; int gi(){
int 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*10+i-'0';i=getchar();}
return ans * f;
} void update(int &node,int last,int pos,int l=1,int r=size){
node = ++cnt; t[node] = t[last]; t[node].cnt++;
if(l == r) return; int mid = (l+r>>1);
if(pos <= mid) update(t[node].ls,t[last].ls,pos,l,mid);
else update(t[node].rs,t[last].rs,pos,mid+1,r);
} int query(int node,int last,int sum,int l=1,int r=size){
if(l == r) return l; int mid = (l+r>>1);
if(2*(t[t[node].ls].cnt-t[t[last].ls].cnt) > sum)
return query(t[node].ls,t[last].ls,sum,l,mid);
if(2*(t[t[node].rs].cnt-t[t[last].rs].cnt) > sum)
return query(t[node].rs,t[last].rs,sum,mid+1,r);
return 0;
} int main(){
//freopen("data.in","r",stdin);
int l, r; n = gi(); m = gi();
for(int i=1;i<=n;i++) w[i] = gi();
memcpy(s,w,sizeof(s)); sort(s+1 , s+n+1);
size = unique(s+1 , s+n+1)-s-1;
for(int i=1;i<=n;i++) rk[i] = lower_bound(s+1 , s+size+1 , w[i])-s;
for(int i=1;i<=n;i++) update(root[i],root[i-1],rk[i]);
for(int i=1;i<=m;i++){
l = gi(); r = gi();
printf("%d\n",query(root[r],root[l-1],r-l+1));
}
return 0;
}

[POI2014] KUR-Couriers(洛谷P3567)的更多相关文章

  1. AC日记——[POI2014]KUR-Couriers 洛谷 P3567

    [POI2014]KUR-Couriers 思路: 卡空间,sb题: 代码: #include <bits/stdc++.h> using namespace std; #define m ...

  2. 洛谷P3567 KUR-Couriers [POI2014] 主席树/莫队

    正解:主席树/莫队 解题报告: 传送门! 这题好像就是个主席树板子题的样子,,,? 毕竟,主席树的最基本的功能就是,维护一段区间内某个数字的个数 但是毕竟是刚get到主席树,然后之前做的一直是第k大, ...

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

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

  4. 洛谷P3567[POI2014]KUR-Couriers(主席树+二分)

    题意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 题解: 最近比赛太多,都没时间切水题了,刚好日推了道主席树裸题,就写了一下 然后 WA80 WA80 WA0 WA90 WA80 ?? ...

  5. [洛谷P3567][POI2014]KUR-Couriers

    题目大意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半.有,输出这个数,否则输出$0$ 题解:主席树,查询区间第$\bigg\lfloor\dfrac{len+1}{2}\bigg\rf ...

  6. 洛谷P3567 [POI2014]KUR-Couriers 主席树

    挺裸的,没啥可讲的. 不带修改的主席树裸题 Code: #include<cstdio> #include<algorithm> using namespace std; co ...

  7. 洛谷 P3580 - [POI2014]ZAL-Freight(单调队列优化 dp)

    洛谷题面传送门 考虑一个平凡的 DP:我们设 \(dp_i\) 表示前 \(i\) 辆车一来一回所需的最小时间. 注意到我们每次肯定会让某一段连续的火车一趟过去又一趟回来,故转移可以枚举上一段结束位置 ...

  8. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  9. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

随机推荐

  1. c/c++容器操作

    C++中的容器大致可以分为两个大类:顺序容器和关联容器.顺序容器中包含有顺序容器适配器. 顺序容器:将单一类型元素聚集起来成为容器,然后根据位置来存储和访问这些元素.主要有vector.list.de ...

  2. OrCAD创建原理图符号图

    1. 首先创建一个库 2. 右键新创建的库,添加新的器件New Part 3. 修改器件属性 4. 添加引脚 添加完引脚之后如图,其中双击引脚,即可修改引脚名字和序号 5. 添加符号的外形 添加完外形 ...

  3. mongodb 安装使用遇到的问题记录

    mongodb 常用命令: https://www.mongodb.org/downloads 官网64位下载链接 https://fastdl.mongodb.org/linux/mongodb-l ...

  4. iOS中的数据库应用

    iOS中的数据库应用 SLQLite简介 什么是SQLite SQLite是一款轻型的嵌入式数据库 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 它的处理速度比Mysql.Post ...

  5. C++学习014函数值传递和地址传递

    当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数 区别就是一个本身不被改变,而另一本身也在改变, 在开发时候都会用到, 这里做下记录 #include <iostre ...

  6. laravel跨域问题

    // 只有同源策略才允许发送cookies // header('Access-Control-Allow-Credentials:true'); 需要要index.php下开启 最近写登录图形验证码 ...

  7. LeetCode 24——两两交换链表中的节点

    1. 题目 2. 解答 新建一个哨兵结点作为头结点,然后每次交换相邻两个结点.并依次将它们连接到新链表中去,再将原链表中后面的结点也串到新链表后面.直至到达链尾或者剩余一个节点,则此时返回新链表的头结 ...

  8. java script 学习

    用JavaScript输出文本 <p>我的第一个段落.</p> <script> document.write(Date()); </script> & ...

  9. IP数据报格式 及分组转发算法

    ip数据报分首部和数据两部分组成: 首部分为固定部分和可变部分 版本——占 4 位,指 IP 协议的版本 目前的 IP 协议版本号为 4 (即 IPv4) 首部长度——占 4 位,可表示的最大数值 是 ...

  10. 如何创建LocalDB数据库和数据库实例

    LocalDB是SQL Server 2012带来的新特性,它是一个专门为开发人员量身定制的轻量级数据库,下面介绍如何使用它. 创建LocalDB数据库的方法: 打开服务器资源管理器,右键点击“数据连 ...