POJ3368Frequent values[RMQ 游程编码]
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 17581 | Accepted: 6346 |
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
//
// main.cpp
// poj3368
//
// Created by Candy on 10/8/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=1e5+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,q,l,r;
int x,last,cnt=,a[N],v[N],c[N],id[N],left[N],right[N];
int st[N][];
void initRMQ(){
memset(st,,sizeof(st));
for(int i=;i<=cnt;i++) st[i][]=c[i];
for(int j=;(<<j)<=cnt;j++)
for(int i=;i+(<<j)-<=cnt;i++)
st[i][j]=max(st[i][j-],st[i+(<<(j-))][j-]);
}
int rmq(int l,int r){
if(l>r) return ;
int k=log(r-l+)/log();
return max(st[l][k],st[r-(<<k)+][k]);
}
int main(int argc, const char * argv[]) {
while((n=read())){
q=read();
memset(c,,sizeof(c));
memset(left,,sizeof(left));
memset(right,,sizeof(right));
v[]=v[n+]=INF;
for(int i=;i<=n;i++){
v[i]=read();
if(v[i]==v[i-]){
c[cnt]++;
right[cnt]=i;
id[i]=cnt;
}else{
cnt++;
a[cnt]=v[i];
c[cnt]++;
left[cnt]=right[cnt]=i;
id[i]=cnt;
}
}
initRMQ();
//for(int i=1;i<=cnt;i++) printf("init %d %d %d %d\n",a[i],c[i],left[i],right[i]);
for(int i=;i<=q;i++){
l=read();r=read();
int ans=;
if(id[l]==id[r]) ans=r-l+;
else{
ans=max(right[id[l]]-l+,r-left[id[r]]+);
ans=max(ans,rmq(id[l]+,id[r]-));
}
printf("%d\n",ans);
}
}
//printf("\n\n\n%d %d %d %d",id[1]+1,c[2],id[10]-1,rmq(id[1]+1,id[10]-1));
return ;
}
POJ3368Frequent values[RMQ 游程编码]的更多相关文章
- poj3368Frequent values(RMQ)
http://poj.org/problem?id=3368 追完韩剧 想起这题来了 想用线段树搞定来着 结果没想出来..然后想RMQ 想出来了 算是离散吧 把每个数出现的次数以及开始的位置及结束的位 ...
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- UVA 11235Frequent values(RMQ)
训练指南P198 题意:给出一个非降序排列的整数数组a1, a2…… an,你的任务是对于一系列询问(i,j),回答ai, ai+1 ……aj 中出现的次数最多的次数 这题不仅学到了rmq的应用还学到 ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- UVA 11235 Frequent Values ---RMQ
大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...
- [poj3368]Frequent values(rmq)
题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 解题关键:统计次数,转化为RMQ问题,运用st表求解,注意边界. 预处理复杂度:$O(n\log n)$ ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)
题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...
随机推荐
- sqlserver 通用分页存储过程(转)
USE [AAA_TYDC] GO /****** Object: StoredProcedure [dbo].[proc_DataPagination] Script Date: 11/20/201 ...
- ASP.NET MVC 了解FileResult的本质
FileResult是一个基于文件的ActionResult,利用FileResult我们可以很容易地将从某个物理文件的内容响应给客户端.ASP.NET MVC定义了三个具体的FileResult,分 ...
- sqlserver 时间格式函数详细
一.时间函数 在使用存储过程,sql函数的时候,会遇到一些对时间的处理.比如时间的获取与加减.这里就用到了sql自带的时间函数.下面我列出这些函数,方便日后记忆,使用. --getdate 获取当前时 ...
- Java实现Mysql数据库自动备份
最近项目中有一个这样的需求,要求定时备份数据库.看了一些网上的资料,了解到主要思路是是使用java中的Runtime类的exec()方法,可以直接调用windows的cmd命令,参数就是Mysql的备 ...
- 从零开始学 Java - 搭建 Spring MVC 框架
没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...
- 【算法】PHP实现冒泡排序和快速排序--防遗忘
有没有这样的感觉,排序算法虽然简单,但是没看过一次,一会就又忘了,所以有必要 自己使用实际的代码运行实现,才记忆牢固,为此Mark //需求:将数组中元素,从大到小排列$a = array(11, 2 ...
- java泛型详解
http://www.cnblogs.com/lwbqqyumidi/p/3837629.html
- hadoop基本命令
1,hadoop job -list 列出Jobtracer上所有的作业 2,hadoop job -kill 任务 杀掉hadoop正在运行的任务 3,hadoop fs ...
- [ASP.NET MVC] ASP.NET Identity登入技术剖析
[ASP.NET MVC] ASP.NET Identity登入技术剖析 前言 ASP.NET Identity是微软所贡献的开源项目,用来提供ASP.NET的验证.授权等等机制.本篇文章介绍ASP. ...
- C# new关键字
在 C# 中,new 关键字可用作运算符.修饰符或约束 1.new 运算符:用于创建对象和调用构造函数.2.new 修饰符:用作修饰符时,new 关键字可以显式隐藏从基类继承的成员.3.new 约束: ...