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),回答序列中出现次 ...
随机推荐
- log4cpp安装使用
1. 主页:http://log4cpp.sourceforge.net“Log4cpp is library of C++ classes for flexible logging to files ...
- 事务回滚 DEMO
因为有些事物回滚 查询的时候 可能查出来空值 我们肯定不愿意把空值添加数据库里面 一般基本的是这么写 if (object_id('add_T_Disclose_DiscloseList', 'P' ...
- Codevs 1860 最大数
题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. 第二行n个正整数,空格隔开 ...
- CMDB资产采集方案
CMDB资产采集方案 CMDB 资产采集的方案总共有四种 Agent SSH类 Saltstack Puttet 方案设计,从性能上考虑 下面前三种是用Python开发的,目标是兼容三种采集方式的软件 ...
- android 通过adb 和 ndk调试堆栈
打开终端 , 输入以下命令, armeabi是应用编译好的.so库的路径 adb logcat|ndk-stack -sym ./armeabi/ 如果堆栈报错,会弹出报错内容. 如下: C:\Use ...
- c++基本配置属性页
怎么调试一个项目. 需要配置好环境. 在一个release版本的环境中,调试要用release-debug版本,一般不用debug版本. 配置类型一般不变.
- NOIP模拟赛 czy的后宫
[题目描述] czy要妥善安排他的后宫,他想在机房摆一群妹子,一共有n个位置排成一排,每个位置可以摆妹子也可以不摆妹子.有些类型妹子如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看了.假定每种 ...
- Ubuntu 下安装mysqlclient报错
pip3 install mysqlclient 报错信息 问题描述: Complete output from command python setup.py egg_info: /bin/sh: ...
- Ubuntu 18.04 下用命令行安装Sublime
介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...
- mysql的字符串连接符
以前用SQL Server 连接字符串是用“+”,现在数据库用mysql,写个累加两个字段值SQL语句居然不支持"+",郁闷了半天在网上查下,才知道mysql里的+是数字相加的操作 ...