侦察兵(区间和)

来源:互联网 发布:js a标签target 编辑:程序博客网 时间:2024/05/01 02:28

这里写图片描述

分析: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.

原创粉丝点击