bzoj 1452 二维树状数组

题意:n*m的矩阵,两种操作:

(1)修改一个格子的权值

(2)求一个子矩阵中指定权值出现的次数

对每种颜色维护一个二维树状数组

var
        a               :array[0..105,0..305,0..305] of longint;
        map             :array[0..305,0..305] of longint;
        i,j             :longint;
        ans,n,m,q       :longint;
        k,x1,y1,c,x2,y2 :longint;
function lowbit(x:longint):longint;
begin
   exit(x and (-x));
end;

procedure add(x,y,c,z:longint);
var
        t:longint;
begin
   while (x<=n) do
   begin
      t:=y;
      while (t<=m) do
      begin
         inc(a[c,x,t],z);
         inc(t,lowbit(t));;
      end;
      inc(x,lowbit(x));
   end;
end;

function sum(x,y,c:longint):longint;
var
        t,ans:longint;
begin
   ans:=0;
   while (x>0) do
   begin
      t:=y;
      while (t>0) do
      begin
         inc(ans,a[c,x,t]);
         dec(t,lowbit(t));
      end;
      dec(x,lowbit(x));
   end;
   exit(ans);
end;

begin
   read(n,m);
   for i:=1 to n do
    for j:=1 to m do
    begin
       read(map[i,j]);
       add(i,j,map[i,j],1);
    end;
   //
   read(q);
   for i:=1 to q do
   begin
      read(k);
      if (k=1) then
      begin
         read(x1,y1,c);
         add(x1,y1,map[x1,y1],-1);
         add(x1,y1,c,1);
         map[x1,y1]:=c;
      end else
      begin
         read(x1,x2,y1,y2,c);
         ans:=sum(x2,y2,c)-sum(x1-1,y2,c)-sum(x2,y1-1,c)+sum(x1-1,y1-1,c);
         writeln(ans);
      end;
   end;
end.
——by Eirlys

你可能感兴趣的:(bzoj,树状数组,bzoj)