标签:
该模板实现的功能——进行区间的乘法和加法,以及区间的求和(1:乘法 2:加法 3:求和)详见BZOJ1798
1 type 2 vet=record 3 a0,a1:int64; 4 end; 5 var 6 i,j,k,l,m,n,a2,a3,a4:longint; 7 p:int64; 8 d1,d2,d:vet; 9 a,c:array[0..1000000] of int64; 10 b:array[0..1000000] of vet; 11 function min(x,y:longint):longint;inline; 12 begin 13 if x<y then min:=x else min:=y; 14 end; 15 function max(x,y:longint):longint;inline; 16 begin 17 if x>y then max:=x else max:=y; 18 end; 19 function merge(d1,d2:vet):vet;inline; 20 var d3:vet; 21 begin 22 d3:=d1; 23 d3.a0:=d3.a0 mod p; 24 d3.a1:=d3.a1 mod p; 25 d2.a0:=d2.a0 mod p; 26 d2.a1:=d2.a1 mod p; 27 d3.a0:=(d3.a0*d2.a0) mod p; 28 d3.a1:=((d3.a1*d2.a0) mod p+d2.a1) mod p; 29 exit(d3); 30 end; 31 procedure built(z,x,y:longint);inline; 32 begin 33 if (x=y) then 34 a[z]:=c[x] mod p 35 else 36 begin 37 built(z*2,x,(x+y) div 2); 38 built(z*2+1,(x+y) div 2+1,y); 39 a[z]:=(a[z*2]+a[z*2+1]) mod p; 40 end; 41 b[z].a0:=1;b[z].a1:=0; 42 end; 43 procedure ext(z,x,y:longint);inline; 44 begin 45 a[z]:=((a[z]*b[z].a0) mod p+b[z].a1*(y-x+1)) mod p; 46 b[z*2]:=merge(b[z*2],b[z]); 47 b[z*2+1]:=merge(b[z*2+1],b[z]); 48 b[z].a0:=1;b[z].a1:=0; 49 end; 50 function op(z,x,y,l,r:longint;d:vet):int64;inline; 51 var a2,a3,a4:int64; 52 begin 53 if l>r then exit(0); 54 ext(z,x,y);a2:=a[z]; 55 if (x=l) and (y=r) then 56 begin 57 b[z]:=d; 58 exit((a2*(b[z].a0-1)) mod p+(b[z].a1*(r-l+1)) mod p); 59 end 60 else 61 begin 62 a3:=op(z*2,x,(x+y) div 2,l,min((x+y) div 2,r),d) mod p; 63 a4:=op(z*2+1,(x+y) div 2+1,y,max((x+y) div 2+1,l),r,d) mod p; 64 a[z]:=(a[z]+(a3+a4) mod p) mod p;exit((a3+a4) mod p); 65 end; 66 end; 67 function sum(z,x,y,l,r:longint;d:vet):int64;inline; 68 var d1,d2:vet; 69 begin 70 if l>r then exit(0); 71 d1:=b[z];d1:=merge(d1,d);; 72 if (x=l) and (y=r) then 73 exit(((d1.a0*a[z]) mod p+(d1.a1*(r-l+1)) mod p) mod p) 74 else 75 exit((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)) mod p); 76 end; 77 procedure showoff(z,x,y,l:longint);inline; 78 begin 79 writeln(‘‘:l*2,z,‘(‘,x,‘,‘,y,‘) = Tag=(‘,b[z].a0,‘,‘,b[z].a1,‘) ‘,a[z]); 80 if x<y then 81 begin 82 showoff(z*2,x,(x+y) div 2,l+1); 83 showoff(z*2+1,(x+y) div 2+1,y,l+1); 84 end; 85 end; 86 begin 87 readln(n,p); 88 for i:=1 to n do read(c[i]); 89 readln; 90 built(1,1,n); 91 readln(m); 92 for i:=1 to m do 93 begin 94 read(j); 95 case j of 96 1:begin 97 readln(a2,a3,a4); 98 d1.a0:=a4;d1.a1:=0; 99 op(1,1,n,a2,a3,d1); 100 end; 101 2:begin 102 readln(a2,a3,a4); 103 d1.a0:=1;d1.a1:=a4; 104 op(1,1,n,a2,a3,d1); 105 end; 106 3:begin 107 readln(a2,a3); 108 d1.a0:=1;d1.a1:=0; 109 writeln((sum(1,1,n,a2,a3,d1)+p) mod p); 110 end; 111 end; 112 end; 113 readln; 114 end. 115
标签:
原文地址:http://www.cnblogs.com/HansBug/p/4222591.html