1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <cmath> using namespace std; int n,m,a[100005],b[100005][20],sum[100005]; inline void ST(void) { int i,j; for(j=1;(1<<j)<=n;++j) { for(i=1;i+(1<<j)-1<=n;++i) { b[i][j]=max(b[i][j-1],b[i+(1<<(j-1))][j-1]); } } return; } inline int ask(int L,int R) { if(L>R)return 0; int x=int((log(R-L+1))/log(2)); return max(b[L][x],b[R-(1<<x)+1][x]); } int main(void) { int i,pos,L,R; scanf("%d%d",&n,&m);pos=n; for(i=1;i<=n;++i) { scanf("%d",&a[i]); if(a[i]==a[i-1])b[i][0]=b[i-1][0]+1; else b[i][0]=1; } for(i=n;i>=1;--i) { if(a[i]==a[i+1])sum[i]=b[pos][0]; else pos=i,sum[i]=b[pos][0]; } ST(); for(i=1;i<=m;++i) { scanf("%d%d",&L,&R); printf("%d\n",min(R-L+1,max(max(sum[L]-b[L][0]+1,b[R][0]),ask(L+sum[L]-b[L][0]+1,R-b[R][0])))); } return 0; }
|