Description
在麦克雷的面前出现了一个有n*m个格子的矩阵,每个格子用“.”或“#”表示,“.”表示这个格子可以放东西,“#”则表示这个格子不能放东西。现在他拿着一条1×2大小的木棒,好奇的他想知道对于一些子矩阵,有多少种放木棒的方案。
Solution
因为棍子是1×2的,所以很容易就能发现,两个被分割的块,除了跨越两个块摆放木棍的方案数会对答案有影响的,否则两块是互不干扰的,那么用矩阵前缀和就能随便解决,只需要再减去边缘跨块的方案数就可以啦!
Code
var
c:array[0..501,0..501] of char;
f,h,l:array[0..501,0..501] of int64;
n,m,i,j,q,x1,x2,y1,y2:longint;
ans:int64;
begin
readln(n,m);
for i:=0 to n do c[i,0]:='#';
for j:=0 to m do c[0,m]:='#';
for i:=0 to n do c[i,m+1]:='#';
for j:=0 to m do c[n+1,m]:='#';
for i:=1 to n do
begin
for j:=1 to m do
begin
read(c[i,j]);
h[i,j]:=h[i,j-1];l[i,j]:=l[i-1,j];
if c[i,j]='#' then continue;
if c[i-1,j]='.' then inc(h[i,j]);
if c[i,j-1]='.' then inc(l[i,j]);
end;
readln;
end;
for i:=1 to n do
for j:=1 to m do
begin
f[i,j]:=f[i-1,j]+f[i,j-1]-f[i-1,j-1];
if c[i,j]='#' then continue;
if c[i-1,j]='.' then inc(f[i,j]);
if c[i,j-1]='.' then inc(f[i,j]);
end;
readln(q);
for i:=1 to q do
begin
readln(x1,y1,x2,y2);
ans:=f[x2,y2]-f[x2,y1-1]-f[x1-1,y2]+f[x1-1,y1-1];
ans:=ans-h[x1,y2]+h[x1,y1-1];
ans:=ans-l[x2,y1]+l[x1-1,y1];
writeln(ans);
end;
end.