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 ...
随机推荐
- scrapy(一)scrapy 安装问题
一.安装scrapy pip install scrapy 二.出现Microsoft Visual C++ 14.0相关问题 注:若出现以下安装错误 building 'twisted.test.r ...
- golang交叉编译:Linux - Windows
环境:Debian jessiego 1.7.4Windows 7 背景: 在debian中写好的程序编译后在windows上运行. 程序中使用了sqlite3 import( _ "git ...
- 第3章 如何用DAP仿真器下载程序
第3章 如何用DAP仿真器下载程序 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
- Django Rest Framework源码剖析(六)-----序列化(serializers)
一.简介 django rest framework 中的序列化组件,可以说是其核心组件,也是我们平时使用最多的组件,它不仅仅有序列化功能,更提供了数据验证的功能(与django中的form类似). ...
- 2017-2018 Exp5 MSF基础应用 20155214
目录 Exp5 MSF基础应用 实验内容 渗透攻击 主要思路 知识点 Exp5 MSF基础应用 本次实验本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路. 主动攻击:m ...
- 2017-2018 Exp3 MAL_免杀原理与实践 20155214
目录 Exp3 MAL_免杀原理与实践 实验内容 对msf生成后门程序的检测 Veil-Evasion应用 Visual Studio2017 + shellcode生成后门 主要思路 知识点 最后的 ...
- 20155239吕宇轩《网络对抗》Exp3 免杀原理与实践
20155239吕宇轩<网络对抗>Exp3 免杀原理与实践 实验过程 Kali使用上次实验msfvenom产生后门的可执行文件,上传到老师提供的网址http://www.virscan.o ...
- CodeForces-1155D Beautiful Array
Description You are given an array \(a\) consisting of \(n\) integers. Beauty of array is the maximu ...
- 微信小程序之缓存——不同页面传递数据
1. 添加缓存 单个密钥允许存储的最大数据长度为1MB,所有数据存储上限为10MB. // 存储信息到storage // 异步存储 set() { wx.setStorage({ key: 'use ...
- Macaca 基础原理浅析
导语 前面几篇文章介绍了在Macaca实践中的一些实用技巧与解决方案,今天简单分析一下Macaca的基础原理.这篇文章将以前面所分享的UI自动化Macaca-Java版实践心得中的demo为基础,进行 ...