The function implemented by the template--multiplication and addition of intervals, and summation of intervals (1: Multiplication 2: Addition 3: summation) See BZOJ1798
1 type2vet=Record3 A0,a1:int64;4 End;5 var6 I,j,k,l,m,n,a2,a3,a4:longint;7 P:int64;8 D1,d2,d:vet;9A,c:Array[0..1000000] ofInt64;TenB:Array[0..1000000] ofvet; One functionmin (x,y:longint): Longint;inline; A begin - ifX<y ThenMin:=xElsemin:=y; - End; the functionMax (x,y:longint): Longint;inline; - begin - ifX>y ThenMax:=xElsemax:=y; - End; + functionmerge (D1,d2:vet): Vet;inline; - varD3:vet; + begin Ad3:=D1; atD3.a0:=d3.a0MoDp; -D3.a1:=d3.a1MoDp; -D2.a0:=d2.a0MoDp; -D2.a1:=d2.a1MoDp; -d3.a0:= (D3.A0*D2.A0)MoDp; -D3.a1:= ((D3.A1*D2.A0)MoDP+D2.A1)MoDp; in exit (D3); - End; to procedurebuilt (z,x,y:longint), inline; + begin - if(x=y) Then theA[Z]:=C[X]MoDP * Else $ beginPanax NotoginsengBuilt (z*2, X, (X+y)Div 2); -Built (z*2+1, (X+y)Div 2+1, y); theA[z]:= (a[z*2]+a[z*2+1])MoDp; + End; Ab[z].a0:=1; b[z].a1:=0; the End; + procedureext (z,x,y:longint); inline; - begin $A[z]:= ((A[Z]*B[Z].A0)MoDP+b[z].a1* (y-x+1))MoDp; $b[z*2]:=merge (b[z*2],b[z]); -b[z*2+1]:=merge (b[z*2+1],b[z]); -b[z].a0:=1; b[z].a1:=0; the End; - functionop (z,x,y,l,r:longint;d:vet): Int64;inline;Wuyi varA2,a3,a4:int64; the begin - ifL>r ThenExit0); WuExt (z,x,y); a2:=A[z]; - if(x=l) and(Y=R) Then About begin $b[z]:=D; -Exit ((a2* (b[z].a0-1))MoDP+ (b[z].a1* (r-l+1))MoDp); - End - Else A begin +A3:=op (z*2, X, (X+y)Div 2, L,min ((x+y)Div 2, R), D)MoDp; theA4:=op (z*2+1, (X+y)Div 2+1, Y,max ((x+y)Div 2+1, l), r,d)MoDp; -A[z]:= (a[z]+ (A3+A4)MoDPMoDP;exit ((A3+A4)MoDp); $ End; the End; the functionsum (z,x,y,l,r:longint;d:vet): Int64;inline; the varD1,d2:vet; the begin - ifL>r ThenExit0); inD1:=b[z];d 1:=merge (d1,d);; the if(x=l) and(Y=R) Then theExit ((D1.a0*a[z)MoDP+ (d1.a1* (r-l+1))MoDPMoDp) About Else theExit (SUM (z*2, X, (X+y)Div 2, L,min ((x+y)Div 2, R), D1) +sum (z*2+1, (X+y)Div 2+1, Y,max ((x+y)Div 2+1, l), R,D1))MoDp); the End; the procedureShowoff (z,x,y,l:longint), inline; + begin -Writeln ("': l*2Z'('X','Y') = tag= (', B[Z].A0,',', B[Z].A1,') ', A[z]); the ifX<y ThenBayi begin theShowoff (z*2, X, (X+y)Div 2, L +1); theShowoff (z*2+1, (X+y)Div 2+1, y,l+1); - End; - End; the begin the readln (n,p); the fori:=1 toN Doread (c[i]); the Readln; -Built1,1, n); the Readln (m); the fori:=1 toM Do the begin94 Read (j); the CaseJ of the 1:begin the readln (A2,A3,A4);98d1.a0:=a4;d1.a1:=0; AboutOp1,1, n,a2,a3,d1); - End;101 2:begin102 readln (A2,A3,A4);103d1.a0:=1;d 1.a1:=A4;104Op1,1, n,a2,a3,d1); the End;106 3:begin107 readln (A2,A3);108d1.a0:=1;d 1.a1:=0;109Writeln (SUM (1,1, N,A2,A3,D1) +p)MoDp); the End;111 End; the End;113 Readln; the End. the
Algorithm template--segment tree interval modification interval summation