poj3368(RMQ——ST)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16543 | Accepted: 5985 |
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
分析
#include<cstdio>
#include<iostream>
#include<cmath>
#define MAX 100010
using namespace std;
int f[MAX][];
int num[MAX];
int sum[MAX];
int n,Q,ans,a,b;
int _max(int a,int b)
{
return a>b?a:b;
}
void ST()
{
for(int i=;i<=n;i++)
f[i][]=sum[i];
int k=log((double)(n+))/log(2.0);
for(int i=;i<=k;i++)
for(int j=;j+(<<i)<=n+;j++)//n+1
f[j][i]=_max(f[j][i-],f[j+(<<(i-))][i-]);
}
int RMQ_Query(int l,int r)
{
if(l>r)return ;
int k=log((double)(r-l+))/log(2.0);
return _max(f[l][k],f[r-(<<k)+][k]);//r-(1<<k)一定要+1
}
int main()
{
while(scanf("%d",&n)!=EOF,n)
{
scanf("%d",&Q);
for(int i=;i<=n;i++)
{
scanf("%d",num+i);
if(i==)
{
sum[i]=;
continue;
}
if(num[i]==num[i-])
sum[i]=sum[i-]+;
else sum[i]=;
}
ST();
while(Q--)
{
scanf("%d%d",&a,&b);
if(a==b||num[a]==num[b])
{
cout<<b-a+<<endl;
continue;
}
int t=a;
while(num[t]==num[t-]&&t<=b)//t 必须小于 b
t++;
int cnt=RMQ_Query(t,b);
ans=_max(cnt,t-a);
printf("%d\n",ans);
}
}
return ;
}
poj3368(RMQ——ST)的更多相关文章
- [NOI2010]超级钢琴(RMQ+堆)
小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中Ai可正可负 ...
- HDU 5726 GCD(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- BZOJ 1067:[SCOI2007]降雨量(RMQ+思维)
http://www.lydsy.com/JudgeOnline/problem.php?id=1067 题意:…… 思路:首先我们开一个数组记录年份,一个记录降雨量,因为年份是按升序排列的,所以我们 ...
- POJ 3419 Difference Is Beautiful(RMQ变形)
题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...
- 2015 多校联赛 ——HDU5289(二分+ST)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- (RMQ版)LCA注意要点
inline int lca(int x,int y){ if(x>y) swap(x,y); ]][x]]<h[rmq[log[y-x+]][y-near[y-x+]+]])? rmq[ ...
- 【BZOJ 2006】2006: [NOI2010]超级钢琴(RMQ+优先队列)
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2792 Solved: 1388 Description 小 ...
- UVA - 1618 Weak Key(RMQ算法)
题目: 给出k个互不相同的证书组成的序列Ni,判断是否存在4个证书Np.Nq.Nr.Ns(1≤p<q<r<s≤k)使得Nq>Ns>Np>Nr或者Nq<Ns&l ...
- CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...
随机推荐
- SQL Server合并版本
1) 更新表(另一张表) a) 写法轻松,更新效率高: update table1 set field1=table2.field1,field2=table2.field2 from ...
- 如何进入IT行业?
其实IT行业就如一个具有标准配件的机一样是开放的行业,无论你是否科班出身,无论你是什么学历,只要你掌握相关的针对性很强的计算机知识,想在IT行业发展随时可以即插即用,这主要由IT行业的特点确PC定的. ...
- coreData数据操作
// 1. 建立模型文件// 2. 建立CoreDataStack// 3. 设置AppDelegate 接着 // // CoreDataStack.swift // CoreDataStackDe ...
- MSSQL附加数据库5120错误(拒绝访问)处理方法
一. 右键需要附加的数据库文件,弹出属性对话框,选择安全标签页. 找到Authenticated Users用户名. 如未找到,进行Authenticated Users用户名的添加. 二. 添加Au ...
- BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元
BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy Init函数 然后统计就ok B. 博弈 题 不懂 推了半天的SG..... 结果这 ...
- dbcp数据源配置杂谈
<!-- 数据源1 --> #驱动信息(driver, url, username, password)driverClassName=net.sourceforge.jtds.jdbc. ...
- ZT 螨虫的话就不要跟狗多接触,狗的寄生虫很多,还有草地,
病情分析:过敏是治不好的,只能做到避免接触.指导意见:螨虫的话就不要跟狗多接触,狗的寄生虫很多,还有草地,尤其是狗经常去的地方,草地就是螨虫的传播介质.你是过敏性体质除了被免 过敏性源外,还要增强体质 ...
- Linux cp命令使用说明
Linux cp命令使用说明 --功能说明:复制目录或文件 --命令格式:cp [参数] <文件或目录> <文件或目录> --常用参数: -R 复制目录 -i 覆盖文件之 ...
- Eclipse 在ubuntu桌面显示快捷启动以及解决Eclipse 在ubuntu中点击菜单栏不起作用的原因.
要在Eclipse中设置好之后,可以通过如下方式在周末显示快捷启动以及解决Eclipse在ubuntu高版本中点击菜单栏项不显示列表的问题 在usr/share/app-install/desktop ...
- 前端tip
background简写 参考地址 http://css.doyoe.com/ 缩写:background:url(test1.jpg) no-repeat scroll 10px 20px/50px ...