Difference Is Beautiful

Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

Mr. Flower's business is growing much faster than originally planned. He has now become the CEO of a world-famous beef corporation. However, the boss never lives a casual life because he should take charge of the subsidiary scattered all over the world. Every year, Mr. Flower needs to analyze the performance reports of these subsidiary companies.

Mr. Flower has N companies, and he numbered them with 0 to N – 1. All of the companies will give Mr. Flower a report about the development each year. Among all of the tedious data, only one thing draws Mr. Flower's attention – the turnover. Turnover of a company can be represented as an integer Pi: positive one represents the amount of profit-making while negative for loss-making.

In fact, Mr. Flower will not be angry with the companies running under deficit. He thinks these companies have a large room for future development. What dissatisfy him are those companies who created the same turnover. Because in his eyes, keeping more than one companies of the same turnover is not necessary.

Now we know the annual turnover of all companies (an integer sequence Pi, the ith represents the turnover of the ith company this year.). We say a number sequence is perfect if all of its numbers are different from each other. Mr. Flower wants to know the length of the longest consecutive perfect sequence in a certain interval [LR] of the turnover sequence, can you help him?

Input

The first line of the input contains two integers N and MN is the number of companies. M is the number of queries. (1 ≤ NM ≤ 200000). The second line contains N integer numbers not exceeding 106 by their absolute values. The ith of them represents the turnover of the ith company this year. The followingM lines contain query descriptions, each description consists of two numbers: LR (0 ≤ L ≤ R ≤ N – 1) and represents the interval that Mr. Flower concerned.

Output

The output contains M lines. For each query, output the length of the longest consecutive perfect sequence between [LR]  

Sample Input

9 2
2 5 4 1 2 3 6 2 4
0 8
2 6

Sample Output

6
5
//模拟的方法:

#include<cstdio>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int up=;
int p,q,i;
bool vis[*up+];//记录有没有访问过
int dis[*up+];//到该点i的最长连续长度
int f[*up+]; //记录前一个(最长连续串的终点位置+1),即f[i]-1
int a[*up+];// 读入使用的数组 int main()
{
scanf("%d%d",&p,&q);
for(i=;i<=p;i++)
scanf("%d",&a[i]);
memset(vis,,sizeof(vis));
f[]=;
dis[]=;
for(i=;i<=p;i++)
{
if (!vis[a[i]+up])//如果没有被访问过
{
dis[i]=dis[i-]+;//那么长度+1
vis[a[i]+up]=;//标记访问过
f[i]=f[i-];//前一个最长串的终点+1
} else
{
int start=i-dis[i-];
while(a[start]!=a[i])//将重复的点前的点全部还原成未访问
{
vis[a[start]+up]=;
start++;
}
dis[i]=i-start;//从重复点后面的都可以利用
f[i]=i;//前一个连续串的终点是i-1
}
}
for(;q>;q--)
{
int l,r,ans;
scanf("%d%d",&l,&r);
l++; r++;
ans=;
while(r-l+>ans)//根据f数组来确定答案
{
ans=max(ans,min(dis[r],r-l+));
r=f[r]-;
}
printf("%d\n",ans);
}
return ;
}

转自:http://www.cnblogs.com/zufezzt/p/5740789.html

先处理出每一个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(RMQ+二分 或者 模拟)的更多相关文章

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

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

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

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

  3. POJ 3419 Difference Is Beautiful

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

  4. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  5. *HDU3486 RMQ+二分

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. poj 3111 K Best 最大化平均值 二分思想

    poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...

  7. hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...

  8. hdu 3486 Interviewe (RMQ+二分)

    Interviewe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

随机推荐

  1. 如何直观的解释back propagation算法?

    转自:知乎-https://www.zhihu.com/question/27239198 作者:匿名用户链接:https://www.zhihu.com/question/27239198/answ ...

  2. spring AOP 代理机制、执行过程、四种实现方式及示例详解

    1.加载过程 spring首先检测配置文件中的代理配置,然后去加载bean; 如果配置文件中没有配置代理,自然代理不会生效,如果配置了代理,但是代理还没有生效,那么有可能是加载顺序的问题,即在检测到代 ...

  3. JPA 系列教程10-双向一对一关联表

    双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...

  4. RPC框架基本原理(一):服务注册

    什么是RPC框架 RPC整个过程涉及四类对象:客户端.客户端代理.服务端和服务端代理.RPC要求客户端和服务端之间约定好调用接口和传输格式(如JSON,Xml等),客户端在调用该接口时,由客户端的代理 ...

  5. mysql主从复制的配置总结

    首先确定安装配置的环境 centOS7,mysql5.6 在配置之前要保证的几个点 1.系统防火墙,如果只是测试,可以关闭防火墙,如果不是测试,请开发需要使用的端口号,如3306: 开放端口 fire ...

  6. H5的新应用-拖到页面上的元素

    -------------------------- <script type="text/javascript">                        // ...

  7. 通过CSS实现各种方向的三角形

    #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid tr ...

  8. LeetCode OJ 110. Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. nefu 197 关键字检索(kmp算法)

    Description 在信息检索系统中一个很重要的环节就是关键字符串的查找,从而很多对自己有用的信息.给你一个很长的一段文字, 和一个关键信息字符串,现在要你判断这段文字里面是否有关键字符串. In ...

  10. Linux添加用户user到用户组group

    添加用户:useradd niot 添加到组:usermod -a -G root niot 改密码:passwd niot 切换:su - niot 查看用户组:groups 将一个用户添加到用户组 ...