Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一个p的树状数组

Posted by wukewen at 2014-01-08 22:25:04 on Problem 1195 and last updated at 2014-01-08 22:25:31
var
  tree:array[0..1025,0..1025] of int64;
  n,size:longint;
procedure plus(x,y,n:longint);
  var
    t:longint;
  begin
    while x<=size do
      begin
      t:=y;
      while t<=size do
        begin
        inc(tree[x,t],n);
        t:=t+t and -t;
        end;
      x:=x+x and -x;
      end;
  end;
function sum(x,y:longint):int64;
  var
    t:longint;
  begin
    sum:=0;
    while x>0 do
      begin
      t:=y;
      while t>0 do
        begin
        inc(sum,tree[x,t]);
        t:=t-t and -t;
        end;
      x:=x-x and -x;
      end;
  end;
procedure add;
  var
    x,y,z:longint;
  begin
    read(x,y,z);
    inc(x); inc(y);
    plus(x,y,z);
  end;
procedure getans;
  var
    x1,y1,x2,y2:longint;
    k:int64;
  begin
    read(x1,y1,x2,y2);
    inc(x1); inc(x2); inc(y1); inc(y2);
    k:=sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1);
    writeln(k);
  end;
begin
  read(n);
  while n<>3 do
    begin
    case n of
      0:begin read(size); fillchar(tree,sizeof(tree),0); end;
      1:add;
      2:getans;
      end;
    read(n);
    end;
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator