分情况讨论,因为有可能一个连续的地方被分割开,
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
#define MAXN 100100
int num2[MAXN],num[MAXN];
int dp[MAXN][100];
void RMQ(int n)
{
int i,j,k,m;
k = (int)(log((double)n)/log(2.0));
for(i = 1;i <= n;i++)
dp[i][0] = num[i];
for(j = 1;j <= k;j++) {
for(i = 1;i+(1<<j)-1 <= n;i++) {
m = i + (1<<(j-1));
dp[i][j] = max(dp[i][j-1],dp[m][j-1]);
}
}
}
int rmq_max(int i,int j)
{
if (i > j) return 0;
int k = (int)(log((double)(j-i+1))/log(2.0));
return max(dp[i][k],dp[j-(1<<k)+1][k]);
}
int work()
{
int n,q,i,l,r,t;
scanf("%d",&n);
if (n == 0) return 0;
scanf("%d",&q);
memset(num, 0, sizeof(num));
memset(dp, 0, sizeof(dp));
num2[0] = MAXN;
for(i = 1;i <= n;i++) {
scanf("%d",&num2[i]);
if (num2[i] == num2[i-1]) num[i] = num[i-1]+1;
else num[i] = 1;
}
RMQ(n);
for(i = 1;i <= q;i++) {
scanf("%d%d",&l,&r);
t = l;
while (num2[t] == num2[t-1] && t <= r) t++;
printf("%d\n",max(t-l,rmq_max(t,r)));
}
return 1;
}
int main()
{
while(work());
return 0;
}