1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int maxn=100005; 7 8 int count[maxn]; 9 int num[maxn],ll[maxn],rr[maxn]; 10 int tot; 11 int d[maxn][50]; 12 13 int rmq (int l,int r){ 14 int k=0; 15 while ( (1<<(k+1)) <= r-l+1 ) k++; 16 return max(d[l][k],d[r-(1<<k)+1][k]); 17 } 18 19 int main (){ 20 int n,q; 21 while (cin>>n&&n){ 22 cin>>q; 23 tot=0; 24 memset (count,0,sizeof count); 25 int temp=99999999; 26 ll[tot]=0; 27 for (int i=1;i<=n;i++){ 28 int x; 29 cin>>x; 30 if (x!=temp){ 31 temp=x; 32 rr[tot]=i-1; 33 tot++; 34 ll[tot]=i; 35 } 36 count[tot]++; 37 num[i]=tot; 38 } 39 rr[tot]=n; 40 for (int i=1;i<=tot;i++) d[i][0]=count[i];//cout<<count[i]<<" ";cout<<endl; 41 for (int j=1;(1<<j)<=tot;j++){ 42 for (int i=1;i+(1<<(j-1))<=tot;i++){ 43 d[i][j]=max (d[i][j-1],d[i+(1<<(j-1))][j-1]); 44 } 45 } 46 while (q--){ 47 int l,r; 48 cin>>l>>r; 49 int ans=0; 50 if (num[l]==num[r]){ 51 cout<<r-l+1<<endl; 52 continue ; 53 } 54 //if (rr[num[l]]<=r) 55 ans=max (ans,rr[num[l]]-l+1); 56 //if (ll[num[r]]>=l) 57 ans=max (ans,r-ll[num[r]]+1);//cout<<num[l]+1<<" "<<num[r]-1<<"err"<<endl; 58 if (num[l]+1<=num[r]-1) 59 ans=max (ans,rmq (num[l]+1,num[r]-1));//cout<<rmq (num[l]+1,num[r]-1)<<"aaa"<<endl; 60 cout<<ans<<endl; 61 } 62 } 63 return 0; 64 }
UVA 11235 Frequent values,布布扣,bubuko.com
时间: 2024-12-29 06:49:26