预处理O(nlgn) 查询O(1)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int value[maxn],cnt[maxn];//开得稍大了,应该maxn / aver_cnt,或者vector
int num[maxn],lp[maxn],rp[maxn];//num[p]表示p所在的段,的编号,和所在段的左右边界
int d[maxn][18];
int sz = 0;
void st_init()//在cnt上求最大值
{
for (int i = 1; i < sz ; i ++) {
d[i][0] = cnt[i];
}
for (int j = 1; (1 << j) < sz; j ++) {
for (int i = 1; i + (1 << j) < sz; i ++) {
d[i][j] = max(d[i][j - 1],d[i + (1 << (j - 1))][j - 1]);
}
}
}
int st_query(int l,int r)
{
if(l > r) return 0;
int k = 0;
while((1 << (1 + k)) <= r - l + 1) k ++;
return max(d[l][k],d[r - (1 << k) + 1][k]);
}
int main()
{
int n,q;
while (scanf("%d",&n) != EOF && n) {
scanf("%d",&q);
memset(value, 0, sizeof(value));
memset(cnt, 0, sizeof(cnt));
memset(d, 0, sizeof(d));
a[0] = -maxn;a[n + 1] = maxn;
sz = 0;
for (int i = 1; i <= n; i ++) {
scanf("%d",&a[i]);
}
for (int i = 1; i <= n + 1; i ++) {
if(a[i - 1] != a[i]){
value[sz ++] = a[i - 1];
}
cnt[sz] ++;
}
int p = 1;//1 ~ (sz - 1)
int t = 1;
for (int i = 1; i <= n; i ++) {
if(value[p] != a[i]) {
t += cnt[p];
p ++;
}
num[i] = p;
lp[i] = t;
rp[i] = t + cnt[p] - 1;
}
st_init();
int l,r,ans;
for (int i = 0; i < q; i ++) {
scanf("%d%d",&l,&r);
if(num[l] == num[r]) printf("%d\n",r - l + 1);
else{
ans = max(rp[l] - l + 1,r - lp[r] + 1);
printf("%d\n",max(ans,st_query(num[l] + 1,num[r] - 1)));
}
}
}
return 0;
}