初始化:
//n为元数的个数 int bitn=(int)(log(n)/log(2)) for (int i=0; i<n; ++i) f[i][0]=input[i]; for (int j=1; j<bitn; ++j) for (int i=0; i<n; ++i) { if (i+(1<<(j-1))>=n) break; f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); }
查询
int query(int s,int e) //查询区间[s,e]的最值 { int k=(int)((log(e-s+1.0)/log(2.0))); return max(f[s][k],f[e-(1<<k)+1][k]); }