题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数。

代码:

/*
rmq算法
当询问到x,y时,设在x之后并且与a[x]相同的最后一个数编号为t,那么x到t之间的数一定相同,t-x+1可能是答案;t+1到y之间的最大连续个数也可能是答案。那么我们设两个数组,hou[i]表示i之后并且与a[i]相同的最后一个数编号,num[i]表示到i为止a[i]连续出现的次数。当询问时,我们只需对两种答案取最大值即可。
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#define M 100010
using namespace std;
int a[M],num[M],hou[M],s[M][],n,m;
int main()
{
while(scanf("%d%d",&n,&m)==)
{
if(!n)break;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==a[i-])num[i]=num[i-]+;
else num[i]=;
s[i][]=num[i];
}
hou[n]=n;
for(int i=n-;i>=;i--)
if(a[i]==a[i+])hou[i]=hou[i+];
else hou[i]=i;
for(int j=;j<=;j++)
for(int i=;i<=n;i++)
{
s[i][j]=s[i][j-];
if(i+(<<(j-))<=n)
s[i][j]=max(s[i][j],s[i+(<<(j-))][j-]);
}
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
int t=hou[a];
if(t>=b)printf("%d\n",b-a+);
else
{
int ans1=t-a+;
a=t+;
int k=log(b-a+)/log();
int ans2=max(s[a][k],s[b-(<<k)+][k]);
printf("%d\n",max(ans1,ans2));
}
}
}
return ;
}

Frequent values(poj 3368)的更多相关文章

  1. poj 3368 Frequent values(段树)

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

  2. poj3368 Frequent values(线段树)

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...

  3. poj 1806 Frequent values(RMQ 统计次数) 详细讲解

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...

  4. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  5. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  6. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  7. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  8. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  9. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

随机推荐

  1. TC609 DIV1 (500)

    Problem Statement      We have balls of K different colors. The colors are numbered 0 through K-1, a ...

  2. Hibernate配置详解

    <!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1.0' ...

  3. 4 Visual Effects 视觉效果 读书笔记 第四章

    4   Visual Effects    视觉效果        读书笔记 第四章 Well, circles and ovals are good, but how about drawing r ...

  4. 临时笔记 Protection

    如果操作系统不使用处理器的多任务机制,它仍然需要为栈创建至少一个TSS 当程序通过调用门改变特权级的时候,处理器执行下面的步骤切换栈,并且执行被调用的程序在新的特权级 1. 使用目标代码段的DPL从T ...

  5. C++ class、struct区别

    一.默认访问控制不同(最主要) struct默认为public,class默认为private.这个访问控制既是指成员的默认访问属性,又指继承时默认的继承属性. 二.定义template时不同 在模版 ...

  6. swift VTables

    VTables https://github.com/apple/swift/blob/master/docs/SIL.rst#vtables decl ::= sil-vtable sil-vtab ...

  7. jsonP 现在360浏览器竟然阻止本机 jquery load一些html js什么的

    别的浏览器正常可以jquery.load本机文件,但是360浏览器不行了,缺德啊!! jsonP代码 index3.html <!DOCTYPE HTML PUBLIC "-//W3C ...

  8. 01XML文档结构

    文档结构 2.1文档结构 2.1.1文档声明及字符编码 <?xml version=“1.0” encoding=“”gb2312 standalone=“yes”?> <?  告诉 ...

  9. Getting start with dbus in systemd (02) - How to create a private dbus-daemon

    Getting start with dbus in systemd (02) 创建一个私有的dbus-daemon (session) 环境 这里我们会有两个app: app1(client),ap ...

  10. pycharm acejumpchar插件

    1链接:https://plugins.jetbrains.com/plugin/7163-emacsideas 2将其pycharm路径下:\.PyCharm2017.1\config\plugin ...