poj1195 二维树状数组【pascal】

来源:互联网 发布:mac字典扩展 编辑:程序博客网 时间:2024/06/06 12:58

二维树状数组模板=。=

var        tt,n,x,y,l,b,r,t,z:longint;        i,j               :longint;        c                 :array[0..1025,0..1025] of longint;function lowbit(x:longint):Longint;begin   exit(x and (-x));end;procedure add(x,y,z:longint);var        t1,t2:longint;begin   while (x<=n) do   begin      t1:=y;      while (t1<=n) do       begin          inc(c[x,t1],z);          inc(t1,lowbit(t1));       end;       inc(x,lowbit(x));   end;end;function sum(x,y:longint):longint;var        ans,y1:longint;begin   ans:=0;   while (x>0) do   begin      y1:=y;      while (y1>0) do      begin         inc(ans,c[x,y1]);         dec(y1,lowbit(y1));      end;      dec(x,lowbit(x));   end;   exit(ans);end;function find(l,b,r,t:longint):Longint;begin   exit(sum(r,t)-sum(l-1,t)-sum(r,b-1)+sum(l-1,b-1));end;begin   read(tt);   while (tt<>3) do   begin      if (tt=0) then      begin         read(n);         fillchar(c,sizeof(c),0);      end else      if (tt=1) then      begin          read(x,y,z);          inc(x);inc(y);          add(x,y,z);      end else      if (tt=2) then      begin           read(l,b,r,t);           inc(l);inc(b);inc(r);inc(t);           writeln(find(l,b,r,t));      end;      read(tt);   end;end.
——by Eirlys

0 0
原创粉丝点击