# 算法模板——线段树6（二维线段树：区域加法+区域求和）（求助phile）

（还有代码略恶心，求原谅。。。^_^)

``` 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
45      fillchar(a,sizeof(a),0);
46      fillchar(b,sizeof(b),0);
47      while not(eof) do
48            begin
50                 case c1 of
51                      'L':begin
53                               op(1,1,1,n,m,a1,a2,a3,a4,a5,0);
54                      end;
55                      'k':begin
57                               writeln(cal(1,1,1,n,m,a1,a2,a3,a4,0));
58                      end;
59                 end;
60            end;
61 end.       ```

250 篇文章37 人订阅

0 条评论

## 相关文章

### 动态规划

​ 动态规划一般来说和分治有点类似都是让他们去处理相同的子问题，但是在动态规划里面你会遇到更多的相同子问题。然后我们就会导致很多的重复计算，所以一般我们可...

3005

993

1923

37211

3294

1914

### 机器学习之线性代数

完整内容已上传到github：https://github.com/ZingP/machine-learning/tree/master/linear_al...

1711

4574