1 D
void initRMQ(int n)
{
int MAX=(int)log2(n);
FOR(i,1,n)
{
f[i][0]=a[i];
}
FOR(j,1,MAX)
{
FOR(i,1,n) if (i+(1<<(j-1))<=n)
{
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
}
int getRMQ(int x,int y)
{
int k=(int)log2(y-x+1);
return max(f[x][k],f[y-(1<<(k))+1][k]);
}
two D
void initRMQ(int n,int m)
{
// leave out init a[i][j][0][0]
int Maxn=(int)log2(n);
int Maxm=(int)log2(m);
FOR(ii,0,Maxn)
{
FOR(jj,0,Maxm)
{
if (ii+jj)
{
FOR(i,1,n) if (i+(1<<ii)-1<=n)
{
FOR(j,1,m) if (j+(1<<jj)-1<=m)
{
if (ii==0)
{
f[i][j][ii][jj]=max(f[i][j][ii][jj-1],f[i][j+(1<<(jj-1))][ii][jj-1]);
}
else
{
f[i][j][ii][jj]=max(f[i][j][ii-1][jj],f[i+(1<<(ii-1))][j][ii-1][jj]);
}
}
}
}
}
}
}
int getRMQ(int x1,int y1,int x2,int y2)//x1,y1:upleft
{
int k1=(int)log2((x2-x1+1));
int k2=(int)log2((y2-y1+1));
int tmp1=f[x1][y1][k1][k2];
int tmp2=f[x2-(1<<k1)+1][y2-(1<<k2)+1][k1][k2];
int tmp3=f[x2-(1<<k1)+1][y1][k1][k2];
int tmp4=f[x1][y2-(1<<k2)+1][k1][k2];
int maxx=max(max(tmp1,tmp2),max(tmp3,tmp4));
return maxx;
}