侦察兵(区间和)

分析:f1[i,j]表示(i,j)左上角数字和,f[i,j]=f[i-1,j]+f[i,j-1]-f[i-1,j-1],+f[i,j],右下角数字和同理。

代码
const
maxn=1500;
var
f1,f2,a:array[-maxn..maxn,-maxn..maxn] of longint;
n,t,x,y,i,j:longint;

begin
assign(input,’scout.in’);reset(input);
assign(output,’scout.out’);rewrite(output);
readln(n,t);
for i:=1 to n do
for j:=1 to n do
read(a[i,j]);
for i:=1 to n do
for j:=1 to n do
f1[i,j]:=f1[i,j-1]+f1[i-1,j]-f1[i-1,j-1]+a[i-1,j-1];
for i:=n downto 1 do
for j:=n downto 1 do
f2[i,j]:=f2[i,j+1]+f2[i+1,j]-f2[i+1,j+1]+a[i+1,j+1];
for i:=1 to t do
begin
readln(x,y);
writeln(f1[x,y]+f2[x,y]);
end;
close(input);close(output);
end.

相关文章:

  • 2022-02-17
  • 2021-07-16
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-01-02
  • 2022-02-18
猜你喜欢
  • 2021-04-30
  • 2021-10-06
  • 2023-04-01
  • 2022-03-04
  • 2022-01-05
  • 2022-12-23
  • 2021-04-28
相关资源
相似解决方案