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 ...
随机推荐
- python_字符串类型
1.在python中用单引号' ',双引号'' '',三引号''' ''' 标注字符串类型. >>> name = "Alex Li" #双引号 >> ...
- 大数据学习——Linux上常用软件安装
4.1 Linux系统软件安装方式 Linux上的软件安装有以下几种常见方式: 1.二进制发布包 软件已经针对具体平台编译打包发布,只要解压,修改配置即可 2.RPM发布包 软件已经按照redhat的 ...
- BNUOJ 1575 Supermarket
Supermarket Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID ...
- zoj 1251 Box of Bricks
Box of Bricks Time Limit: 2 Seconds Memory Limit: 65536 KB Little Bob likes playing with his bo ...
- zoj 2886 Look and Say
Look and Say Time Limit: 2 Seconds Memory Limit: 65536 KB The look and say sequence is defined ...
- VIM键盘图
- POJ 1635 树的最小表示法
题目大意: 用一堆01字符串表示在树上走动的路径,0表示往前走,1表示往回走,问两种路径方式下形成的树是不是相同的树 我们可以利用递归的方法用hash字符串表示每一棵子树,然后将所有子树按照字典序排序 ...
- POJ 3177 边双连通求连通量度的问题
这道题的总体思路就是找到连通量让它能够看作一个集合,然后找这个集合的度,度数为1的连通量为k,那么需要添加(k+1)/2条边才可以保证边双连通 这里因为一个连通量中low[]大小是相同的,所以我们用a ...
- Java调用K3Cloud的密码加密算法实现登录密码检验
背景: 最近要开始做K3Cloud移动,BOS平台的移动单据收费,就想单独做移动模块,搭建环境:后台SSH2,前端Android.在手机端登录时通过Ajax方式传递用户名和密码到后台校验,后台在去K3 ...
- 匿名函数--lambda函数
匿名函数 匿名函数:为了解决一些功能很简单的需求而设计的一句话函数 (python对匿名函数支持有限,只有一些简单的条件下可以用匿名函数) 匿名函数固定格式: func = lambda *args: ...