POJ 3368 Frequent values RMQ ST算法/线段树
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 15229 | Accepted: 5550 |
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
题意:
给你一个非递减序列。有m次询问,每次询问区间之间出现最多的数字出现的次数。
题解:
RMQ/线段树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
using namespace std ;
typedef long long ll;
#define inf 100000
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//******************************************************************
struct ss{
int l,r,num,v;
}a[];
int counts[];
int dp[][],n,m,p;
void rmq_init()
{
memset(dp,,sizeof(dp));
for(int i=;i<=p;i++){
dp[i][]=counts[i];
}
int k=floor(log((double)n+)/log(2.0));
for(int j=;j<=k;j++)
{
for(int i=;i+(<<j)-<=p;i++)
{
int oo=i+(<<(j-));
dp[i][j]=max(dp[i][j-],dp[oo][j-]);
}
}
}
int RMQ(int x,int y)
{
int oo=floor(log((double)y-x+)/log(2.0));
return max(dp[x][oo],dp[y-(<<(oo))+][oo]);
}
void work()
{
int l,r;
for(int i=;i<=m;i++)
{
scanf("%d%d",&l,&r);
if(a[l].num==a[r].num){
cout<<r-l+<<endl;
}
else {
int ans=a[l].r-l+;
ans=max(r-a[r].l+,ans);
l=a[l].num+;
r=a[r].num-;
if(l<=r) ans=max(ans,RMQ(l,r));
cout<<ans<<endl;
}
}
}
void init()
{ int f=,r=,l=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].v);
if(f&&a[i].v!=a[i-].v)
{
counts[p]=r-l+;
for(int j=l;j<=r;j++)
a[j].num=p,a[j].l=l,a[j].r=r;
r++;
p++;
l=r;
}
else if(f){
r++;
}
if(f==)
{
l=;
r=,f=;
p=;
} }
counts[p]=r-l+;
for(int j=l;j<=r;j++)
a[j].num=p,a[j].l=l,a[j].r=r;
// for(int i=1;i<=n;i++)cout<<counts[i]<<" ";
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==)break;
init();
rmq_init();
work();
}
return ;
}
RMQ
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
using namespace std ;
typedef long long ll;
#define inf 100000
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//******************************************************************
struct ss
{
int l,r,v,num;
}tr[*],a[];
int counts[];
int n,m,p;
void build(int k,int s,int t)
{
tr[k].l=s;
tr[k].r=t;
if(s==t){
tr[k].v=counts[s];
return ;
}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
tr[k].v=max(tr[k<<].v,tr[k<<|].v);
}
int ask(int k,int s,int t)
{
if(s==tr[k].l&&t==tr[k].r)
{
return tr[k].v;
}
int mid=(tr[k].l+tr[k].r)>>;
if(t<=mid)return ask(k<<,s,t);
else if(s>mid)return ask(k<<|,s,t);
else {
return max(ask(k<<,s,mid),ask(k<<|,mid+,t));
}
}
void init()
{
int f=,l=,r=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].v);
if(f&&a[i].v!=a[i-].v)
{
counts[p]=r-l+;
for(int j=l;j<=r;j++)a[j].l=l,a[j].r=r,a[j].num=p; r++;
l=r;
p++;
}
else if(f){
r++;
}
if(!f)
{
f=l=;
r=p=;
}
}
counts[p]=r-l+;
for(int j=l;j<=r;j++)a[j].l=l,a[j].r=r,a[j].num=p;
///for(int i=1;i<=p;i++)cout<<counts[i]<<" ";
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
p=;
if(n==)break;
init();
build(,,p);
// cout<<p<<endl;
int x,y;
for(int i=;i<=m;i++)
{ scanf("%d%d",&x,&y);
if(a[x].num==a[y].num){
cout<<y-x+<<endl;
}
else {
int ans=a[x].r-x+;
ans=max(ans,y-a[y].l+);
x=a[x].num+;
y=a[y].num-;
if(x<=y)ans=max(ask(,x,y),ans);
cout<<ans<<endl;
}
}
}
return ;
}
线段树
POJ 3368 Frequent values RMQ ST算法/线段树的更多相关文章
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- POJ 3368 Frequent values RMQ 训练指南 好题
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- poj 3368 Frequent values -Sparse-Table
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16537 Accepted: 5981 Description You ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
随机推荐
- JavaScript基础对象---Number
一.创建Number实例对象 /** * new Number(value); * value 被创建对象的数字值 * * Number 对象主要用于: 如果参数无法被转换为数字,则返回 NaN. 在 ...
- 给Django中的url起名字
url反转 =>reverse 1.from django.shortcuts import reverse 2. 利用reverse函数对URL名称进行反转 reverse(url名称 ...
- 事务场景中,抛出异常被catch后,如果需要回滚,一定要手动回滚事务
Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是checked异常,默认情况下数据库操作还是会 ...
- 大数据学习——安装zooleeper
1 alt+p,上传zookeeper-3.4.5.tar.gz 2 解压安装包 ,安装在apps目录下 tar -zxvf zookeeper-3.4.5.tar.gz -C apps 3 删除zo ...
- Struts2的Action配置的各项默认值
1 如果没有为action指定class,默认是ActionSupport 2 如果没有为action指定method,默认执行action中的execute()方法 3 如果没有指定result的n ...
- 【java基础 4】树形结构数据呈现的非递归算法(循环)实现
一.基本概况 上一篇博客介绍到用递归实现树结构数据的查找,那么这篇博客,我就结合自己对于树的理解,然后用一种非递归的方式进行树结构数据的处理.首先,改造数据库表设计,加入度的概念: 首先,layer的 ...
- 【ITOO 3】.NET 动态建库建表:实用EF框架提供的codeFirst实现动态建库
导读:在上篇博客中,介绍了使用SQL字符拼接的方式,实现动态建库建表的方法.这样做虽然也能够实现效果,但是,太麻烦,而且,如果改动表结构,字段的话,会对代码修改很多.但是EF给我们提供了一种代码先行的 ...
- gridview读取Excel文件中的数据,并将其导入数据库
原文发布时间为:2008-10-16 -- 来源于本人的百度文章 [由搬家工具导入] //将需要导入的文件上传到服务器 string filePath = "" ...
- PatentTips - Reducing Write Amplification in a Flash Memory
BACKGROUND OF THE INVENTION Conventional NAND Flash memories move data in the background to write ov ...
- ACM-ICPC 2018 沈阳赛区网络预赛 G 容斥原理
https://nanti.jisuanke.com/t/31448 解析 易得an=n*n+n O(1)得到前n项和 再删除与m不互素的数 我们用欧拉函数求出m的质因数 枚举其集合的子集 进行 ...