先占坑,日后来补
贴个代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int val[N], cnt[N], num[N], le[N], ri[N];
int d[N][20];
int n, q, now;
void RMQ_init(){
for(int i = 1; i <= now; i++) d[i][0] = cnt[i];
for(int j = 1; (1 << j) <= now; j++) //这步是保证2^j <= n
for(int i = 1; i + (1 << j) - 1 <= now; i++)
d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
}
int RMQ_query(int l, int r){
int k = 0;
while((1 << (k + 1)) <= r - l + 1) k++;
return max(d[l][k], d[r - (1 << k) + 1][k]);
}
void process(){
memset(cnt, 0, (n + 5) * sizeof(int));
now = 1;
val[1] = a[1]; cnt[1] = 1; num[1] = 1; le[1] = 1; ri[1] = 1;
int i = 2;
while(a[i] == val[now]){
cnt[1]++;
num[i] = 1;
le[i] = 1;
i++;
}
for(int k = 1; k <= i - 1; k++) ri[k] = i - 1;
for(; i <= n; i++){
val[++now] = a[i];
int j = i;
while(a[j] == val[now]){
cnt[now]++;
num[j] = now;
le[j] = i;
j++;
}
for(int k = i; k <= j - 1; k++) ri[k] = j - 1;
i = j - 1;
}
RMQ_init();
}
int solve(int l, int r){
if(num[l] == num[r]) return r - l + 1;
int ans = ri[l] - l + 1;
ans = max(ans, r - le[r] + 1);
if(num[l] + 1 <= num[r] - 1) ans = max(ans, RMQ_query(num[l] + 1, num[r] - 1));
return ans;
}
int main()
{
while(~scanf("%d", &n) && n > 0){
scanf("%d", &q);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
process();
while(q--){
int l, r;
scanf("%d%d", &l, &r);
printf("%d\n", solve(l, r));
}
}
return 0;
}