二维RMQ
# include <stdio.h>
![](/Images/OutliningIndicators/None.gif)
# define N 255
# define L 8
![](/Images/OutliningIndicators/None.gif)
int n, b, k, ll, jj;
int h[N][N];
int mx[N][N][L], mn[N][N][L];
![](/Images/OutliningIndicators/None.gif)
int Max(int x, int y)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
return x > y ? x : y;
}
![](/Images/OutliningIndicators/None.gif)
int Min(int x, int y)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
return x < y ? x : y;
}
![](/Images/OutliningIndicators/None.gif)
void init(void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int i, j;
scanf("%d%d%d", &n, &b, &k);
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d", &h[i][j]);
mx[i][j][0] = mn[i][j][0] = h[i][j];
}
}
![](/Images/OutliningIndicators/None.gif)
void prep(void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int p, q, j, l;
jj = 0, ll = 1;
while (ll*2 <= b) ++jj, ll *= 2;
for (j = 1, l = 1; l <= ll; l *= 2, ++j)
for (p = 1; p+l*2 <= n+1; ++p)
for (q = 1; q+l*2 <= n+1; ++q)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mx[p][q][j] = Max(Max(mx[p][q][j-1], mx[p+l][q+l][j-1]),
Max(mx[p][q+l][j-1], mx[p+l][q][j-1]));
mn[p][q][j] = Min(Min(mn[p][q][j-1], mn[p+l][q+l][j-1]),
Min(mn[p][q+l][j-1], mn[p+l][q][j-1]));
//printf("%d %d\t%d %d\t%d\n", p, q, mx[p][q][j], mn[p][q][j], l);
}
}
![](/Images/OutliningIndicators/None.gif)
int rmq(int x, int y)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
int s, t;
s = Max(Max(mx[x][y][jj], mx[x+b-ll][y+b-ll][jj]),
Max(mx[x+b-ll][y][jj], mx[x][y+b-ll][jj]));
t = Min(Min(mn[x][y][jj], mn[x+b-ll][y+b-ll][jj]),
Min(mn[x+b-ll][y][jj], mn[x][y+b-ll][jj]));
printf("%d\t%d\n", s, t);
*/
return Max(Max(mx[x][y][jj], mx[x+b-ll][y+b-ll][jj]),
Max(mx[x+b-ll][y][jj], mx[x][y+b-ll][jj])) -
Min(Min(mn[x][y][jj], mn[x+b-ll][y+b-ll][jj]),
Min(mn[x+b-ll][y][jj], mn[x][y+b-ll][jj]));
}
![](/Images/OutliningIndicators/None.gif)
void solve(void)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int i, x, y;
for (i = 1; i <= k; ++i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d%d", &x, &y);
printf("%d\n", rmq(x, y));
}
}
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
// freopen("test.in", "r", stdin);
![](/Images/OutliningIndicators/InBlock.gif)
init();
prep();
solve();
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted on 2012-08-27 09:00
yajunw 阅读(117)
评论(0) 编辑 收藏 引用