bzoj 3132: 上帝造题的七分钟 (二维树状数组)

推推公式,最后变成四个东西的前缀和

然后不知道为什么一直wa,数据在本地测是没有错的&

好心的管理员还给了某位p党大神a了的代码,感人肺腑(虽然还是没发现到底我的程序是问题)

var
  f1,f2,f3,f4:array[0..2500,0..2500]of longint;
  x1,y1,x2,y2,n,m,j:longint;
  ch:char;
 
 
 
function lowbit(x:longint):longint;
begin
  exit(x and (-x));
end;
 
procedure add(x,y,z:longint);
var
  i,j:longint;
begin
  if (x=0) or (y=0) then exit;
  i:=x;
  while i<=n do begin
    j:=y;
    while j<=m do begin
      inc(f1[i,j],z);
      inc(f2[i,j],z*y);
      inc(f3[i,j],z*x);
      inc(f4[i,j],z*x*y);
      inc(j,lowbit(j));
    end;
    inc(i,lowbit(i));
  end;
end;
 
function askans(x,y:longint):longint;
var
  ans,i,j:longint;
begin
  ans:=0;
  i:=x;
  while i>=1 do begin
    j:=y;
    while j>=1 do begin
      ans:=ans+f1[i,j]*(x+1)*(y+1);
      ans:=ans-f2[i,j]*(x+1);
      ans:=ans-f3[i,j]*(y+1);
      ans:=ans+f4[i,j];
      dec(j,lowbit(j));
    end;
    dec(i,lowbit(i));
  end;
  //writeln(x,' ',y,' ',ans);
  exit(ans);
end;
 
begin
  readln(ch,n,m);
  fillchar(f1,sizeof(f1),0);
  fillchar(f2,sizeof(f2),0);
  fillchar(f3,sizeof(f3),0);
  fillchar(f4,sizeof(f4),0);
  while not eof do begin
    read(ch);
    if ch='L' then begin
      readln(x1,y1,x2,y2,j);
      add(x1,y1,j);
      add(x1,y2+1,-j);
      add(x2+1,y1,-j);
      add(x2+1,y2+1,j);
    end
    else
    if ch='k' then begin
      readln(x1,y1,x2,y2);
      writeln(askans(x2,y2)+askans(x1-1,y1-1)-askans(x1-1,y2)-askans(x2,y1-1));
    end;
  end;
end.
View Code

 

posted @ 2015-03-22 22:06  Macaulish  阅读(240)  评论(0编辑  收藏  举报