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 ...
随机推荐
- solr之windws下搭建solr服务
安装Solr 首先保证已经正确安装了Java 下载Solr,当前最新版6.1.0 Solr各个版本下载地址 Solr从6.0之后需要Java1.8所以如果使用Solr6.0及其以上版本,请确保Java ...
- luogu1856 [USACO5.5]矩形周长Picture
看到一坨矩形就要想到扫描线.(poj atantis) 我们把横边竖边分开计算,因为横边竖边其实没有区别,以下论述全为考虑竖边的. 怎样统计一个竖边对答案的贡献呢?答:把这个竖边加入线段树,当前的总覆 ...
- cf839c Journey
大水题 #include <iostream> #include <cstdio> using namespace std; int n, du[100005], hea[10 ...
- 跟初学者学习IbatisNet第二篇
在上一篇里面我们知道了什么是IbatisNet,并且知道了如何用IbatisNet进行简单的增删改查的操作,在这一篇文章里面我们主要介绍一下IbatisNet操作存储过程. 我们一般把存储过程分为两种 ...
- python之Gui编程事件绑定 2014-4-8
place() 相对定位与绝对定位 相对定位 拖动会发生变化 绝对定位不会from Tkinter import *root = Tk()# Absolute positioningButton(ro ...
- 斗地主(codevs 4610)
题目描述 Description 牛牛最近迷上了一种叫斗地主的扑克游戏. 斗地主是一种使用黑桃.红心.梅花.方片的 A 到 K 加上大小王的共 54 张牌来进行的扑克牌游戏.在斗地主中, 牌的大小关系 ...
- as3corelib Tutorial:How to Use ArrayUtil Class in Flex
ArrayUtil class contains static utility methods for manipulating and working with Arrays. Note that ...
- mysql针对转义字符的模糊搜索
由于urlencode之后会产生很多'%'符号,这个符号在mysql模糊搜索中代表任意字符,显示会出现问题,例如 name字段经过urlencode之后变成‘%E6%9D%8E%E5%87%A1’,如 ...
- codeforces 1041 d 二分
题意转化:有一些区间,要求选一些连续的区间.两两区间间隔的和要求小于H.要求区间的长度和尽可能长. 二分区间长度的和,check一下就行 #include <bits/stdc++.h> ...
- PAT (Advanced Level) 1037. Magic Coupon (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...