Algorithm template--segment Tree 9 (interval plus + interval sum + interval square and)

Source: Internet
Author: User

Title, implement a program, enter the number of n, the following maintenance:

1.1 x y for the [x, Y] Interval

2.2 x y to find the sum of squares of [x, Y] intervals

3.3 x y z Add the [x, Y] interval to all Z

4.4 x Y for the sum of the sum of 22 numbers multiplied by the [x, Y] interval (in fact 4 is a simple combination of 1, 2)

As follows:

1 var2 I,j,k,l,m,n:longint;3 T:int64;4A,b,c:Array[0..100000] ofInt64;5 type6Rec=Record7 Aa,bb:int64;8     End;9 Ten functionMax (x,y:longint): Longint; One          begin A               ifX>y ThenMax:=xElsemax:=y; -          End; - functionmin (x,y:longint): Longint; the          begin -               ifX<y ThenMin:=xElsemin:=y; -          End; - procedurebuilt (z,x,y:longint); +           begin -                ifX=y Then +                   begin A read (a[z]); atb[z]:=a[z]*A[z]; -                   End -                Else -                    begin -Built (z*2, X, (X+y)Div 2); -Built (z*2+1, (X+y)Div 2+1, y); ina[z]:=a[z*2]+a[z*2+1]; -b[z]:=b[z*2]+b[z*2+1]; to                    End; +c[z]:=0; -           End; the procedureext (z,x,y:longint); *           begin $                ifc[z]=0  Thenexit;Panax Notoginsengb[z]:=b[z]+2*c[z]*a[z]+ (y-x+1) *c[z]*C[z]; -a[z]:=a[z]+ (y-x+1)*C[z]; the                ifX<>y Then +                   begin Ac[z*2]:=c[z*2]+C[z]; thec[z*2+1]:=c[z*2+1]+C[z]; +                   End; -c[z]:=0; $           End; $ functionsuma (z,x,y,l,r:longint): Int64; -          begin -               ifL>r ThenExit0); the ext (z,x,y); -               if(x=l) and(Y=R) Thenexit (A[z]);WuyiExit (Suma (z*2, X, (X+y)Div 2, L,min (R, (x+y)Div 2)) +suma (z*2+1, (X+y)Div 2+1, Y,max ((x+y)Div 2+1, L), R); the          End; - functionSumb (z,x,y,l,r:longint): Int64; Wu          begin -               ifL>r ThenExit0); About ext (z,x,y); $               if(x=l) and(Y=R) Thenexit (B[z]); -Exit (Sumb (z*2, X, (X+y)Div 2, L,min (R, (x+y)Div 2)) +sumb (z*2+1, (X+y)Div 2+1, Y,max ((x+y)Div 2+1, L), R); -          End; - functionAdd (Z,x,y,l,r:longint;t:int64): rec; A          varTt,tt1,tt2:rec; +          begin thett.aa:=0; tt.bb:=0; -               ifL>r Thenexit (TT); $               if(x=l) and(Y=R) Then the                  begin thett.aa:=t* (r-l+1); thett.bb:=2*t* (a[z]+c[z]* (r-l+1)) +t*t* (r-l+1); thec[z]:=c[z]+T; - exit (TT); in                  End; the ext (z,x,y); theTt1:=add (z*2, X, (X+y)Div 2, L,min (R, (x+y)Div 2), t); AboutTt2:=add (z*2+1, (X+y)Div 2+1, Y,max ((x+y)Div 2+1, L), r,t); thett.aa:=tt1.aa+Tt2.aa; thett.bb:=tt1.bb+tt2.bb; thea[z]:=a[z]+Tt.aa; +b[z]:=b[z]+tt.bb; - exit (TT); the          End;Bayi begin the READLN (n); theBuilt1,1, n); -       whileTrue Do -          begin the read (j,k,l); the                CaseJ of the                    1:begin theWriteln (Suma (1,1, n,k,l)); -                    End; the                    2:begin theWriteln (Sumb (1,1, n,k,l)); the                    End;94                    3:begin the read (t); theAdd1,1, n,k,l,t); the                    End;98                    4:begin AboutT:=suma (1,1, n,k,l); -Writeln ((T*t-sumb (1,1, n,k,l))Div 2);101                    End;102               End;103 Readln;104          End; the End.106                        

Algorithm template--segment Tree 9 (interval plus + interval sum + interval square and)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.