二维树状数组

//二维树状数组
//即行lowbit,列lowbit
var c:array[0..1030,0..1030]of longint;
    n,m,x,y,k,x1,y1,x2,y2:longint;
function lowbit(x:longint):longint;
begin
 lowbit:=x and(-x);
end;
procedure modify(x,y,delta:longint);
var i,j:longint;
begin 
 i:=x;
 while i<=n do 
  begin
   j:=y;
   while j<=n do
    begin 
     c[i,j]:=c[i,j]+delta;
     j:=j+lowbit(j);
    end;
   i:=i+lowbit(i);
  end; 
end;
function sum(x,y:longint):longint;
var ans,i,j:longint;
begin 
 ans:=0;
 i:=x;
 while i>0 do
  begin
   j:=y;
   while j>0 do
    begin
     ans:=ans+c[i,j];
     j:=j-lowbit(j);
    end;
   i:=i-lowbit(i);
  end;
 sum:=ans;
end;
begin
 read(n);
 read(m);
 while m<>3 do
  begin
   if m=1 then 
    begin
     read(x,y,k);
     x:=x+1;y:=y+1;
     modify(x,y,k);
    end;
   if m=2 then 
    begin
     read(x1,y1,x2,y2);
     inc(x1);inc(x2);inc(y1);inc(y2);
     writeln(sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1));
    end;
   read(m);
  end;
end.

你可能感兴趣的:(二维树状数组)