标签:des style blog http color io os ar java
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1146 Accepted Submission(s): 415
#include <iostream> #include <cstdio> #include <cmath> using namespace std; #define N 100010 int n,m; int a[N]; int dp[N][20]; int id[N]; int len[N],l[N],r[N]; void init() { int i,j; for(i=1;i<=n;i++) { dp[i][0]=len[i]; } int k=(int)(log((double)n)/log(2.0)); for(j=1;j<=k;j++) { for(i=1;i+(1<<j)-1<=n;i++) { dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } } } int query(int i,int j) { int k=(int)(log((double)(j-i+1))/log(2.0)); int res=max(dp[i][k],dp[j-(1<<k)+1][k]); return res; } int main() { int i,pos; while(scanf("%d",&n),n) { pos=0; scanf("%d",&m); for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]!=a[i-1]) { pos++; l[pos]=i; } id[i]=pos; r[pos]=i; len[pos]=r[pos]-l[pos]+1; } n=pos; init(); int x,y,xx,yy,ans1,ans2,ans3; while(m--) { int x,y; scanf("%d%d",&x,&y); xx=id[x]; yy=id[y]; if(xx==yy) ans1=ans2=y-x+1; //特殊情况、当x和y在同一个区间、答案是y-x+1 else { ans1=r[xx]-x+1; ans2=y-l[yy]+1; } ans3=0; if(xx+1<=yy-1)ans3=query(xx+1,yy-1); printf("%d\n",max(max(ans1,ans2),ans3)); } } return 0; }
RMQ [HDU 1806] Frequent values
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/hate13/p/4045707.html