Frequent values(ST)
描述
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.
输入
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 nintegers 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.
输出
For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.
样例输入
10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
样例输出
1
4
3
提示
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1e5+;
int n,m,a[N],rt[N],pre[N];
int st[N][];
int query(int l,int r)
{
if(l>r) return ;
int k=log2(r-l+);
return max(st[l][k],st[r-(<<k)+][k]);
}
int main()
{
while(~scanf("%d",&n),n)
{
memset(st,,sizeof st);
scanf("%d",&m);
a[]=INF;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
if(a[i]==a[i-]) pre[i]=pre[i-]+;
else pre[i]=;
}
for(int i=;i<=n;i++)
st[i][]=pre[i];
for(int j=;j<;j++)
for(int i=;i+(<<(j-))<=n;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
rt[n]=n;
for(int i=n-;i>=;i--)
{
if(a[i]==a[i+]) rt[i]=rt[i+];
else rt[i]=i;
}
while(m--)
{
int l,r,tmp;
scanf("%d%d",&l,&r);
if(rt[l]==rt[l-]) tmp=min(r,rt[l])+;
else tmp=l;
int ans=query(tmp,r);
ans=max(ans,tmp-l);
printf("%d\n",ans);
}
}
return ;
}
Frequent values(ST)的更多相关文章
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...
- 【POJ 3368】Frequent values(RMQ)
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- 51nod——1174 区间中最大的数(ST)
题目链接 给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 7 6 3 1.i = 1, j = 3,对应的数为7 6 3,最大的数 ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- poj 1806 Frequent values(RMQ 统计次数) 详细讲解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...
随机推荐
- leetcdoe Valid Anagram
题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description Given two strings s and ...
- Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境
首先创建一个多模块的SpringBoot项目 项目结构 父pom的内容如下: <?xml version="1.0" encoding="UTF-8"?& ...
- Python+selenium之带unittest的脚本分析
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...
- Python+selenium之键盘事件
keuys()类提供键盘上所有的按键方法.send_keys()方法可以用来模拟键盘输入. from selenium import webdriver from selenium.webdriver ...
- 监测元素resize
前言 近来有需求要做分页,听起来可能有点Low. 所以我要把Low的事情做得有点逼格. 分页本身没啥,但是数据量起来了,比如十万. 要是不做点处理, 那你的页面估计爽得很,机器也爽得很. 放心,我不会 ...
- Oracle中ROWID详解
oracle数据库的表中的每一行数据都有一个唯一的标识符,或者称为rowid,在oracle内部通常就是使用它来访问数据的.rowid需要 10个字节的存储空间,并用18个字符来显示.该值表明了该行在 ...
- opencv中mat矩阵如何debug
mat img 看type:img.type() rows:img.rows cols:img.cols channels:img.channels(),channels()这是一个函数,和上面两个有 ...
- jquery Syntax error, unrecognized expression:的解决方法
原文地址 https://blog.csdn.net/flowingfog/article/details/42739773 问题: 将模板的html内容转换成jquery时报以下错误:Syntax ...
- Confirm the Ending--freecodecamp算法题目
Confirm the Ending(检查字符串结尾) 要求 判断一个字符串(str)是否以指定的字符串(target)结尾. 如果是,返回true;如果不是,返回false. 思路 利用.repla ...
- centOS下lnamp安装
首先安装apache,mysql ,最后安装php 1>apache安装 安装:yum install -y httpd 运行:/bin/systemctl start httpd.servic ...