# 分析

$tree\left[k\right].right=tree\left[k\ast 2\right].right+tree\left[k\ast 2+1\right].left$$tree[k].right=tree[k∗2].right+tree[k∗2+1].left$

$tree\left[k\right].maxs=max\left(tree\left[k\ast 2\right].maxs,tree\left[k\ast 2+1\right].maxs,tree\left[k\ast 2\right].right+tree\left[k\ast 2+1\right].left\right)$$tree[k].maxs=max(tree[k∗2].maxs,tree[k∗2+1].maxs,tree[k∗2].right+tree[k∗2+1].left)$

# 代码

#include <cstdio>
#include <cstring>
#define g (k<<1)
using namespace std;
int n,m,b[100001];
struct treen{int left,right,w;}tree[300001];
int max(int a,int b){return (a>b)?a:b;}
int min(int a,int b){return (a<b)?a:b;}
void build(int k,int l,int r){
if (l==r) tree[k].left=tree[k].right=tree[k].w=1;
else {
int mid=(l+r)>>1;
build(g,l,mid); build(1+g,mid+1,r);
if (tree[g].left==mid-l+1&&b[mid]==b[mid+1])
tree[k].left=tree[g].right+tree[1+g].left;
else tree[k].left=tree[g].left;
if (tree[1+g].right==r-mid&&b[mid]==b[mid+1])
tree[k].right=tree[g].right+tree[1+g].left;
else tree[k].right=tree[1+g].right;
if (b[mid]==b[mid+1])
tree[k].w=max(max(tree[g].w,tree[1+g].w),tree[g].right+tree[1+g].left);
else tree[k].w=max(tree[g].w,tree[1+g].w);
}
}
int find(int k,int l,int r,int x,int y){
if (l>y||r<x||r<l) return 0;
if (l>=x&&r<=y) return tree[k].w;
int mid=(l+r)>>1; int m=0,m1=0,m2=0;
if (b[mid]==b[mid+1]) m=min(mid-x+1,tree[g].right)+min(y-mid,tree[1+g].left);
if (x<=mid) m1=find(g,l,mid,x,y);
if (y>mid) m2=find(1+g,mid+1,r,x,y);
return m=max(m,max(m1,m2));
}
int main(){
while (scanf("%d",&n)&&n){
scanf("%d",&m); memset(tree,0,sizeof(tree));
for (int i=1;i<=n;i++) scanf("%d",&b[i]);
build(1,1,n); int x,y;
while (m--){
scanf("%d%d",&x,&y);
printf("%d\n",find(1,1,n,x,y));
}
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120