[vijos P1512] SuperBrother打鼹鼠

这周好好码树状数组和线段树!!之前没写过二维树状数组,凭借一维的思路居然写了个比较像模像样的东西出来,原来我没那么脑残。唯一要注意的就是getsum四个矩形加减的边界条件,这里看了别人标程才意识到错误QAQ!

program vijos_p1512;
var f,t:array[0..1030,0..1030] of longint;
    n,m,i,j,x,y,k,x1,x2,y1,y2:integer;
    ans:longint;
function lowbit(x:longint):longint;
begin
  lowbit:=x and (-x);
end;

procedure add(x,y,delta:longint);
var ty:longint;
begin
  ty:=y;
  while (x<=n) do
    begin
      while (y<=n) do
        begin
          inc(f[x,y],delta);
          y:=y+lowbit(y);
        end;
      x:=x+lowbit(x);
      y:=ty;
    end;
end;

function getsum(x,y:longint):longint;
var ty,p:longint;
begin
  ty:=y;p:=0;
  while (x>0) do
    begin
      while (y>0) do
        begin
          inc(p,f[x,y]);
          y:=y-lowbit(y);
        end;
      x:=x-lowbit(x);
      y:=ty;
    end;
  getsum:=p;
end;

begin
  readln(n);
  read(m);
  while m<>3 do
    begin
      if m=1 then
        begin
          readln(x,y,k);
          add(x+1,y+1,k);
        end;
      if m=2 then
        begin
          readln(x1,y1,x2,y2);
          ans:=getsum(x2+1,y2+1)-getsum(x1,y2+1)-getsum(x2+1,y1)+getsum(x1,y1);
          writeln(ans);
        end;
      read(m);
    end;
end.
SuperBrother打鼹鼠

测试数据 #0: Accepted, time = 0 ms, mem = 4780 KiB, score = 10

测试数据 #1: Accepted, time = 46 ms, mem = 4776 KiB, score = 10

测试数据 #2: Accepted, time = 7 ms, mem = 4776 KiB, score = 10

测试数据 #3: Accepted, time = 0 ms, mem = 4780 KiB, score = 10

测试数据 #4: Accepted, time = 46 ms, mem = 4776 KiB, score = 10

测试数据 #5: Accepted, time = 15 ms, mem = 4776 KiB, score = 10

测试数据 #6: Accepted, time = 15 ms, mem = 4780 KiB, score = 10

测试数据 #7: Accepted, time = 15 ms, mem = 4772 KiB, score = 10

测试数据 #8: Accepted, time = 31 ms, mem = 4780 KiB, score = 10

测试数据 #9: Accepted, time = 31 ms, mem = 4776 KiB, score = 10

(神马居然没秒杀?难道还有更快做法?!)

原文地址:https://www.cnblogs.com/Sky-Grey/p/3579389.html