• 算法模板——线段树6(二维线段树:区域加法+区域求和)(求助phile)


    实现功能——对于一个N×M的方格,1:输入一个区域,将此区域全部值作加法;2:输入一个区域,求此区域全部值的和

    其实和一维线段树同理,只是不知道为什么速度比想象的慢那么多,求解释。。。@acphile

    (还有代码略恶心,求原谅。。。^_^)

     1 const tvp=8000000;
     2 var
     3    i,j,k,l,m,n,a1,a2,a3,a4,a5:longint;
     4    a,b:array[0..tvp] of longint;
     5    c1,c2:char;
     6 function max(x,y:longint):longint;inline;
     7          begin
     8               if x>y then max:=x else max:=y;
     9          end;
    10 function min(x,y:longint):longint;inline;
    11          begin
    12               if x<y then min:=x else min:=y;
    13          end;
    14 function op(z,x1,y1,x2,y2,lx,ly,rx,ry,nu,d:longint):longint;inline;
    15          var
    16             a1,a2,a3,a4,a5:longint;
    17          begin
    18               if (lx>rx) or (ly>ry) then exit(0);
    19               if (x1=lx) and (y1=ly) and (x2=rx) and (y2=ry) then
    20                  begin
    21                       b[z]:=b[z]+nu;
    22                       exit(nu*(rx-lx+1)*(ry-ly+1));
    23                  end;
    24               a2:=op(z*4-2,x1,y1,(x1+x2) div 2,(y1+y2) div 2,lx,ly,min(rx,(x1+x2) div 2),min(ry,(y1+y2) div 2),nu,d);
    25               a3:=op(z*4-1,x1,(y1+y2) div 2+1,(x1+x2) div 2,y2,lx,max(ly,(y1+y2) div 2+1),min(rx,(x1+x2) div 2),ry,nu,d);
    26               a4:=op(z*4,(x1+x2) div 2+1,y1,x2,(y1+y2) div 2,max(lx,(x1+x2) div 2+1),ly,rx,min(ry,(y1+y2) div 2),nu,d);
    27               a5:=op(z*4+1,(x1+x2) div 2+1,(y1+y2) div 2+1,x2,y2,max(lx,(x1+x2) div 2+1),max(ly,(y1+y2) div 2+1),rx,ry,nu,d);
    28               a[z]:=a[z]+a2+a3+a4+a5;
    29               exit(a2+a3+a4+a5);
    30          end;
    31 function cal(z,x1,y1,x2,y2,lx,ly,rx,ry,d:longint):longint;inline;
    32          var a1,a2,a3,a4,a5:longint;
    33          begin
    34               if (lx>rx) or (ly>ry) then exit(0);
    35               d:=d+b[z];
    36               if (x1=lx) and (y1=ly) and (x2=rx) and (y2=ry) then exit(a[z]+d*(rx-lx+1)*(ry-ly+1));
    37               a2:=cal(z*4-2,x1,y1,(x1+x2) div 2,(y1+y2) div 2,lx,ly,min(rx,(x1+x2) div 2),min(ry,(y1+y2) div 2),d);
    38               a3:=cal(z*4-1,x1,(y1+y2) div 2+1,(x1+x2) div 2,y2,lx,max(ly,(y1+y2) div 2+1),min(rx,(x1+x2) div 2),ry,d);
    39               a4:=cal(z*4,(x1+x2) div 2+1,y1,x2,(y1+y2) div 2,max(lx,(x1+x2) div 2+1),ly,rx,min(ry,(y1+y2) div 2),d);
    40               a5:=cal(z*4+1,(x1+x2) div 2+1,(y1+y2) div 2+1,x2,y2,max(lx,(x1+x2) div 2+1),max(ly,(y1+y2) div 2+1),rx,ry,d);
    41               exit(a2+a3+a4+a5);
    42          end;
    43 begin
    44      readln(c1,n,m);
    45      fillchar(a,sizeof(a),0);
    46      fillchar(b,sizeof(b),0);
    47      while not(eof) do
    48            begin
    49                 read(c1,a1,a2,a3,a4);
    50                 case c1 of
    51                      'L':begin
    52                               readln(a5);
    53                               op(1,1,1,n,m,a1,a2,a3,a4,a5,0);
    54                      end;
    55                      'k':begin
    56                               readln;
    57                               writeln(cal(1,1,1,n,m,a1,a2,a3,a4,0));
    58                      end;
    59                 end;
    60            end;
    61 end.       
  • 相关阅读:
    jQuery tablesort插件推荐
    更改firefox默认搜索引擎
    Chrome Firefox 自定义背景色
    python 基础之列表切片内置方法
    python 基础之while无限循环
    python 基础之for循环有限循环
    python 基础之格式化输出
    nginx之HTTP模块配置
    Kubernetes之pod的属性
    nginx的工作流程
  • 原文地址:https://www.cnblogs.com/HansBug/p/4237745.html
Copyright © 2020-2023  润新知