贴完代码就滚,杭州邀请赛被虐的节奏-_-#
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 100200
using namespace std;
int num[maxn],ic,last_num;
int pos[maxn];
int a[maxn],n;
int sum[maxn];
int x,y,m;
int dp[maxn][20];
bool init()
{
ic=0;last_num=10000000;
scanf("%d",&n);
if(n==0)return 0;
scanf("%d",&m);
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]!=last_num)
{
++ic;
num[ic]++;
last_num=a[i];
}
else
{
num[ic]++;
}
pos[i]=ic;
}
for(int i=1;i<=ic;i++)sum[i]=num[i]+sum[i-1];
return 1;
}
void RMQ_init()
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=ic;i++)
dp[i][0]=num[i];
for(int j=1;(1<<j)<=ic;j++)
{
for(int i=1;i+(1<<j)-1<=ic;i++)
{
dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
int RMQ(int a,int b)
{
int len=0;
while(a+(1<<(len+1))-1<=b)len++;
return max(dp[a][len],dp[b-(1<<len)+1][len]);
}
void solve()
{
int p1,p2,temp;
RMQ_init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
p1=pos[x];
p2=pos[y];
if(p1!=p2)
temp=max(sum[p1]-x+1,y-sum[p2-1]);
else
temp=y-x+1;
if(p1+1<=p2-1)
{
temp=max(temp,RMQ(p1+1,p2-1));
}
printf("%d\n",temp);
}
}
int main()
{
//freopen("3286.in","r",stdin);
while(init())
solve();
return 0;
}