一句话,多次查询区间的众数的次数

注意多组数据!!!!

RMQ方法:

预处理 i 及其之前相同的数的个数

再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同的一段 (RMQ)

但是要注意 to[i] 大于查询范围的情况, 以及RMQ时 x < y 的情况!!

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
const int maxd=;
int n,q;
int a[maxn];
int sum[maxn];
int to[maxn];
inline bool init()
{
if(!~scanf("%d%d",&n,&q) || !n) return false;
for(int i=;i<=n;i++) scanf("%d",a+i);
sum[]=;
for(int i=;i<=n;i++)
if(a[i-]^a[i]) sum[i]=;
else sum[i]=sum[i-]+;
to[n]=n;
for(int i=n-;i;i--)
to[i] = a[i]^a[i+]? i : to[i+];
return true;
}
int dp[maxn][maxd];
void ST()
{
for(int i=;i<=n;i++) dp[i][] = sum[i];
int k=;
while( (<<k+) <= n ) k++;
for(int j=;j<=k;j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j] = max( dp[i][j-] , dp[i+(<<j-)][j-] ); // j-1 moved !!!
}
inline int RMQ(int x,int y)
{
if (x>y) return -INF; // INF here to make the case extinct!!
int k=;
while( (<<k+) <= (y-x+) ) k++;
return max(dp[x][k] , dp[y-(<<k)+][k]);
}
int main()
{
freopen("fre.in","r",stdin);
freopen("fre.out","w",stdout);
while(init())
{
ST();
for(int i=;i<=q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",max(sum[min(to[x],y)]-sum[x]+ , RMQ(to[x]+,y)));
}
}
return ;
}

线段树方法:

不急,懒得写了。。

[RMQ] [线段树] POJ 3368 Frequent Values的更多相关文章

  1. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  2. POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】

    传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  3. POJ 3368 Frequent values 线段树与RMQ解法

    题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...

  4. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...

  5. POJ 3368 Frequent values (基础RMQ)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14742   Accepted: 5354 ...

  6. poj 3368 Frequent values(段树)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 ...

  7. poj 3368 Frequent values -Sparse-Table

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16537   Accepted: 5981 Description You ...

  8. Poj 3368 Frequent values

    /* 线段树区间合并 维护几个信息 到时候乱搞一下就好了 开始T了 有一种情况可以不用递归 直接算出来 */ #include<iostream> #include<cstdio&g ...

  9. POJ 3368 Frequent values(线段树区间合并)

    [题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...

随机推荐

  1. hdu 2844 Coins (多重背包)

    题意是给你几个数,再给你这几个数的可以用的个数,然后随机找几个数来累加, 让我算可以累加得到的数的种数! 解题思路:先将背包初始化为-1,再用多重背包计算,最后检索,若bb[i]==i,则说明i这个数 ...

  2. 教你50招提升ASP.NET性能(九):显式的使用using语句减少内存泄露

    (15)Reduce memory leaks dramatically with the “using” statement 招数15: 显式的使用using语句减少内存泄露 If a type i ...

  3. 将string转换成UTF8在进行请求

    在请求服务器时,如果参数中带有中文字符.就会报参数格式错误,需要将其转换成UTF8 @interface NSString (NSURLUtilities) /* Adds all percent e ...

  4. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 44 bytes) in

    最近莫名出现这个错误. 研究一下原因很奇葩呢. 原因:sql获取数据库中数据,取出数据赋给变量,数据太多,超过memory_limit内存设置了. 解决方法:设置memory_limit不建议.优化代 ...

  5. Codeforces Round #324 (Div. 2) C. Marina and Vasya 贪心

    C. Marina and Vasya Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/pr ...

  6. Aizu 2305 Beautiful Currency DP

    Beautiful Currency Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  7. [React Fundamentals] Introduction to Properties

    This lesson will teach you the basics of setting properties in your React components. class App exte ...

  8. [Bootstrap] 6. Navigation

    Too Much Navigation? How many of Bootstrap's Navigation Components can we use on a page? 0 1 More th ...

  9. 王帅:深入PHP内核

    [问底]王帅:深入PHP内核(三)——内核利器哈希表与哈希碰撞攻击   [问底]王帅:深入PHP内核(二)——SAPI探究   [问底]王帅:深入PHP内核(一)——弱类型变量原理探究  

  10. 垃圾回收算法手册:自动内存管理的艺术 BOOK

    垃圾回收算法手册:自动内存管理的艺术 2016-03-18 华章计算机 内容简介 PROSPECTUS 本书是自动内存管理领域的里程碑作品,汇集了这个领域里经过50多年的研究沉积下来的最佳实践,包含当 ...