先处理出每一个i位置向左最远能到达的位置L[i]。每一次询问,要找到L,R区间中的p位置,p位置左边的L[i]都是小于L的,p位置开始,到R位置,L[i]都大于等于L,对于前者,最大值为p-L,后者求一个区间最大值即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+;
int n,a[maxn],b[maxn],c[maxn],q,L[maxn];
int dp[maxn][],f[maxn]; void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=f[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
} int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
return max(dp[L][k],dp[R-(<<k)+][k]);
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b, b + n); int sz = unique(b, b + n) - b;
for(int i=;i<n;i++) a[i]=lower_bound(b, b + sz, a[i])-b+;
memset(c,-,sizeof c);
for(int i=;i<n;i++)
{
if(i==) L[]=,c[a[i]]=;
else L[i]=max(L[i-],c[a[i]]+),c[a[i]]=i;
}
for(int i=;i<n;i++) f[i]=i-L[i]+;
RMQ_init();
for(int i=;i<=q;i++)
{
int LL,RR; scanf("%d%d",&LL,&RR);
int l=LL,r=RR,p=-;
while(l<=r)
{
int mid=(l+r)/;
if(L[mid]<LL) l=mid+,p=mid;
else r=mid-;
}
int ans;
if(p==-) ans=RMQ(LL,RR);
else if(p==RR) ans=RR-LL+;
else ans=max(p-LL+,RMQ(p+,RR));
printf("%d\n",ans);
}
return ;
}

POJ 3419 Difference Is Beautiful的更多相关文章

  1. POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)

    Difference Is Beautiful Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  2. POJ 3419 Difference Is Beautiful(RMQ变形)

    题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...

  3. POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)

    题意:给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度. 析:由于n太大,无法暴力, ...

  4. POJ 2253 Difference of Clustering

    题意:给出一堆点,求从起点到终点的所有通路中相邻点的距离的最大值的最小值.(意思就是自己百度吧……) 解法:用相邻点的最大值作为权值代替路径的距离跑最短路或者最小生成树.然后我写了一个我以为是优化过的 ...

  5. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  6. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  7. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  8. Smallest Difference(POJ 2718)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6740   Accepted: 18 ...

  9. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

随机推荐

  1. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  2. C# 常用接口学习 ICollection<T>

    C# 常用接口学习 ICollection<T> 作者:乌龙哈里 时间:2015-11-01 平台:Window7 64bit,Visual Studio Community 2015 参 ...

  3. Linux 相关的error处理

    1  dpkg: error: duplicate file trigger interest for filename Notice the first and last lines of /var ...

  4. js 处理数据里面的空格

    str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...

  5. C#字符串常见操作总结

    string类常用的方法和总结小记

  6. Linux服务器建站基础-选择何种配置和安装环境项目

    我们准备在搬瓦工笔记中,边整理和分享利用Linux VPS建站过程的同时,也会分享关于用户在选择和使用VPS服务器以及网站管理运营中的一些心得和建议.经常有很多网友在很多主机论坛.QQ群众问道,有没有 ...

  7. SSH综合练习-第1天

    SSH综合练习-仓库管理系统-第一天 综合练习的整体目的: 整合应用 Struts2 .Hibernate.Spring .Mysql . jQuery Ajax.java基础知识 熟悉企业SSH 基 ...

  8. Linux权限解释

    从左至右,第一位数字代表文件所有者的权限,第二位数字代表同组用户的权限,第三位数字代表其他用户的权限. 而具体的权限是由数字来表示的,读取的权限等于4,用r表示:写入的权限等于2,用w表示:执行的权限 ...

  9. 《JS权威指南学习总结--6.6属性getter和setter》

    内容要点: 一.对象属性     对象属性是由名字.值和一组特性构成的.在ES5中,属性值可以用一个或两个方法替代,这两个方法就是getter和setter.由getter和setter定义的属性称做 ...

  10. MPI发送接收例子

    原文地址:http://blog.csdn.net/ziren235/article/details/1704203 #include"mpi.h" int main(int ar ...