1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| //二维RMQ-正方形
int fm[maxn][maxn][8];
int fn[maxn][maxn][8];
void initMaxMin()
{
for(int k=0;(1<<k)<=s;++k)
{
for(int i=1;i+(1<<(k))-1<=n;++i) for(int j=1;j+(1<<(k))-1<=m;++j)
{
if(k==0)
{
fm[i][j][0]=fn[i][j][0]=a[i][j];
}
else
{
fm[i][j][k]=
Max(
Max(
fm[i][j][k-1],
fm[i+(1<<(k-1))][j][k-1]),
Max(
fm[i][j+(1<<(k-1))][k-1],
fm[i+(1<<(k-1))][j+(1<<(k-1))][k-1])
);
fn[i][j][k]=
Min(
Min(
fn[i][j][k-1],
fn[i+(1<<(k-1))][j][k-1]),
Min(
fn[i][j+(1<<(k-1))][k-1],
fn[i+(1<<(k-1))][j+(1<<(k-1))][k-1])
);
}
}
}
}
int qMax(int ax,int ay,int bx,int by)
{
int k=log2(bx-ax+1);
return Max(
Max(
fm[ax][ay][k],
fm[bx-(1<<k)+1][ay][k]),
Max(
fm[ax][by-(1<<k)+1][k],
fm[bx-(1<<k)+1][by-(1<<k)+1][k])
);
}
int qMin(int ax,int ay,int bx,int by)
{
int k=log2(bx-ax+1);
return Min(
Min(
fn[ax][ay][k],
fn[bx-(1<<k)+1][ay][k]),
Min(
fn[ax][by-(1<<k)+1][k],
fn[bx-(1<<k)+1][by-(1<<k)+1][k])
);
}
|