Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
哪位大牛给个RMQ的代码?我的不知道问题在哪?#include<iostream> #include<cmath> #define N 100002 using namespace std; int dpmax[N][18],n,q; struct Node{ int data,left,right; }; Node node[N]; int max(int x,int y){ if(x>y) return x; return y; } void create_Dpmax(){ int i,j,l,r,temp; for(i=1;i<=n;i++) dpmax[i][0]=1; for(j=1;j<=log((double)(n+1))/log(2.0);j++) for(i=1;i+(1<<j)-1<=n;i++){ int mid=i+(1<<(j-1))-1; temp=max(dpmax[i][j-1],dpmax[i+(1<<(j-1))][j-1]); if(node[mid].data==node[mid].data){ if(node[mid].left<i) l=1<<(j-1); else l=i+(1<<(j-1))-node[mid].left; if(node[mid].right>i+(1<<j)-1) r=1<<(j-1); else r=node[mid].right-(i+(1<<(j-1))-1); dpmax[i][j]=max(temp,l+r); } else dpmax[i][j]=temp; } } int getmax(int a,int b){ int k=(int)(log((double)(b-a+1))/log(2.0)); return max(dpmax[a][k],dpmax[b-(1<<k)+1][k]); } int main(){ int i,x,y; while(scanf("%d",&n),n!=0){ scanf("%d",&q); for(i=1;i<=n;i++) scanf("%d",&node[i].data); node[1].left=1; for(i=2;i<=n;i++) if(node[i-1].data==node[i].data) node[i].left=node[i-1].left; else node[i].left=i; node[n].right=n; for(i=n-1;0<i;i--) if(node[i].data==node[i+1].data) node[i].right=node[i+1].right; else node[i].right=i; /* for(i=1;i<=n;i++) cout<<node[i].data<<" "<<node[i].left<<" "<<node[i].right<<endl; // */ node[i-1].right=i-1; create_Dpmax(); for(i=1;i<=q;i++){ scanf("%d %d",&x,&y); printf("%d\n",getmax(x,y)); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator