poj3368 Frequent values(线段树)
Description
You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.
Input
The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.
The last test case is followed by a line containing a single 0.
Output
For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.
Sample Input
10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
Sample Output
1
4
3
Source
program rrr(input,output);
type
treetype=record
l,r,m,lm,rm:longint;
end;
var
a:array[..]of treetype;
c:array[..]of longint;
n,i,q,x,y:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure build(k,l,r:longint);
var
mid,i:longint;
begin
a[k].l:=l;a[k].r:=r;
if l=r then begin a[k].m:=;a[k].lm:=;a[k].rm:=;exit; end;
mid:=(l+r)>>;i:=k+k;
build(i,l,mid);build(i+,mid+,r);
a[k].m:=max(a[i].m,a[i+].m);if c[mid]=c[mid+] then a[k].m:=max(a[k].m,a[i].rm+a[i+].lm);
if c[l]=c[mid+] then a[k].lm:=a[i].lm+a[i+].lm else a[k].lm:=a[i].lm;
if c[mid]=c[r] then a[k].rm:=a[i].rm+a[i+].rm else a[k].rm:=a[i+].rm;
end;
function query(k:longint):longint;
var
mid,i,ans:longint;
begin
if (x<=a[k].l) and (a[k].r<=y) then exit(a[k].m);
mid:=(a[k].l+a[k].r)>>;i:=k+k;
ans:=;
if x<=mid then ans:=query(i);
if mid<y then ans:=max(ans,query(i+));
if (x<=mid) and (mid<y) and (c[mid]=c[mid+]) then ans:=max(ans,min(mid-x+,a[i].rm)+min(y-mid,a[i+].lm));
exit(ans);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
while true do
begin
read(n);if n= then break;
readln(q);
for i:= to n do read(c[i]);
build(,,n);
for i:= to q do begin readln(x,y);writeln(query()); end;
end;
close(input);close(output);
end.
poj3368 Frequent values(线段树)的更多相关文章
- POJ3368(Frequent values)--线段树
题目在这里 3368 Accepted 7312K 1829MS C++ 6936B 题意为给你一组数据,再给定一组区间,问你这个区间内出现次数最多的元素的次数是多少. 我还记得这题是学校校赛基础的题 ...
- HDOJ-1806 ( Frequent values ) 线段树区间合并
http://acm.hdu.edu.cn/showproblem.php?pid=1806 线段树维护区间出现频率最高的出现次数.为了维护上者,需要维护线段前后缀的出现次数,当和其他线段在端点处的字 ...
- UVA 11235 Frequent values 线段树/RMQ
vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- hdu 1806 Frequent values 线段树
题目链接 给一个非递减数列, n个数, m个询问, 每个询问给出区间[L, R], 求这个区间里面出现次数最多的数的次数. 非递减数列, 这是最关键的一个条件... 需要保存一个区间最左边的数, 最右 ...
- POJ3368 Frequent values(RMQ线段树)
题目大概说给一个递增序列,询问区间出现最多的数. 用莫队算法比较直观,虽然应该会T..好像也可以主席树..不过题目给的序列是有序的,因而相同的数会聚在一起. 考虑把序列分成一段一段,使每段都包含极大的 ...
- [poj3368]Frequent values(rmq)
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ ...
- poj3368 Frequent values
思路: 转化为RMQ. 实现: #include <cstdio> #include <cstring> #include <algorithm> using na ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
随机推荐
- Kafka设计解析(二十一)Kafka水位(high watermark)与leader epoch的讨论
转载自 huxihx,原文链接 Kafka水位(high watermark)与leader epoch的讨论 本文主要讨论0.11版本之前Kafka的副本备份机制的设计问题以及0.11是如何解决的. ...
- (三) DRF 序列化
一.单表的GET和POST: 使用serializers序列化,针对每一个表,需要单独写函数.一般会写在views.py里面,但是这样做,会导致整个文件代码过长.需要分离出来! 在app01(应用名) ...
- ceph存储osd启动异常处理和正常启停操作
机器角色:cloudstack虚拟机的宿主机:ceph存储机器. 事件:ceph存储的物理机器由于内存异常,需要停机更换,仅仅是把该物理机上面的虚拟机迁移走,同时启动了停机维护,然后就直接关机.结果造 ...
- DB2创建function(一)
案例一:根据传入的值返回一个满足条件的值.适用于查询的字段(经过较复杂逻辑得出) CREATE FUNCTION "FAS"."GET_ALL_NAME" ( ...
- Linux CentOS7系统中phpMyAdmin安装配置
今天介绍的是如何在Linux CentOS7系统中配置phpMyAdmin. 目录 环境准备 安装包 基本设置 网站预览 环境准备 linux centos7系统 ssh软件 php语言环境 mysq ...
- MySQL优化:explain using temporary
什么时候会使用临时表:group/order没设计好的时候 1.order没用索引 2.order用了索引, 但不是和where相同的索引 3.order用了两个索引, 但不是联合索引 4.order ...
- EZ 2018 04 01 ZJOI2018模拟赛04.01
现在开始填以前的坑 这次老叶强制我们打一下这次省选题,然后我已经做好了掉Rating到死的准备 然后考完--莫名涨了 Orz 题目链接 由于很多东西我都不会,所以详细请看:dalao的题解 T1 我T ...
- Qt控件使用汇总
QTableWidget: //tab居中显示 QTabWidget::tab-bar { alignment: center; } //tab边框样式,边框颜色,前景色 QTabBar::tab { ...
- 【第七课】Nginx反向代理和负载均衡
目录 一.Nginx负载均衡集群 介绍 二.实现一个简单的负载均衡 三.Nginx负载均衡组件介绍 四.Nginx负载均衡实际应用 一.Nginx负载均衡集群 介绍 负载均衡(Load Balance ...
- 老项目迁移到springboot之后,上线服务器出现404的解决方法
原因是老项目迁移到springboot之后,已经不再使用web.xml的配置了,但是WEB-INF目录下还有web.xml,所以才导致的404,所以只需要在源码处删除整个WEB-INF重新build即 ...